-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathFormLineStringLength.tsx
62 lines (56 loc) · 1.68 KB
/
FormLineStringLength.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import React, { useEffect, useState, useMemo } from 'react';
import type { WidgetProps } from '@rjsf/utils';
import cx from 'classnames';
import { toNumber } from 'lodash';
import { useTranslation } from 'react-i18next';
export const FormLineStringLength = ({ id, value, required, onChange }: WidgetProps) => {
const { t } = useTranslation();
const [length, setLength] = useState<number>(toNumber(value));
useEffect(() => {
setLength(value);
}, [value]);
const hasChanged = useMemo(() => toNumber(value) !== length, [value, length]);
return (
<div>
<input
className="form-control"
id={id}
required={required}
type="number"
value={length}
onKeyDown={(e) => {
if (hasChanged) {
if (e.key === 'Enter') {
e.preventDefault();
onChange(length);
} else if (e.key === 'Escape') {
setLength(value);
}
}
}}
onChange={(e) => {
setLength(e.target.valueAsNumber);
}}
/>
<div className="my-1 d-flex justify-content-end">
<button
type="button"
disabled={!hasChanged}
className={cx('btn btn-sm btn-secondary', { disabled: !hasChanged })}
onClick={() => setLength(toNumber(value))}
>
{t('common.cancel')}
</button>
<button
type="button"
disabled={!hasChanged}
className={cx('ml-1 btn btn-sm btn-primary', { disabled: !hasChanged })}
onClick={() => onChange(length)}
>
{t('common.confirm')}
</button>
</div>
</div>
);
};
export default FormLineStringLength;