@patternfly/react-core 4.189.3 → 4.191.0
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/CHANGELOG.md +41 -0
- package/dist/esm/components/NumberInput/NumberInput.d.ts.map +1 -1
- package/dist/esm/components/NumberInput/NumberInput.js +13 -1
- package/dist/esm/components/NumberInput/NumberInput.js.map +1 -1
- package/dist/esm/components/Tabs/Tabs.d.ts +2 -0
- package/dist/esm/components/Tabs/Tabs.d.ts.map +1 -1
- package/dist/esm/components/Tabs/Tabs.js +24 -18
- package/dist/esm/components/Tabs/Tabs.js.map +1 -1
- package/dist/esm/components/TimePicker/TimePicker.d.ts +6 -4
- package/dist/esm/components/TimePicker/TimePicker.d.ts.map +1 -1
- package/dist/esm/components/TimePicker/TimePicker.js +37 -26
- package/dist/esm/components/TimePicker/TimePicker.js.map +1 -1
- package/dist/esm/components/TimePicker/TimePickerUtils.d.ts +3 -2
- package/dist/esm/components/TimePicker/TimePickerUtils.d.ts.map +1 -1
- package/dist/esm/components/TimePicker/TimePickerUtils.js +57 -42
- package/dist/esm/components/TimePicker/TimePickerUtils.js.map +1 -1
- package/dist/esm/helpers/util.d.ts +2 -1
- package/dist/esm/helpers/util.d.ts.map +1 -1
- package/dist/esm/helpers/util.js +5 -4
- package/dist/esm/helpers/util.js.map +1 -1
- package/dist/js/components/NumberInput/NumberInput.d.ts.map +1 -1
- package/dist/js/components/NumberInput/NumberInput.js +13 -1
- package/dist/js/components/NumberInput/NumberInput.js.map +1 -1
- package/dist/js/components/Tabs/Tabs.d.ts +2 -0
- package/dist/js/components/Tabs/Tabs.d.ts.map +1 -1
- package/dist/js/components/Tabs/Tabs.js +24 -18
- package/dist/js/components/Tabs/Tabs.js.map +1 -1
- package/dist/js/components/TimePicker/TimePicker.d.ts +6 -4
- package/dist/js/components/TimePicker/TimePicker.d.ts.map +1 -1
- package/dist/js/components/TimePicker/TimePicker.js +36 -25
- package/dist/js/components/TimePicker/TimePicker.js.map +1 -1
- package/dist/js/components/TimePicker/TimePickerUtils.d.ts +3 -2
- package/dist/js/components/TimePicker/TimePickerUtils.d.ts.map +1 -1
- package/dist/js/components/TimePicker/TimePickerUtils.js +59 -43
- package/dist/js/components/TimePicker/TimePickerUtils.js.map +1 -1
- package/dist/js/helpers/util.d.ts +2 -1
- package/dist/js/helpers/util.d.ts.map +1 -1
- package/dist/js/helpers/util.js +5 -4
- package/dist/js/helpers/util.js.map +1 -1
- package/dist/umd/react-core.css +18 -0
- package/dist/umd/react-core.js +135 -90
- package/dist/umd/react-core.min.css +18 -0
- package/dist/umd/react-core.min.js +3 -3
- package/package.json +6 -6
- package/src/components/NumberInput/NumberInput.tsx +17 -0
- package/src/components/NumberInput/__tests__/__snapshots__/NumberInput.test.tsx.snap +10 -0
- package/src/components/NumberInput/examples/NumberInput.md +43 -0
- package/src/components/Tabs/Tabs.tsx +25 -18
- package/src/components/TimePicker/TimePicker.tsx +54 -31
- package/src/components/TimePicker/TimePickerUtils.tsx +76 -39
- package/src/components/TimePicker/__tests__/TimePicker.test.tsx +49 -11
- package/src/components/TimePicker/examples/TimePicker.md +19 -2
- package/src/helpers/util.ts +10 -4
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@patternfly/react-core",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.191.0",
|
|
4
4
|
"description": "This library provides a set of common React components for use with the PatternFly reference implementation.",
|
|
5
5
|
"main": "dist/js/index.js",
|
|
6
6
|
"module": "dist/esm/index.js",
|
|
@@ -34,16 +34,16 @@
|
|
|
34
34
|
"generate": "node scripts/copyStyles.js"
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"@patternfly/react-icons": "^4.
|
|
38
|
-
"@patternfly/react-styles": "^4.
|
|
39
|
-
"@patternfly/react-tokens": "^4.
|
|
37
|
+
"@patternfly/react-icons": "^4.42.0",
|
|
38
|
+
"@patternfly/react-styles": "^4.41.0",
|
|
39
|
+
"@patternfly/react-tokens": "^4.43.0",
|
|
40
40
|
"focus-trap": "6.2.2",
|
|
41
41
|
"react-dropzone": "9.0.0",
|
|
42
42
|
"tippy.js": "5.1.2",
|
|
43
43
|
"tslib": "^2.0.0"
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
46
|
-
"@patternfly/patternfly": "4.
|
|
46
|
+
"@patternfly/patternfly": "4.167.0",
|
|
47
47
|
"@rollup/plugin-commonjs": "^21.0.0",
|
|
48
48
|
"@rollup/plugin-node-resolve": "^13.0.0",
|
|
49
49
|
"@rollup/plugin-replace": "^3.0.0",
|
|
@@ -60,5 +60,5 @@
|
|
|
60
60
|
"react": "^16.8.0 || ^17.0.0",
|
|
61
61
|
"react-dom": "^16.8.0 || ^17.0.0"
|
|
62
62
|
},
|
|
63
|
-
"gitHead": "
|
|
63
|
+
"gitHead": "77aae8d7980584c12ddcab1522ed9b7931588150"
|
|
64
64
|
}
|
|
@@ -4,6 +4,7 @@ import { css } from '@patternfly/react-styles';
|
|
|
4
4
|
import MinusIcon from '@patternfly/react-icons/dist/esm/icons/minus-icon';
|
|
5
5
|
import PlusIcon from '@patternfly/react-icons/dist/esm/icons/plus-icon';
|
|
6
6
|
import { Button, ButtonProps } from '../Button';
|
|
7
|
+
import { KEY_CODES } from '../../helpers';
|
|
7
8
|
|
|
8
9
|
export interface NumberInputProps extends React.HTMLProps<HTMLDivElement> {
|
|
9
10
|
/** Value of the number input */
|
|
@@ -44,6 +45,19 @@ export interface NumberInputProps extends React.HTMLProps<HTMLDivElement> {
|
|
|
44
45
|
plusBtnProps?: ButtonProps;
|
|
45
46
|
}
|
|
46
47
|
|
|
48
|
+
type DefaultKeyDownHandlerArgs = Pick<NumberInputProps, 'inputName' | 'onMinus' | 'onPlus'>;
|
|
49
|
+
|
|
50
|
+
const defaultKeyDownHandler = (args: DefaultKeyDownHandlerArgs) => (event: React.KeyboardEvent<HTMLInputElement>) => {
|
|
51
|
+
if (KEY_CODES.ARROW_UP === event.keyCode && args.onPlus) {
|
|
52
|
+
event.preventDefault();
|
|
53
|
+
args.onPlus(null, args.inputName);
|
|
54
|
+
}
|
|
55
|
+
if (KEY_CODES.ARROW_DOWN === event.keyCode && args.onMinus) {
|
|
56
|
+
event.preventDefault();
|
|
57
|
+
args.onMinus(null, args.inputName);
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
|
|
47
61
|
export const NumberInput: React.FunctionComponent<NumberInputProps> = ({
|
|
48
62
|
value = 0,
|
|
49
63
|
className,
|
|
@@ -66,6 +80,8 @@ export const NumberInput: React.FunctionComponent<NumberInputProps> = ({
|
|
|
66
80
|
...props
|
|
67
81
|
}: NumberInputProps) => {
|
|
68
82
|
const numberInputUnit = <div className={css(styles.numberInputUnit)}>{unit}</div>;
|
|
83
|
+
const keyDownHandler =
|
|
84
|
+
inputProps && inputProps.onKeyDown ? inputProps.onKeyDown : defaultKeyDownHandler({ inputName, onMinus, onPlus });
|
|
69
85
|
return (
|
|
70
86
|
<div
|
|
71
87
|
className={css(styles.numberInput, className)}
|
|
@@ -100,6 +116,7 @@ export const NumberInput: React.FunctionComponent<NumberInputProps> = ({
|
|
|
100
116
|
{...(onChange && { onChange })}
|
|
101
117
|
{...(!onChange && { readOnly: true })}
|
|
102
118
|
{...inputProps}
|
|
119
|
+
onKeyDown={keyDownHandler}
|
|
103
120
|
/>
|
|
104
121
|
<Button
|
|
105
122
|
variant="control"
|
|
@@ -72,6 +72,7 @@ exports[`numberInput disables lower threshold 1`] = `
|
|
|
72
72
|
<input
|
|
73
73
|
aria-label="Input"
|
|
74
74
|
className="pf-c-form-control"
|
|
75
|
+
onKeyDown={[Function]}
|
|
75
76
|
readOnly={true}
|
|
76
77
|
type="number"
|
|
77
78
|
value={0}
|
|
@@ -209,6 +210,7 @@ exports[`numberInput disables upper threshold 1`] = `
|
|
|
209
210
|
<input
|
|
210
211
|
aria-label="Input"
|
|
211
212
|
className="pf-c-form-control"
|
|
213
|
+
onKeyDown={[Function]}
|
|
212
214
|
readOnly={true}
|
|
213
215
|
type="number"
|
|
214
216
|
value={100}
|
|
@@ -361,6 +363,7 @@ exports[`numberInput passes button props successfully 1`] = `
|
|
|
361
363
|
<input
|
|
362
364
|
aria-label="Input"
|
|
363
365
|
className="pf-c-form-control"
|
|
366
|
+
onKeyDown={[Function]}
|
|
364
367
|
readOnly={true}
|
|
365
368
|
type="number"
|
|
366
369
|
value={5}
|
|
@@ -504,6 +507,7 @@ exports[`numberInput passes input props successfully 1`] = `
|
|
|
504
507
|
className="pf-c-form-control"
|
|
505
508
|
name="test-name"
|
|
506
509
|
onChange={[MockFunction]}
|
|
510
|
+
onKeyDown={[Function]}
|
|
507
511
|
type="number"
|
|
508
512
|
value={5}
|
|
509
513
|
/>
|
|
@@ -645,6 +649,7 @@ exports[`numberInput renders custom width 1`] = `
|
|
|
645
649
|
<input
|
|
646
650
|
aria-label="Input"
|
|
647
651
|
className="pf-c-form-control"
|
|
652
|
+
onKeyDown={[Function]}
|
|
648
653
|
readOnly={true}
|
|
649
654
|
type="number"
|
|
650
655
|
value={5}
|
|
@@ -783,6 +788,7 @@ exports[`numberInput renders defaults & extra props 1`] = `
|
|
|
783
788
|
<input
|
|
784
789
|
aria-label="Input"
|
|
785
790
|
className="pf-c-form-control"
|
|
791
|
+
onKeyDown={[Function]}
|
|
786
792
|
readOnly={true}
|
|
787
793
|
type="number"
|
|
788
794
|
value={0}
|
|
@@ -922,6 +928,7 @@ exports[`numberInput renders disabled 1`] = `
|
|
|
922
928
|
aria-label="Input"
|
|
923
929
|
className="pf-c-form-control"
|
|
924
930
|
disabled={true}
|
|
931
|
+
onKeyDown={[Function]}
|
|
925
932
|
readOnly={true}
|
|
926
933
|
type="number"
|
|
927
934
|
value={90}
|
|
@@ -1066,6 +1073,7 @@ exports[`numberInput renders unit & position 1`] = `
|
|
|
1066
1073
|
<input
|
|
1067
1074
|
aria-label="Input"
|
|
1068
1075
|
className="pf-c-form-control"
|
|
1076
|
+
onKeyDown={[Function]}
|
|
1069
1077
|
readOnly={true}
|
|
1070
1078
|
type="number"
|
|
1071
1079
|
value={5}
|
|
@@ -1204,6 +1212,7 @@ exports[`numberInput renders unit 1`] = `
|
|
|
1204
1212
|
<input
|
|
1205
1213
|
aria-label="Input"
|
|
1206
1214
|
className="pf-c-form-control"
|
|
1215
|
+
onKeyDown={[Function]}
|
|
1207
1216
|
readOnly={true}
|
|
1208
1217
|
type="number"
|
|
1209
1218
|
value={5}
|
|
@@ -1345,6 +1354,7 @@ exports[`numberInput renders value 1`] = `
|
|
|
1345
1354
|
<input
|
|
1346
1355
|
aria-label="Input"
|
|
1347
1356
|
className="pf-c-form-control"
|
|
1357
|
+
onKeyDown={[Function]}
|
|
1348
1358
|
readOnly={true}
|
|
1349
1359
|
type="number"
|
|
1350
1360
|
value={90}
|
|
@@ -357,3 +357,46 @@ class NumberInputSizes extends React.Component {
|
|
|
357
357
|
}
|
|
358
358
|
}
|
|
359
359
|
```
|
|
360
|
+
|
|
361
|
+
### Custom increment/decrement
|
|
362
|
+
|
|
363
|
+
```js
|
|
364
|
+
import React from 'react';
|
|
365
|
+
import { NumberInput } from '@patternfly/react-core';
|
|
366
|
+
|
|
367
|
+
class CustomStepNumberInput extends React.Component {
|
|
368
|
+
constructor(props) {
|
|
369
|
+
super(props);
|
|
370
|
+
this.state = {
|
|
371
|
+
value: 90,
|
|
372
|
+
step: 3,
|
|
373
|
+
};
|
|
374
|
+
|
|
375
|
+
this.stepper = step => () => {
|
|
376
|
+
this.setState(prev => ({...prev, value: prev.value + step }))
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
this.onChange = event => {
|
|
380
|
+
this.setState({
|
|
381
|
+
value: Number(event.target.value)
|
|
382
|
+
});
|
|
383
|
+
};
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
render() {
|
|
387
|
+
const { value } = this.state;
|
|
388
|
+
return (
|
|
389
|
+
<NumberInput
|
|
390
|
+
value={value}
|
|
391
|
+
onMinus={this.stepper(-3)}
|
|
392
|
+
onChange={this.onChange}
|
|
393
|
+
onPlus={this.stepper(3)}
|
|
394
|
+
inputName="input"
|
|
395
|
+
inputAriaLabel="number input"
|
|
396
|
+
minusBtnAriaLabel="minus"
|
|
397
|
+
plusBtnAriaLabel="plus"
|
|
398
|
+
/>
|
|
399
|
+
);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
```
|
|
@@ -129,6 +129,8 @@ export class Tabs extends React.Component<TabsProps, TabsState> {
|
|
|
129
129
|
}
|
|
130
130
|
}
|
|
131
131
|
|
|
132
|
+
scrollTimeout: NodeJS.Timeout = null;
|
|
133
|
+
|
|
132
134
|
static defaultProps: PickOptional<TabsProps> = {
|
|
133
135
|
activeKey: 0,
|
|
134
136
|
onSelect: () => undefined as any,
|
|
@@ -182,28 +184,32 @@ export class Tabs extends React.Component<TabsProps, TabsState> {
|
|
|
182
184
|
}
|
|
183
185
|
|
|
184
186
|
handleScrollButtons = () => {
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
187
|
+
// add debounce to the scroll event
|
|
188
|
+
clearTimeout(this.scrollTimeout);
|
|
189
|
+
this.scrollTimeout = setTimeout(() => {
|
|
190
|
+
const container = this.tabList.current;
|
|
191
|
+
let disableLeftScrollButton = true;
|
|
192
|
+
let disableRightScrollButton = true;
|
|
193
|
+
let showScrollButtons = false;
|
|
189
194
|
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
195
|
+
if (container && !this.props.isVertical) {
|
|
196
|
+
// get first element and check if it is in view
|
|
197
|
+
const overflowOnLeft = !isElementInView(container, container.firstChild as HTMLElement, false);
|
|
193
198
|
|
|
194
|
-
|
|
195
|
-
|
|
199
|
+
// get last element and check if it is in view
|
|
200
|
+
const overflowOnRight = !isElementInView(container, container.lastChild as HTMLElement, false);
|
|
196
201
|
|
|
197
|
-
|
|
202
|
+
showScrollButtons = overflowOnLeft || overflowOnRight;
|
|
198
203
|
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
204
|
+
disableLeftScrollButton = !overflowOnLeft;
|
|
205
|
+
disableRightScrollButton = !overflowOnRight;
|
|
206
|
+
}
|
|
207
|
+
this.setState({
|
|
208
|
+
showScrollButtons,
|
|
209
|
+
disableLeftScrollButton,
|
|
210
|
+
disableRightScrollButton
|
|
211
|
+
});
|
|
212
|
+
}, 100);
|
|
207
213
|
};
|
|
208
214
|
|
|
209
215
|
scrollLeft = () => {
|
|
@@ -261,6 +267,7 @@ export class Tabs extends React.Component<TabsProps, TabsState> {
|
|
|
261
267
|
window.removeEventListener('resize', this.handleScrollButtons, false);
|
|
262
268
|
}
|
|
263
269
|
}
|
|
270
|
+
clearTimeout(this.scrollTimeout);
|
|
264
271
|
}
|
|
265
272
|
|
|
266
273
|
componentDidUpdate(prevProps: TabsProps) {
|
|
@@ -17,7 +17,8 @@ import {
|
|
|
17
17
|
pmSuffix,
|
|
18
18
|
getHours,
|
|
19
19
|
getMinutes,
|
|
20
|
-
isWithinMinMax
|
|
20
|
+
isWithinMinMax,
|
|
21
|
+
getSeconds
|
|
21
22
|
} from './TimePickerUtils';
|
|
22
23
|
|
|
23
24
|
export interface TimePickerProps
|
|
@@ -41,7 +42,7 @@ export interface TimePickerProps
|
|
|
41
42
|
/** True if the time is 24 hour time. False if the time is 12 hour time */
|
|
42
43
|
is24Hour?: boolean;
|
|
43
44
|
/** Optional event handler called each time the value in the time picker input changes. */
|
|
44
|
-
onChange?: (time: string, hour?: number, minute?: number, isValid?: boolean) => void;
|
|
45
|
+
onChange?: (time: string, hour?: number, minute?: number, seconds?: number, isValid?: boolean) => void;
|
|
45
46
|
/** Optional validator can be provided to override the internal time validator. */
|
|
46
47
|
validateTime?: (time: string) => boolean;
|
|
47
48
|
/** Id of the time picker */
|
|
@@ -61,10 +62,12 @@ export interface TimePickerProps
|
|
|
61
62
|
stepMinutes?: number;
|
|
62
63
|
/** Additional props for input field */
|
|
63
64
|
inputProps?: TextInputProps;
|
|
64
|
-
/** A time string indicating the minimum value allowed. The format could be
|
|
65
|
+
/** A time string indicating the minimum value allowed. The format could be an ISO 8601 formatted date string or in 'HH{delimiter}MM' format */
|
|
65
66
|
minTime?: string | Date;
|
|
66
|
-
/** A time string indicating the maximum value allowed. The format could be
|
|
67
|
+
/** A time string indicating the maximum value allowed. The format could be an ISO 8601 formatted date string or in 'HH{delimiter}MM' format */
|
|
67
68
|
maxTime?: string | Date;
|
|
69
|
+
/** Includes number of seconds with the chosen time and allows users to manually edit the seconds value. */
|
|
70
|
+
includeSeconds?: boolean;
|
|
68
71
|
}
|
|
69
72
|
|
|
70
73
|
interface TimePickerState {
|
|
@@ -106,24 +109,26 @@ export class TimePicker extends React.Component<TimePickerProps, TimePickerState
|
|
|
106
109
|
|
|
107
110
|
constructor(props: TimePickerProps) {
|
|
108
111
|
super(props);
|
|
109
|
-
const { is24Hour, delimiter, time } = this.props;
|
|
112
|
+
const { is24Hour, delimiter, time, includeSeconds } = this.props;
|
|
110
113
|
let { minTime, maxTime } = this.props;
|
|
111
114
|
if (minTime === '') {
|
|
112
|
-
|
|
115
|
+
const minSeconds = includeSeconds ? `${delimiter}00` : '';
|
|
116
|
+
minTime = is24Hour ? `00${delimiter}00${minSeconds}` : `12${delimiter}00${minSeconds} AM`;
|
|
113
117
|
}
|
|
114
118
|
if (maxTime === '') {
|
|
115
|
-
|
|
119
|
+
const maxSeconds = includeSeconds ? `${delimiter}59` : '';
|
|
120
|
+
maxTime = is24Hour ? `23${delimiter}59${maxSeconds}` : `11${delimiter}59${maxSeconds} PM`;
|
|
116
121
|
}
|
|
117
122
|
const timeRegex = this.getRegExp();
|
|
118
123
|
this.state = {
|
|
119
124
|
isInvalid: false,
|
|
120
125
|
isOpen: false,
|
|
121
|
-
timeState: parseTime(time, timeRegex, delimiter, !is24Hour),
|
|
126
|
+
timeState: parseTime(time, timeRegex, delimiter, !is24Hour, includeSeconds),
|
|
122
127
|
focusedIndex: null,
|
|
123
128
|
scrollIndex: 0,
|
|
124
129
|
timeRegex,
|
|
125
|
-
minTimeState: parseTime(minTime, timeRegex, delimiter, !is24Hour),
|
|
126
|
-
maxTimeState: parseTime(maxTime, timeRegex, delimiter, !is24Hour)
|
|
130
|
+
minTimeState: parseTime(minTime, timeRegex, delimiter, !is24Hour, includeSeconds),
|
|
131
|
+
maxTimeState: parseTime(maxTime, timeRegex, delimiter, !is24Hour, includeSeconds)
|
|
127
132
|
};
|
|
128
133
|
}
|
|
129
134
|
|
|
@@ -183,7 +188,7 @@ export class TimePicker extends React.Component<TimePickerProps, TimePickerState
|
|
|
183
188
|
|
|
184
189
|
componentDidUpdate(prevProps: TimePickerProps, prevState: TimePickerState) {
|
|
185
190
|
const { timeState, isOpen, isInvalid, timeRegex } = this.state;
|
|
186
|
-
const { time, is24Hour, delimiter } = this.props;
|
|
191
|
+
const { time, is24Hour, delimiter, includeSeconds } = this.props;
|
|
187
192
|
if (isOpen && !prevState.isOpen && timeState && !isInvalid) {
|
|
188
193
|
this.scrollToSelection(timeState);
|
|
189
194
|
}
|
|
@@ -194,7 +199,7 @@ export class TimePicker extends React.Component<TimePickerProps, TimePickerState
|
|
|
194
199
|
}
|
|
195
200
|
if (time !== '' && time !== prevProps.time) {
|
|
196
201
|
this.setState({
|
|
197
|
-
timeState: parseTime(time, timeRegex, delimiter, !is24Hour)
|
|
202
|
+
timeState: parseTime(time, timeRegex, delimiter, !is24Hour, includeSeconds)
|
|
198
203
|
});
|
|
199
204
|
}
|
|
200
205
|
}
|
|
@@ -272,10 +277,16 @@ export class TimePicker extends React.Component<TimePickerProps, TimePickerState
|
|
|
272
277
|
});
|
|
273
278
|
};
|
|
274
279
|
|
|
275
|
-
getRegExp = () =>
|
|
276
|
-
this.props
|
|
277
|
-
|
|
278
|
-
|
|
280
|
+
getRegExp = (includeSeconds: boolean = true) => {
|
|
281
|
+
const { is24Hour, delimiter } = this.props;
|
|
282
|
+
let baseRegex = `\\s*(\\d\\d?)${delimiter}([0-5]\\d)`;
|
|
283
|
+
|
|
284
|
+
if (includeSeconds) {
|
|
285
|
+
baseRegex += `${delimiter}?([0-5]\\d)?`;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
return new RegExp(`^${baseRegex}${is24Hour ? '' : '\\s*([AaPp][Mm])?'}\\s*$`);
|
|
289
|
+
};
|
|
279
290
|
|
|
280
291
|
getOptions = () =>
|
|
281
292
|
(this.menuRef && this.menuRef.current ? Array.from(this.menuRef.current.children) : []) as HTMLElement[];
|
|
@@ -284,14 +295,16 @@ export class TimePicker extends React.Component<TimePickerProps, TimePickerState
|
|
|
284
295
|
if (this.props.validateTime) {
|
|
285
296
|
return this.props.validateTime(time);
|
|
286
297
|
}
|
|
287
|
-
|
|
288
|
-
|
|
298
|
+
|
|
299
|
+
const { delimiter, is24Hour, includeSeconds } = this.props;
|
|
300
|
+
return validateTime(time, this.getRegExp(includeSeconds), delimiter, !is24Hour);
|
|
289
301
|
};
|
|
290
302
|
|
|
291
303
|
isValidTime = (time: string) => {
|
|
292
|
-
const { delimiter } = this.props;
|
|
304
|
+
const { delimiter, includeSeconds } = this.props;
|
|
293
305
|
const { minTimeState, maxTimeState } = this.state;
|
|
294
|
-
|
|
306
|
+
|
|
307
|
+
return isWithinMinMax(minTimeState, maxTimeState, time, delimiter, includeSeconds);
|
|
295
308
|
};
|
|
296
309
|
|
|
297
310
|
isValid = (time: string) => this.isValidFormat(time) && this.isValidTime(time);
|
|
@@ -300,8 +313,9 @@ export class TimePicker extends React.Component<TimePickerProps, TimePickerState
|
|
|
300
313
|
// on close, parse and validate input
|
|
301
314
|
this.setState(prevState => {
|
|
302
315
|
const { timeRegex, isInvalid } = prevState;
|
|
303
|
-
const { delimiter, is24Hour } = this.props;
|
|
304
|
-
const time = parseTime(prevState.timeState, timeRegex, delimiter, !is24Hour);
|
|
316
|
+
const { delimiter, is24Hour, includeSeconds } = this.props;
|
|
317
|
+
const time = parseTime(prevState.timeState, timeRegex, delimiter, !is24Hour, includeSeconds);
|
|
318
|
+
|
|
305
319
|
return {
|
|
306
320
|
isOpen,
|
|
307
321
|
timeState: time,
|
|
@@ -312,11 +326,13 @@ export class TimePicker extends React.Component<TimePickerProps, TimePickerState
|
|
|
312
326
|
|
|
313
327
|
onSelect = (selection: string) => {
|
|
314
328
|
const { timeRegex, timeState } = this.state;
|
|
315
|
-
const { delimiter, is24Hour } = this.props;
|
|
316
|
-
const time = parseTime(selection, timeRegex, delimiter, !is24Hour);
|
|
329
|
+
const { delimiter, is24Hour, includeSeconds } = this.props;
|
|
330
|
+
const time = parseTime(selection, timeRegex, delimiter, !is24Hour, includeSeconds);
|
|
331
|
+
|
|
317
332
|
if (time !== timeState) {
|
|
318
333
|
this.onInputChange(time);
|
|
319
334
|
}
|
|
335
|
+
|
|
320
336
|
this.setState({
|
|
321
337
|
isOpen: false
|
|
322
338
|
});
|
|
@@ -330,11 +346,15 @@ export class TimePicker extends React.Component<TimePickerProps, TimePickerState
|
|
|
330
346
|
};
|
|
331
347
|
|
|
332
348
|
onInputChange = (newTime: string) => {
|
|
333
|
-
|
|
334
|
-
|
|
349
|
+
const { onChange } = this.props;
|
|
350
|
+
const { timeRegex } = this.state;
|
|
351
|
+
|
|
352
|
+
if (onChange) {
|
|
353
|
+
onChange(
|
|
335
354
|
newTime,
|
|
336
|
-
getHours(newTime,
|
|
337
|
-
getMinutes(newTime,
|
|
355
|
+
getHours(newTime, timeRegex),
|
|
356
|
+
getMinutes(newTime, timeRegex),
|
|
357
|
+
getSeconds(newTime, timeRegex),
|
|
338
358
|
this.isValid(newTime)
|
|
339
359
|
);
|
|
340
360
|
}
|
|
@@ -347,8 +367,9 @@ export class TimePicker extends React.Component<TimePickerProps, TimePickerState
|
|
|
347
367
|
|
|
348
368
|
onBlur = (event: React.FocusEvent<HTMLInputElement>) => {
|
|
349
369
|
const { timeRegex } = this.state;
|
|
350
|
-
const { delimiter, is24Hour } = this.props;
|
|
351
|
-
const time = parseTime(event.currentTarget.value, timeRegex, delimiter, !is24Hour);
|
|
370
|
+
const { delimiter, is24Hour, includeSeconds } = this.props;
|
|
371
|
+
const time = parseTime(event.currentTarget.value, timeRegex, delimiter, !is24Hour, includeSeconds);
|
|
372
|
+
|
|
352
373
|
this.setState({
|
|
353
374
|
isInvalid: !this.isValid(time)
|
|
354
375
|
});
|
|
@@ -369,13 +390,15 @@ export class TimePicker extends React.Component<TimePickerProps, TimePickerState
|
|
|
369
390
|
stepMinutes,
|
|
370
391
|
width,
|
|
371
392
|
delimiter,
|
|
393
|
+
inputProps,
|
|
372
394
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
373
395
|
onChange,
|
|
374
396
|
time,
|
|
375
397
|
validateTime,
|
|
376
|
-
inputProps,
|
|
377
398
|
minTime,
|
|
378
399
|
maxTime,
|
|
400
|
+
includeSeconds,
|
|
401
|
+
/* eslint-enable @typescript-eslint/no-unused-vars */
|
|
379
402
|
...props
|
|
380
403
|
} = this.props;
|
|
381
404
|
const { timeState, isOpen, isInvalid, focusedIndex, minTimeState, maxTimeState } = this.state;
|
|
@@ -29,7 +29,7 @@ export const makeTimeOptions = (
|
|
|
29
29
|
.getMinutes()
|
|
30
30
|
.toString()
|
|
31
31
|
.padStart(2, '0');
|
|
32
|
-
const timeOption = hour
|
|
32
|
+
const timeOption = `${hour}${delimiter}${minutes}${hour12 ? suffix : ''}`;
|
|
33
33
|
// time option is valid if within min/max constraints
|
|
34
34
|
if (isWithinMinMax(minTime, maxTime, timeOption, delimiter)) {
|
|
35
35
|
res.push(timeOption);
|
|
@@ -39,7 +39,13 @@ export const makeTimeOptions = (
|
|
|
39
39
|
return res;
|
|
40
40
|
};
|
|
41
41
|
|
|
42
|
-
export const parseTime = (
|
|
42
|
+
export const parseTime = (
|
|
43
|
+
time: string | Date,
|
|
44
|
+
timeRegex: RegExp,
|
|
45
|
+
delimiter: string,
|
|
46
|
+
is12Hour: boolean,
|
|
47
|
+
includeSeconds: boolean
|
|
48
|
+
): string => {
|
|
43
49
|
const date = new Date(time);
|
|
44
50
|
|
|
45
51
|
// if default time is a ISO 8601 formatted date string, we parse it to hh:mm(am/pm) format
|
|
@@ -48,30 +54,40 @@ export const parseTime = (time: string | Date, timeRegex: RegExp, delimiter: str
|
|
|
48
54
|
? `${date.getHours() > 12 ? date.getHours() - 12 : date.getHours()}`
|
|
49
55
|
: `${date.getHours()}`.padStart(2, '0');
|
|
50
56
|
const minutes = `${date.getMinutes()}`.padStart(2, '0');
|
|
57
|
+
const seconds = includeSeconds ? `${date.getSeconds()}`.padStart(2, '0') : '';
|
|
58
|
+
const secondsWithDelimiter = seconds ? `${delimiter}${seconds}` : '';
|
|
51
59
|
let ampm = '';
|
|
60
|
+
|
|
52
61
|
if (is12Hour && date.getHours() > 11) {
|
|
53
62
|
ampm = pmSuffix;
|
|
54
63
|
} else if (is12Hour) {
|
|
55
64
|
ampm = amSuffix;
|
|
56
65
|
}
|
|
57
|
-
|
|
66
|
+
|
|
67
|
+
return `${hours}${delimiter}${minutes}${secondsWithDelimiter}${ampm}`;
|
|
58
68
|
} else if (typeof time === 'string') {
|
|
59
69
|
time = time.trim();
|
|
60
|
-
if (
|
|
61
|
-
const [, hours, minutes, suffix = ''] = timeRegex.exec(time);
|
|
62
|
-
const
|
|
63
|
-
// Format AM/PM according to design
|
|
70
|
+
if (time !== '' && validateTime(time, timeRegex, delimiter, is12Hour)) {
|
|
71
|
+
const [, hours, minutes, seconds, suffix = ''] = timeRegex.exec(time);
|
|
72
|
+
const secondsWithDelimiter = includeSeconds ? `${delimiter}${seconds ?? '00'}` : '';
|
|
64
73
|
let ampm = '';
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
74
|
+
|
|
75
|
+
// Format AM/PM according to design
|
|
76
|
+
if (is12Hour) {
|
|
77
|
+
const uppercaseSuffix = suffix.toUpperCase();
|
|
78
|
+
|
|
79
|
+
if (uppercaseSuffix === amSuffix.toUpperCase().trim()) {
|
|
80
|
+
ampm = amSuffix;
|
|
81
|
+
} else if (uppercaseSuffix === pmSuffix.toUpperCase().trim()) {
|
|
82
|
+
ampm = pmSuffix;
|
|
83
|
+
} else {
|
|
84
|
+
// if this 12 hour time is missing am/pm but otherwise valid,
|
|
85
|
+
// append am/pm depending on time of day
|
|
86
|
+
ampm = new Date().getHours() > 11 ? pmSuffix : amSuffix;
|
|
87
|
+
}
|
|
73
88
|
}
|
|
74
|
-
|
|
89
|
+
|
|
90
|
+
return `${hours}${delimiter}${minutes}${secondsWithDelimiter}${ampm}`;
|
|
75
91
|
}
|
|
76
92
|
}
|
|
77
93
|
return time.toString();
|
|
@@ -95,10 +111,10 @@ export const validateTime = (time: string, timeRegex: RegExp, delimiter: string,
|
|
|
95
111
|
export const getHours = (time: string, timeRegex: RegExp) => {
|
|
96
112
|
const parts = time.match(timeRegex);
|
|
97
113
|
if (parts && parts.length) {
|
|
98
|
-
if (/pm/i.test(parts[
|
|
114
|
+
if (/pm/i.test(parts[4])) {
|
|
99
115
|
return parseInt(parts[1]) === 12 ? parseInt(parts[1]) : parseInt(parts[1]) + 12;
|
|
100
116
|
}
|
|
101
|
-
if (/am/i.test(parts[
|
|
117
|
+
if (/am/i.test(parts[4])) {
|
|
102
118
|
return parseInt(parts[1]) === 12 ? 0 : parseInt(parts[1]);
|
|
103
119
|
}
|
|
104
120
|
return parseInt(parts[1]);
|
|
@@ -111,33 +127,54 @@ export const getMinutes = (time: string, timeRegex: RegExp) => {
|
|
|
111
127
|
return parts && parts.length ? parseInt(parts[2]) : null;
|
|
112
128
|
};
|
|
113
129
|
|
|
114
|
-
export const
|
|
130
|
+
export const getSeconds = (time: string, timeRegex: RegExp) => {
|
|
131
|
+
const seconds = time.match(timeRegex)?.[3];
|
|
132
|
+
|
|
133
|
+
return seconds ? parseInt(seconds) : null;
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
export const isWithinMinMax = (
|
|
137
|
+
minTime: string,
|
|
138
|
+
maxTime: string,
|
|
139
|
+
time: string,
|
|
140
|
+
delimiter: string,
|
|
141
|
+
includeSeconds?: boolean
|
|
142
|
+
) => {
|
|
115
143
|
// do not throw error if empty string
|
|
116
144
|
if (time.trim() === '') {
|
|
117
145
|
return true;
|
|
118
146
|
}
|
|
119
|
-
const convertTo24Hour = (time: string): string => {
|
|
120
|
-
const timeReg = new RegExp(`^\\s*(\\d\\d?)${delimiter}([0-5]\\d)\\s*([AaPp][Mm])?\\s*$`);
|
|
121
|
-
const regMatches = timeReg.exec(time);
|
|
122
|
-
if (!regMatches || !regMatches.length) {
|
|
123
|
-
return;
|
|
124
|
-
}
|
|
125
|
-
let hours = regMatches[1].padStart(2, '0');
|
|
126
|
-
const minutes = regMatches[2];
|
|
127
|
-
const suffix = regMatches[3] || '';
|
|
128
|
-
if (suffix.toUpperCase() === 'PM' && hours !== '12') {
|
|
129
|
-
hours = `${parseInt(hours) + 12}`;
|
|
130
|
-
} else if (suffix.toUpperCase() === 'AM' && hours === '12') {
|
|
131
|
-
hours = '00';
|
|
132
|
-
}
|
|
133
|
-
return `${hours}${delimiter}${minutes}`;
|
|
134
|
-
};
|
|
135
147
|
|
|
136
148
|
// correctly format as 24hr times (12:30AM => 00:30, 1:15 => 01:15)
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
149
|
+
const min24HourTime = convertTo24Hour(minTime, delimiter, includeSeconds);
|
|
150
|
+
const selected24HourTime = convertTo24Hour(time, delimiter, includeSeconds);
|
|
151
|
+
const max24HourTime = convertTo24Hour(maxTime, delimiter, includeSeconds);
|
|
140
152
|
|
|
141
153
|
// simple string comparison for 24hr times
|
|
142
|
-
return
|
|
154
|
+
return min24HourTime <= selected24HourTime && selected24HourTime <= max24HourTime;
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
const convertTo24Hour = (time: string, delimiter: string, includeSeconds: boolean): string => {
|
|
158
|
+
const timeReg = new RegExp(`^\\s*(\\d\\d?)${delimiter}([0-5]\\d)${delimiter}?([0-5]\\d)?\\s*([AaPp][Mm])?\\s*$`);
|
|
159
|
+
const regMatches = timeReg.exec(time);
|
|
160
|
+
if (!regMatches || !regMatches.length) {
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
let hours = regMatches[1].padStart(2, '0');
|
|
164
|
+
const minutes = regMatches[2];
|
|
165
|
+
let seconds = regMatches[3] ? `${delimiter}${regMatches[3]}` : '';
|
|
166
|
+
|
|
167
|
+
// When seconds is empty and 'includeSeconds' is enabled, append 0 seconds.
|
|
168
|
+
if (!seconds && includeSeconds) {
|
|
169
|
+
seconds = `${delimiter}00`;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const suffix = regMatches[4] || '';
|
|
173
|
+
if (suffix.toUpperCase() === 'PM' && hours !== '12') {
|
|
174
|
+
hours = `${parseInt(hours) + 12}`;
|
|
175
|
+
} else if (suffix.toUpperCase() === 'AM' && hours === '12') {
|
|
176
|
+
hours = '00';
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
return `${hours}${delimiter}${minutes}${seconds}`;
|
|
143
180
|
};
|