@alfalab/core-components-time-input 2.1.19 → 2.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alfalab/core-components-time-input",
3
- "version": "2.1.19",
3
+ "version": "2.2.1",
4
4
  "description": "",
5
5
  "keywords": [],
6
6
  "license": "MIT",
@@ -15,7 +15,7 @@
15
15
  "react-dom": "^16.9.0 || ^17.0.1 || ^18.0.0"
16
16
  },
17
17
  "dependencies": {
18
- "@alfalab/core-components-input": "^11.1.18",
18
+ "@alfalab/core-components-input": "^12.0.1",
19
19
  "tslib": "^2.4.0"
20
20
  }
21
21
  }
@@ -0,0 +1,87 @@
1
+ /* eslint-disable no-useless-escape */
2
+
3
+ import React, { ChangeEvent, useState } from 'react';
4
+
5
+ import { Input, InputProps } from '@alfalab/core-components-input';
6
+
7
+ import { format, isCompleteTimeInput, isValidInputValue } from './utils';
8
+
9
+ export type TimeInputProps = Omit<InputProps, 'onChange'> & {
10
+ /**
11
+ * Обработчик изменения значения
12
+ */
13
+ onChange?: (
14
+ event: ChangeEvent<HTMLInputElement>,
15
+ payload: { hours: number; mins: number; value: string },
16
+ ) => void;
17
+
18
+ /**
19
+ * Обработчик окончания ввода
20
+ */
21
+ onComplete?: (
22
+ event: ChangeEvent<HTMLInputElement>,
23
+ payload: { hours: number; mins: number; value: string },
24
+ ) => void;
25
+ };
26
+
27
+ export const TimeInput = React.forwardRef<HTMLInputElement, TimeInputProps>(
28
+ (
29
+ { defaultValue = '', value: propValue, onChange, onComplete, className, ...restProps },
30
+ ref,
31
+ ) => {
32
+ const [value, setValue] = useState(propValue || defaultValue);
33
+
34
+ const handleChange = (event: ChangeEvent<HTMLInputElement>) => {
35
+ const { value: newValue } = event.target;
36
+
37
+ if (newValue.length > 5) return;
38
+
39
+ // Позволяем вводить только цифры и двоеточия
40
+ if (/[^\d:]/.test(newValue)) {
41
+ return;
42
+ }
43
+
44
+ const colon = newValue.match(/\:/g);
45
+
46
+ // Не даем вводить больше, чем одно двоеточие
47
+ if (colon && colon.length > 1) {
48
+ return;
49
+ }
50
+
51
+ const formattedValue = format(newValue);
52
+
53
+ const formattedValueArr = formattedValue.split(':');
54
+ const hours = Number(formattedValueArr[0]);
55
+ const mins = Number(formattedValueArr[1]);
56
+
57
+ setValue(formattedValue);
58
+
59
+ if (onChange) onChange(event, { hours, mins, value: formattedValue });
60
+
61
+ if (isCompleteTimeInput(formattedValue)) {
62
+ const valid = formattedValue.length > 0 && isValidInputValue(formattedValue);
63
+
64
+ if (!valid) return;
65
+
66
+ if (onComplete) {
67
+ onComplete(event, { hours, mins, value: formattedValue });
68
+ }
69
+ }
70
+ };
71
+
72
+ const handleClearClick = () => {
73
+ setValue('');
74
+ };
75
+
76
+ return (
77
+ <Input
78
+ {...restProps}
79
+ ref={ref}
80
+ value={value}
81
+ className={className}
82
+ onChange={handleChange}
83
+ onClear={handleClearClick}
84
+ />
85
+ );
86
+ },
87
+ );
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from './Component';
2
+ export { isValidInputValue } from './utils';
@@ -0,0 +1,31 @@
1
+ /* eslint-disable no-useless-escape */
2
+
3
+ export const DATE_MASK = [/\d/, /\d/, ':', /\d/, /\d/];
4
+
5
+ export const isCompleteTimeInput = (input: string) => input.length === DATE_MASK.length;
6
+
7
+ export const isValidTimeFormat = (value: string): boolean => {
8
+ const timeArr = value.split(':');
9
+ const hours = timeArr[0];
10
+ const mins = timeArr[1];
11
+
12
+ if (hours.length !== 2 || Number(hours) > 23) {
13
+ return false;
14
+ }
15
+
16
+ if (mins.length !== 2 || Number(mins) > 59) {
17
+ return false;
18
+ }
19
+
20
+ return true;
21
+ };
22
+
23
+ export const isValidInputValue = (inputValue?: string) =>
24
+ !inputValue || (isCompleteTimeInput(inputValue) && isValidTimeFormat(inputValue));
25
+
26
+ export const format = (value: string): string =>
27
+ value
28
+ .replace(/^(\d\d)(\d)$/, '$1:$2') // 123 => 12:3
29
+ .replace(/^(\d\d)(\d\d)/, '$1:$2') // 12345 => 12:45 (если вместо двоеточия введена цифра, она обратно заменяется на двоеточие)
30
+ .replace(/^(\d):(\d\d)(\d)/, '$1:$2') // 1:234 => 1:23
31
+ .replace(/\:$/, ''); // 12: => 12 || : => void
@@ -0,0 +1 @@
1
+ export * from './format';