@alfalab/core-components-time-input 2.2.37 → 2.2.38
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/moderncssm/Component.d.ts +45 -0
- package/moderncssm/Component.js +47 -0
- package/moderncssm/index.d.ts +2 -0
- package/moderncssm/index.js +2 -0
- package/moderncssm/utils/format.d.ts +6 -0
- package/moderncssm/utils/format.js +23 -0
- package/moderncssm/utils/index.d.ts +1 -0
- package/moderncssm/utils/index.js +1 -0
- package/package.json +4 -4
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import { ChangeEvent } from "react";
|
|
4
|
+
import { InputProps } from "@alfalab/core-components-input";
|
|
5
|
+
type TimeInputProps = Omit<InputProps, 'onChange'> & {
|
|
6
|
+
/**
|
|
7
|
+
* Обработчик изменения значения
|
|
8
|
+
*/
|
|
9
|
+
onChange?: (event: ChangeEvent<HTMLInputElement>, payload: {
|
|
10
|
+
hours: number;
|
|
11
|
+
mins: number;
|
|
12
|
+
value: string;
|
|
13
|
+
}) => void;
|
|
14
|
+
/**
|
|
15
|
+
* Обработчик окончания ввода
|
|
16
|
+
*/
|
|
17
|
+
onComplete?: (event: ChangeEvent<HTMLInputElement>, payload: {
|
|
18
|
+
hours: number;
|
|
19
|
+
mins: number;
|
|
20
|
+
value: string;
|
|
21
|
+
}) => void;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* @deprecated
|
|
25
|
+
* use UniversalDateInput instead
|
|
26
|
+
*/
|
|
27
|
+
declare const TimeInput: React.ForwardRefExoticComponent<Omit<InputProps, "onChange"> & {
|
|
28
|
+
/**
|
|
29
|
+
* Обработчик изменения значения
|
|
30
|
+
*/
|
|
31
|
+
onChange?: ((event: ChangeEvent<HTMLInputElement>, payload: {
|
|
32
|
+
hours: number;
|
|
33
|
+
mins: number;
|
|
34
|
+
value: string;
|
|
35
|
+
}) => void) | undefined;
|
|
36
|
+
/**
|
|
37
|
+
* Обработчик окончания ввода
|
|
38
|
+
*/
|
|
39
|
+
onComplete?: ((event: ChangeEvent<HTMLInputElement>, payload: {
|
|
40
|
+
hours: number;
|
|
41
|
+
mins: number;
|
|
42
|
+
value: string;
|
|
43
|
+
}) => void) | undefined;
|
|
44
|
+
} & React.RefAttributes<HTMLInputElement>>;
|
|
45
|
+
export { TimeInputProps, TimeInput };
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import React, { useState } from 'react';
|
|
2
|
+
import { Input } from '@alfalab/core-components-input/moderncssm';
|
|
3
|
+
import { format, isCompleteTimeInput, isValidInputValue } from './utils/format.js';
|
|
4
|
+
|
|
5
|
+
/* eslint-disable no-useless-escape */
|
|
6
|
+
/**
|
|
7
|
+
* @deprecated
|
|
8
|
+
* use UniversalDateInput instead
|
|
9
|
+
*/
|
|
10
|
+
const TimeInput = React.forwardRef(({ defaultValue = '', value: propValue, onChange, onComplete, className, ...restProps }, ref) => {
|
|
11
|
+
const [value, setValue] = useState(propValue || defaultValue);
|
|
12
|
+
const handleChange = (event) => {
|
|
13
|
+
const { value: newValue } = event.target;
|
|
14
|
+
if (newValue.length > 5)
|
|
15
|
+
return;
|
|
16
|
+
// Позволяем вводить только цифры и двоеточия
|
|
17
|
+
if (/[^\d:]/.test(newValue)) {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
const colon = newValue.match(/\:/g);
|
|
21
|
+
// Не даем вводить больше, чем одно двоеточие
|
|
22
|
+
if (colon && colon.length > 1) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
const formattedValue = format(newValue);
|
|
26
|
+
const formattedValueArr = formattedValue.split(':');
|
|
27
|
+
const hours = Number(formattedValueArr[0]);
|
|
28
|
+
const mins = Number(formattedValueArr[1]);
|
|
29
|
+
setValue(formattedValue);
|
|
30
|
+
if (onChange)
|
|
31
|
+
onChange(event, { hours, mins, value: formattedValue });
|
|
32
|
+
if (isCompleteTimeInput(formattedValue)) {
|
|
33
|
+
const valid = formattedValue.length > 0 && isValidInputValue(formattedValue);
|
|
34
|
+
if (!valid)
|
|
35
|
+
return;
|
|
36
|
+
if (onComplete) {
|
|
37
|
+
onComplete(event, { hours, mins, value: formattedValue });
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
const handleClearClick = () => {
|
|
42
|
+
setValue('');
|
|
43
|
+
};
|
|
44
|
+
return (React.createElement(Input, { ...restProps, ref: ref, value: value, className: className, onChange: handleChange, onClear: handleClearClick }));
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
export { TimeInput };
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
declare const DATE_MASK: (string | RegExp)[];
|
|
2
|
+
declare const isCompleteTimeInput: (input: string) => boolean;
|
|
3
|
+
declare const isValidTimeFormat: (value: string) => boolean;
|
|
4
|
+
declare const isValidInputValue: (inputValue?: string) => boolean;
|
|
5
|
+
declare const format: (value: string) => string;
|
|
6
|
+
export { DATE_MASK, isCompleteTimeInput, isValidTimeFormat, isValidInputValue, format };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/* eslint-disable no-useless-escape */
|
|
2
|
+
const DATE_MASK = [/\d/, /\d/, ':', /\d/, /\d/];
|
|
3
|
+
const isCompleteTimeInput = (input) => input.length === DATE_MASK.length;
|
|
4
|
+
const isValidTimeFormat = (value) => {
|
|
5
|
+
const timeArr = value.split(':');
|
|
6
|
+
const hours = timeArr[0];
|
|
7
|
+
const mins = timeArr[1];
|
|
8
|
+
if (hours.length !== 2 || Number(hours) > 23) {
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
11
|
+
if (mins.length !== 2 || Number(mins) > 59) {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
return true;
|
|
15
|
+
};
|
|
16
|
+
const isValidInputValue = (inputValue) => !inputValue || (isCompleteTimeInput(inputValue) && isValidTimeFormat(inputValue));
|
|
17
|
+
const format = (value) => value
|
|
18
|
+
.replace(/^(\d\d)(\d)$/, '$1:$2') // 123 => 12:3
|
|
19
|
+
.replace(/^(\d\d)(\d\d)/, '$1:$2') // 12345 => 12:45 (если вместо двоеточия введена цифра, она обратно заменяется на двоеточие)
|
|
20
|
+
.replace(/^(\d):(\d\d)(\d)/, '$1:$2') // 1:234 => 1:23
|
|
21
|
+
.replace(/\:$/, ''); // 12: => 12 || : => void
|
|
22
|
+
|
|
23
|
+
export { DATE_MASK, format, isCompleteTimeInput, isValidInputValue, isValidTimeFormat };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./format";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { DATE_MASK, format, isCompleteTimeInput, isValidInputValue, isValidTimeFormat } from './format.js';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alfalab/core-components-time-input",
|
|
3
|
-
"version": "2.2.
|
|
3
|
+
"version": "2.2.38",
|
|
4
4
|
"description": "",
|
|
5
5
|
"keywords": [],
|
|
6
6
|
"license": "MIT",
|
|
@@ -15,9 +15,9 @@
|
|
|
15
15
|
"react-dom": "^16.9.0 || ^17.0.1 || ^18.0.0"
|
|
16
16
|
},
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"@alfalab/core-components-input": "^15.0
|
|
18
|
+
"@alfalab/core-components-input": "^15.1.0",
|
|
19
19
|
"tslib": "^2.4.0"
|
|
20
20
|
},
|
|
21
|
-
"themesVersion": "13.0
|
|
22
|
-
"varsVersion": "9.
|
|
21
|
+
"themesVersion": "13.1.0",
|
|
22
|
+
"varsVersion": "9.12.0"
|
|
23
23
|
}
|