@patternfly/react-core 4.189.2 → 4.190.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 +38 -0
- 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/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 +123 -89
- package/dist/umd/react-core.min.css +314 -296
- package/dist/umd/react-core.min.js +3 -3
- package/package.json +6 -6
- 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.190.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.41.0",
|
|
38
|
+
"@patternfly/react-styles": "^4.40.0",
|
|
39
|
+
"@patternfly/react-tokens": "^4.42.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": "e76b0322ba29c50bd1c4c86d4a24b2e032586e5a"
|
|
64
64
|
}
|
|
@@ -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
|
};
|
|
@@ -4,42 +4,42 @@ import { act } from 'react-dom/test-utils';
|
|
|
4
4
|
import { TimePicker } from '../TimePicker'
|
|
5
5
|
|
|
6
6
|
describe('test timepicker onChange method with valid values', () => {
|
|
7
|
-
const testOnChange = ({
|
|
7
|
+
const testOnChange = ({ inputProps, expects }) => {
|
|
8
8
|
const onChange = jest.fn();
|
|
9
9
|
|
|
10
10
|
const event: any = {
|
|
11
|
-
currentTarget: { value:
|
|
11
|
+
currentTarget: { value: inputProps.value }
|
|
12
12
|
};
|
|
13
|
-
const view = mount(<TimePicker onChange={onChange}
|
|
14
|
-
|
|
13
|
+
const view = mount(<TimePicker onChange={onChange} {...inputProps} aria-label="time picker" />);
|
|
14
|
+
|
|
15
15
|
act(() => {
|
|
16
16
|
view.find('input').prop('onChange')(event);
|
|
17
17
|
})
|
|
18
|
-
expect(onChange).toBeCalledWith(
|
|
18
|
+
expect(onChange).toBeCalledWith(inputProps.value, expects.hour, expects.minutes, expects.seconds, true);
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
test('should return the correct value using the AM/PM pattern - midnight', () => {
|
|
22
|
-
testOnChange({
|
|
22
|
+
testOnChange({ inputProps: { value: "12:00 AM", is24Hour: false }, expects: { hour: 0, minutes: 0, seconds: null }})
|
|
23
23
|
})
|
|
24
24
|
|
|
25
25
|
test('should return the correct value using the AM/PM pattern - midday', () => {
|
|
26
|
-
testOnChange({
|
|
26
|
+
testOnChange({ inputProps: { value: "12:35 PM", is24Hour: false }, expects: { hour: 12, minutes: 35, seconds: null }})
|
|
27
27
|
})
|
|
28
28
|
|
|
29
29
|
test('should return the correct value using the AM/PM pattern - last minute of the day', () => {
|
|
30
|
-
testOnChange({
|
|
30
|
+
testOnChange({ inputProps: { value: "11:59 PM", is24Hour: false }, expects: { hour: 23, minutes: 59, seconds: null }})
|
|
31
31
|
})
|
|
32
32
|
|
|
33
33
|
test('should return the correct value using the 24h pattern - midnight', () => {
|
|
34
|
-
testOnChange({
|
|
34
|
+
testOnChange({ inputProps: { value: "00:00:00", is24Hour: true, includeSeconds: true }, expects: { hour: 0, minutes: 0, seconds: 0 }})
|
|
35
35
|
})
|
|
36
36
|
|
|
37
37
|
test('should return the correct value using the 24h pattern - midday', () => {
|
|
38
|
-
testOnChange({
|
|
38
|
+
testOnChange({ inputProps: { value: "12:35", is24Hour: false }, expects: { hour: 12, minutes: 35, seconds: null }})
|
|
39
39
|
})
|
|
40
40
|
|
|
41
41
|
test('should return the correct value using the 24h pattern - last minute of the day', () => {
|
|
42
|
-
testOnChange({
|
|
42
|
+
testOnChange({ inputProps: { value: "23:59", is24Hour: true }, expects: { hour: 23, minutes: 59, seconds: null }})
|
|
43
43
|
})
|
|
44
44
|
})
|
|
45
45
|
|
|
@@ -90,3 +90,41 @@ describe('test isInvalid', () => {
|
|
|
90
90
|
expect(view.find('.pf-m-error')).toHaveLength(1);
|
|
91
91
|
})
|
|
92
92
|
})
|
|
93
|
+
|
|
94
|
+
describe('test includeSeconds', () => {
|
|
95
|
+
const baseProps = {
|
|
96
|
+
"aria-label": "time picker",
|
|
97
|
+
value: "12:00 PM",
|
|
98
|
+
includeSeconds: true
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
it('should be valid still with a max value of "11:59:59 PM" by default', () => {
|
|
102
|
+
const event: any = { currentTarget: { value: "11:59:59 PM" } };
|
|
103
|
+
const view = mount(<TimePicker {...baseProps} />);
|
|
104
|
+
|
|
105
|
+
act(() => view.find('input').prop('onChange')(event));
|
|
106
|
+
view.update();
|
|
107
|
+
|
|
108
|
+
expect(view.find('.pf-m-error')).toHaveLength(0);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it('should be valid still with a max value of "23:59:59" by default when using 24 hour time', () => {
|
|
112
|
+
const event: any = { currentTarget: { value: "23:59:59" } };
|
|
113
|
+
const view = mount(<TimePicker {...baseProps} value="20:00" is24Hour />);
|
|
114
|
+
|
|
115
|
+
act(() => view.find('input').prop('onChange')(event));
|
|
116
|
+
view.update();
|
|
117
|
+
|
|
118
|
+
expect(view.find('.pf-m-error')).toHaveLength(0);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it('should be invalid with a max value of "24:00:00" by default', () => {
|
|
122
|
+
const event: any = { currentTarget: { value: "24:00:00" } };
|
|
123
|
+
const view = mount(<TimePicker {...baseProps} value="20:00" is24Hour />);
|
|
124
|
+
|
|
125
|
+
act(() => view.find('input').prop('onChange')(event));
|
|
126
|
+
view.update();
|
|
127
|
+
|
|
128
|
+
expect(view.find('.pf-m-error')).toHaveLength(1);
|
|
129
|
+
});
|
|
130
|
+
});
|
|
@@ -15,13 +15,14 @@ import React from 'react';
|
|
|
15
15
|
import { TimePicker } from '@patternfly/react-core';
|
|
16
16
|
|
|
17
17
|
SimpleTimePicker = () => {
|
|
18
|
-
const onChange = (time, hour, minute, isValid) => {
|
|
18
|
+
const onChange = (time, hour, minute, seconds, isValid) => {
|
|
19
19
|
console.log("time", time);
|
|
20
20
|
console.log("hour", hour);
|
|
21
21
|
console.log("minute", minute);
|
|
22
|
+
console.log("seconds", seconds);
|
|
22
23
|
console.log("isValid", isValid);
|
|
23
24
|
};
|
|
24
|
-
|
|
25
|
+
|
|
25
26
|
return <TimePicker time="3:35 AM" onChange={onChange}/>;
|
|
26
27
|
}
|
|
27
28
|
```
|
|
@@ -52,3 +53,19 @@ import { TimePicker } from '@patternfly/react-core';
|
|
|
52
53
|
|
|
53
54
|
<TimePicker is24Hour minTime="9:30" maxTime="17:15" placeholder="14:00"/>
|
|
54
55
|
```
|
|
56
|
+
|
|
57
|
+
### With seconds
|
|
58
|
+
```js
|
|
59
|
+
import React from 'react';
|
|
60
|
+
import { TimePicker } from '@patternfly/react-core';
|
|
61
|
+
|
|
62
|
+
SimpleTimePicker = () => <TimePicker time="3:35:20 PM" includeSeconds />;
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### 24 hours with seconds
|
|
66
|
+
```js
|
|
67
|
+
import React from 'react';
|
|
68
|
+
import { TimePicker } from '@patternfly/react-core';
|
|
69
|
+
|
|
70
|
+
SimpleTimePicker = () => <TimePicker time="12:35:50" includeSeconds is24Hour />;
|
|
71
|
+
```
|
package/src/helpers/util.ts
CHANGED
|
@@ -39,24 +39,30 @@ export function debounce(this: any, func: (...args: any[]) => any, wait: number)
|
|
|
39
39
|
* @param {HTMLElement} container The container to check if the element is in view of.
|
|
40
40
|
* @param {HTMLElement} element The element to check if it is view
|
|
41
41
|
* @param {boolean} partial true if partial view is allowed
|
|
42
|
+
* @param {boolean} strict true if strict mode is set, never consider the container width and element width
|
|
42
43
|
*
|
|
43
44
|
* @returns { boolean } True if the component is in View.
|
|
44
45
|
*/
|
|
45
|
-
export function isElementInView(
|
|
46
|
+
export function isElementInView(
|
|
47
|
+
container: HTMLElement,
|
|
48
|
+
element: HTMLElement,
|
|
49
|
+
partial: boolean,
|
|
50
|
+
strict: boolean = false
|
|
51
|
+
): boolean {
|
|
46
52
|
if (!container || !element) {
|
|
47
53
|
return false;
|
|
48
54
|
}
|
|
49
55
|
const containerBounds = container.getBoundingClientRect();
|
|
50
56
|
const elementBounds = element.getBoundingClientRect();
|
|
51
|
-
const containerBoundsLeft = Math.
|
|
57
|
+
const containerBoundsLeft = Math.ceil(containerBounds.left);
|
|
52
58
|
const containerBoundsRight = Math.floor(containerBounds.right);
|
|
53
|
-
const elementBoundsLeft = Math.
|
|
59
|
+
const elementBoundsLeft = Math.ceil(elementBounds.left);
|
|
54
60
|
const elementBoundsRight = Math.floor(elementBounds.right);
|
|
55
61
|
|
|
56
62
|
// Check if in view
|
|
57
63
|
const isTotallyInView = elementBoundsLeft >= containerBoundsLeft && elementBoundsRight <= containerBoundsRight;
|
|
58
64
|
const isPartiallyInView =
|
|
59
|
-
partial &&
|
|
65
|
+
(partial || (!strict && containerBounds.width < elementBounds.width)) &&
|
|
60
66
|
((elementBoundsLeft < containerBoundsLeft && elementBoundsRight > containerBoundsLeft) ||
|
|
61
67
|
(elementBoundsRight > containerBoundsRight && elementBoundsLeft < containerBoundsRight));
|
|
62
68
|
|