mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-13 11:23:50 +00:00
refract make reusable components
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
import { IconButton } from '@chakra-ui/button';
|
||||
import { FiPlus } from "react-icons/fi";
|
||||
|
||||
export default function AddIconBtn(props) {
|
||||
return (
|
||||
<IconButton
|
||||
size={props.size || 'xs'}
|
||||
icon={<FiPlus />}
|
||||
colorScheme='blue'
|
||||
onClick={() => props.clickHandler}
|
||||
/>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
import { IconButton } from '@chakra-ui/button';
|
||||
import { FiMinusCircle } from 'react-icons/fi';
|
||||
|
||||
export default function BlockIconBtn(props) {
|
||||
return (
|
||||
<IconButton
|
||||
size={props.size || 'xs'}
|
||||
icon={<FiMinusCircle />}
|
||||
colorScheme='purple'
|
||||
onClick={() => props.clickHandler}
|
||||
/>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
import { IconButton } from '@chakra-ui/button';
|
||||
import { FiClock } from 'react-icons/fi';
|
||||
|
||||
export default function DelayIconBtn(props) {
|
||||
return (
|
||||
<IconButton
|
||||
size={props.size || 'xs'}
|
||||
icon={<FiClock />}
|
||||
colorScheme='yellow'
|
||||
onClick={() => props.clickHandler}
|
||||
/>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
import { IconButton } from '@chakra-ui/button';
|
||||
import { FiMinus } from 'react-icons/fi';
|
||||
|
||||
export default function DeleteIconBtn(props) {
|
||||
return (
|
||||
<IconButton
|
||||
size={props.size || 'xs'}
|
||||
icon={<FiMinus />}
|
||||
colorScheme='red'
|
||||
onClick={() => props.clickHandler}
|
||||
/>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
import EditableTimer from '../../input/EditableTimer';
|
||||
import DelayValue from '../../input/DelayValue';
|
||||
|
||||
export default function EventTimes(props) {
|
||||
const { updateValues, delay, timeStart, timeEnd } = props;
|
||||
|
||||
return (
|
||||
<>
|
||||
<EditableTimer
|
||||
name='timeStart'
|
||||
updateValues={updateValues}
|
||||
time={timeStart}
|
||||
delay={delay}
|
||||
/>
|
||||
<EditableTimer
|
||||
name='timeEnd'
|
||||
updateValues={updateValues}
|
||||
time={timeEnd}
|
||||
delay={delay}
|
||||
/>
|
||||
<DelayValue delay={delay} />
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -1,17 +1,22 @@
|
||||
import { addMinutes, format, parseISO } from "date-fns";
|
||||
import { addMinutes, format, parseISO } from 'date-fns';
|
||||
|
||||
export const timeFormat = 'HH:mm';
|
||||
export const timeFormatSeconds = 'HH:mm:ss';
|
||||
|
||||
// make date with string
|
||||
// make date with string
|
||||
export const timeToDate = (time) => {
|
||||
const today = new Date();
|
||||
return new Date(today.toDateString() + ' ' + time);
|
||||
};
|
||||
|
||||
// utility to parse and format
|
||||
export const dateToTime = (time) => {
|
||||
const fTime = parseISO(time, 1);
|
||||
return format(fTime, timeFormat);
|
||||
};
|
||||
|
||||
// small shorthand for adding delay and formatting date
|
||||
export const addAndFormat = (time, delay) => {
|
||||
const fTime = parseISO(time, 1);
|
||||
return format(addMinutes(fTime, delay), timeFormat);
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
import style from './EditableTimer.module.css';
|
||||
|
||||
export default function DelayValue(props) {
|
||||
const { delay } = props;
|
||||
const delayed = delay > 0;
|
||||
return (
|
||||
<div className={delayed && style.delayValue}>
|
||||
{delayed && <span>{`+ ${delay}`}</span>}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
import { Editable, EditableInput, EditablePreview } from '@chakra-ui/editable';
|
||||
import style from './EditableText.module.css';
|
||||
|
||||
export default function EditableText(props) {
|
||||
const { label, defaultValue, placeholder, handleSubmit } = props;
|
||||
return (
|
||||
<div style={{ display: 'block' }}>
|
||||
<span className={props.underlined ? style.titleUnderlined : style.title}>
|
||||
{label}
|
||||
</span>
|
||||
<Editable
|
||||
onSubmit={(v) => handleSubmit(v)}
|
||||
defaultValue={defaultValue}
|
||||
placeholder={placeholder}
|
||||
style={{ display: 'inline' }}
|
||||
>
|
||||
<EditablePreview />
|
||||
<EditableInput style={{ width: '13em', minWidth: '13em' }} />
|
||||
</Editable>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
.title,
|
||||
.titleUnderlined {
|
||||
padding-left: 1em;
|
||||
font-size: 0.8em;
|
||||
color: #888;
|
||||
display: inline-block;
|
||||
width: 6em;
|
||||
cursor: default;
|
||||
}
|
||||
|
||||
.titleUnderlined {
|
||||
border-bottom: 1px solid #0001;
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
import { Editable, EditableInput, EditablePreview } from '@chakra-ui/editable';
|
||||
import { useEffect, useState } from 'react';
|
||||
import { addAndFormat, timeToDate, dateToTime } from '../dateConfig';
|
||||
import style from './EditableTimer.module.css';
|
||||
|
||||
export default function EditableTimer(props) {
|
||||
const { name, updateValues, time, delay } = props;
|
||||
const [value, setValue] = useState('');
|
||||
const [showValue, setShowValue] = useState('');
|
||||
|
||||
// prepare time fields
|
||||
useEffect(() => {
|
||||
setValue(addAndFormat(time, delay));
|
||||
}, [time, delay]);
|
||||
|
||||
const handleSubmit = (submitedVal) => {
|
||||
console.log('edit: on submit');
|
||||
|
||||
if (submitedVal === value) return;
|
||||
|
||||
updateValues(name, timeToDate(submitedVal));
|
||||
setValue(addAndFormat(time, delay));
|
||||
};
|
||||
|
||||
const showNormal = () => {
|
||||
console.log('edit: on edit');
|
||||
setValue(dateToTime(time));
|
||||
};
|
||||
|
||||
const handleChange = (submitedVal) => {
|
||||
console.log('edit: change handler', submitedVal);
|
||||
setValue(submitedVal);
|
||||
};
|
||||
|
||||
const handleBlur = () => {
|
||||
console.log('edit: onBlur');
|
||||
setValue(addAndFormat(time, delay));
|
||||
};
|
||||
|
||||
console.log('hh', value, time);
|
||||
|
||||
return (
|
||||
<div className={style.time}>
|
||||
<Editable
|
||||
// submitOnBlur={false}
|
||||
// onEdit={showNormal}
|
||||
onSubmit={(v) => handleSubmit(v)}
|
||||
onChange={(v) => handleChange(v)}
|
||||
onBlur={(v) => handleBlur(v)}
|
||||
value={value}
|
||||
placeholder='--:--'
|
||||
style={{ textAlign: 'center' }}
|
||||
className={delay > 0 && style.delayedEditable}
|
||||
>
|
||||
<EditablePreview />
|
||||
<EditableInput type='time' min='00:00' max='23:59' />
|
||||
</Editable>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
.time {
|
||||
background-color: #fff8;
|
||||
border: 1px solid #0001;
|
||||
border-radius: 4px;
|
||||
width: 5em;
|
||||
height: fit-content;
|
||||
}
|
||||
|
||||
.delayedEditable {
|
||||
background-color: #ecc94b22;
|
||||
}
|
||||
|
||||
.delayValue {
|
||||
font-size: 14px;
|
||||
line-height: 2em;
|
||||
color: #555;
|
||||
background-color: #ffeaa066;
|
||||
border-radius: 4px;
|
||||
text-align: center;
|
||||
border: 1px solid #0001;
|
||||
}
|
||||
Reference in New Issue
Block a user