Files
ontime/client/src/common/components/input/DelayInput.jsx
T
Carlos Valente b7e758bf24 Chore/ts (#199)
* chore: add typescript and dependencies
* refactor: remove react imports
* refactor: fix entrypoint
* chore: upgrade chakra and deps
* chore: configure eslint
2022-09-14 21:01:58 +02:00

63 lines
1.5 KiB
React

import { useCallback, useEffect, useState } from 'react';
import { Editable, EditableInput, EditablePreview } from '@chakra-ui/react';
import PropTypes from 'prop-types';
import { clamp } from '../../utils/math';
import style from './TimeInput.module.css';
const inputProps = {
width: 20,
fontWeight: 400,
backgroundColor: 'rgba(0,0,0,0.05)',
color: '#fff',
border: '1px solid #ecc94b55',
borderRadius: '8px',
placeholder: '-',
textAlign: 'center',
};
export default function DelayInput(props) {
const { actionHandler, value } = props;
const [_value, setValue] = useState(value);
useEffect(() => {
if (value == null) return;
setValue(value);
}, [value]);
const handleSubmit = useCallback(
(newValue) => {
if (newValue === value) return;
if (newValue === '') setValue(0);
// convert to ms and updates
const msVal = clamp(newValue, -60, 60) * 60000;
actionHandler('update', { field: 'duration', value: msVal });
},
[actionHandler, value]
);
const labelText = `minutes ${value >= 0 ? 'delayed' : 'ahead'}`;
return (
<div className={style.timeInput}>
<Editable
{...inputProps}
value={_value}
onChange={(v) => setValue(v)}
onSubmit={(v) => handleSubmit(v)}
>
<EditablePreview />
<EditableInput type='number' min='-60' max='60' />
</Editable>
<span className={style.label}>{labelText}</span>
</div>
);
}
DelayInput.propTypes = {
actionHandler: PropTypes.func,
value: PropTypes.number,
};