-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathStdcmInputVia.tsx
60 lines (50 loc) · 1.73 KB
/
StdcmInputVia.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
import { useMemo, useEffect, useState } from 'react';
import { Input } from '@osrd-project/ui-core';
import { debounce } from 'lodash';
import { useTranslation } from 'react-i18next';
import { StdcmStopTypes } from '../../types';
type StdcmInputViaProps = {
stopType: StdcmStopTypes;
stopDuration?: number;
updatePathStepStopTime: (stopTime: string) => void;
};
const StdcmInputVia = ({ stopType, stopDuration, updatePathStepStopTime }: StdcmInputViaProps) => {
const { t } = useTranslation('stdcm');
const [pathStepStopTime, setPathStepStopTime] = useState('');
const stopWarning = stopType === StdcmStopTypes.DRIVER_SWITCH && stopDuration && stopDuration < 3;
const debounceUpdatePathStepStopTime = useMemo(
() => debounce((value) => updatePathStepStopTime(value), 300),
[]
);
useEffect(() => {
setPathStepStopTime(stopDuration ? `${stopDuration}` : '');
}, [stopDuration]);
return (
stopType !== StdcmStopTypes.PASSAGE_TIME && (
<div className="stop-time">
<Input
id="stdcm-via-stop-time"
type="text"
label={t('trainPath.stopFor')}
onChange={(e) => {
// TODO: Find a better way to prevent user from entering decimal values
const value = e.target.value.replace(/[\D.,]/g, '');
setPathStepStopTime(value);
debounceUpdatePathStepStopTime(value);
}}
value={pathStepStopTime}
trailingContent="minutes"
statusWithMessage={
stopWarning
? {
status: 'warning',
message: t('trainPath.warningMinStopTime'),
}
: undefined
}
/>
</div>
)
);
};
export default StdcmInputVia;