@patternfly/react-core 4.189.5 → 4.191.2

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.
Files changed (36) hide show
  1. package/CHANGELOG.md +38 -0
  2. package/dist/esm/components/NumberInput/NumberInput.d.ts.map +1 -1
  3. package/dist/esm/components/NumberInput/NumberInput.js +13 -1
  4. package/dist/esm/components/NumberInput/NumberInput.js.map +1 -1
  5. package/dist/esm/components/TimePicker/TimePicker.d.ts +6 -4
  6. package/dist/esm/components/TimePicker/TimePicker.d.ts.map +1 -1
  7. package/dist/esm/components/TimePicker/TimePicker.js +37 -26
  8. package/dist/esm/components/TimePicker/TimePicker.js.map +1 -1
  9. package/dist/esm/components/TimePicker/TimePickerUtils.d.ts +3 -2
  10. package/dist/esm/components/TimePicker/TimePickerUtils.d.ts.map +1 -1
  11. package/dist/esm/components/TimePicker/TimePickerUtils.js +57 -42
  12. package/dist/esm/components/TimePicker/TimePickerUtils.js.map +1 -1
  13. package/dist/js/components/NumberInput/NumberInput.d.ts.map +1 -1
  14. package/dist/js/components/NumberInput/NumberInput.js +13 -1
  15. package/dist/js/components/NumberInput/NumberInput.js.map +1 -1
  16. package/dist/js/components/TimePicker/TimePicker.d.ts +6 -4
  17. package/dist/js/components/TimePicker/TimePicker.d.ts.map +1 -1
  18. package/dist/js/components/TimePicker/TimePicker.js +36 -25
  19. package/dist/js/components/TimePicker/TimePicker.js.map +1 -1
  20. package/dist/js/components/TimePicker/TimePickerUtils.d.ts +3 -2
  21. package/dist/js/components/TimePicker/TimePickerUtils.d.ts.map +1 -1
  22. package/dist/js/components/TimePicker/TimePickerUtils.js +59 -43
  23. package/dist/js/components/TimePicker/TimePickerUtils.js.map +1 -1
  24. package/dist/umd/react-core.js +105 -68
  25. package/dist/umd/react-core.min.js +1 -1
  26. package/package.json +5 -5
  27. package/src/components/NumberInput/NumberInput.tsx +17 -0
  28. package/src/components/NumberInput/__tests__/__snapshots__/NumberInput.test.tsx.snap +10 -0
  29. package/src/components/NumberInput/examples/NumberInput.md +43 -0
  30. package/src/components/TimePicker/TimePicker.tsx +54 -31
  31. package/src/components/TimePicker/TimePickerUtils.tsx +76 -39
  32. package/src/components/TimePicker/__tests__/TimePicker.test.tsx +49 -11
  33. package/src/components/TimePicker/examples/TimePicker.md +19 -2
  34. package/src/demos/Button.md +67 -0
  35. package/src/demos/Tabs.md +7 -0
  36. package/src/demos/examples/Tabs/ModalTabs.tsx +142 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@patternfly/react-core",
3
- "version": "4.189.5",
3
+ "version": "4.191.2",
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,9 +34,9 @@
34
34
  "generate": "node scripts/copyStyles.js"
35
35
  },
36
36
  "dependencies": {
37
- "@patternfly/react-icons": "^4.40.5",
38
- "@patternfly/react-styles": "^4.39.5",
39
- "@patternfly/react-tokens": "^4.41.5",
37
+ "@patternfly/react-icons": "^4.42.2",
38
+ "@patternfly/react-styles": "^4.41.2",
39
+ "@patternfly/react-tokens": "^4.43.2",
40
40
  "focus-trap": "6.2.2",
41
41
  "react-dropzone": "9.0.0",
42
42
  "tippy.js": "5.1.2",
@@ -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": "01c8b913108c70fcddb6ee6d7bdf523dffc620b7"
63
+ "gitHead": "346e73d12c36bf0c75c1208656efc85b4ceaac9a"
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
+ ```
@@ -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 an ISO 8601 formatted date string or in 'HH{delimiter}MM' format */
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 an ISO 8601 formatted date string or in 'HH{delimiter}MM' format */
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
- minTime = is24Hour ? `00${delimiter}00` : `12${delimiter}00AM`;
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
- maxTime = is24Hour ? `23${delimiter}59` : `11${delimiter}59PM`;
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.is24Hour
277
- ? new RegExp(`^\\s*(\\d\\d?)${this.props.delimiter}([0-5]\\d)\\s*$`)
278
- : new RegExp(`^\\s*(\\d\\d?)${this.props.delimiter}([0-5]\\d)\\s*([AaPp][Mm])?\\s*$`);
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
- const { delimiter, is24Hour } = this.props;
288
- return validateTime(time, this.state.timeRegex, delimiter, !is24Hour);
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
- return isWithinMinMax(minTimeState, maxTimeState, time, delimiter);
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
- if (this.props.onChange) {
334
- this.props.onChange(
349
+ const { onChange } = this.props;
350
+ const { timeRegex } = this.state;
351
+
352
+ if (onChange) {
353
+ onChange(
335
354
  newTime,
336
- getHours(newTime, this.state.timeRegex),
337
- getMinutes(newTime, this.state.timeRegex),
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 + delimiter + minutes + (hour12 ? suffix : '');
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 = (time: string | Date, timeRegex: RegExp, delimiter: string, is12Hour: boolean): string => {
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
- return `${hours}${delimiter}${minutes}${ampm}`;
66
+
67
+ return `${hours}${delimiter}${minutes}${secondsWithDelimiter}${ampm}`;
58
68
  } else if (typeof time === 'string') {
59
69
  time = time.trim();
60
- if (is12Hour && time !== '' && validateTime(time, timeRegex, delimiter, is12Hour)) {
61
- const [, hours, minutes, suffix = ''] = timeRegex.exec(time);
62
- const uppercaseSuffix = suffix.toUpperCase();
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
- if (uppercaseSuffix === amSuffix.toUpperCase().trim()) {
66
- ampm = amSuffix;
67
- } else if (uppercaseSuffix === pmSuffix.toUpperCase().trim()) {
68
- ampm = pmSuffix;
69
- } else {
70
- // if this 12 hour time is missing am/pm but otherwise valid,
71
- // append am/pm depending on time of day
72
- ampm = new Date().getHours() > 11 ? pmSuffix : amSuffix;
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
- return `${hours}${delimiter}${minutes}${ampm}`;
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[3])) {
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[3])) {
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 isWithinMinMax = (minTime: string, maxTime: string, time: string, delimiter: string) => {
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
- minTime = convertTo24Hour(minTime);
138
- time = convertTo24Hour(time);
139
- maxTime = convertTo24Hour(maxTime);
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 minTime <= time && time <= maxTime;
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 = ({ input, expects }) => {
7
+ const testOnChange = ({ inputProps, expects }) => {
8
8
  const onChange = jest.fn();
9
9
 
10
10
  const event: any = {
11
- currentTarget: { value: input.value }
11
+ currentTarget: { value: inputProps.value }
12
12
  };
13
- const view = mount(<TimePicker onChange={onChange} value={input.value} is24Hour={input.is24Hour} aria-label="time picker" />);
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(input.value, expects.hour, expects.minutes, true);
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({ input: { value: "12:00 AM", is24Hour: false }, expects: { hour: 0, minutes: 0 }})
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({ input: { value: "12:35 PM", is24Hour: false }, expects: { hour: 12, minutes: 35 }})
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({ input: { value: "11:59 PM", is24Hour: false }, expects: { hour: 23, minutes: 59 }})
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({ input: { value: "00:00", is24Hour: true }, expects: { hour: 0, minutes: 0 }})
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({ input: { value: "12:35", is24Hour: false }, expects: { hour: 12, minutes: 35 }})
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({ input: { value: "23:59", is24Hour: true }, expects: { hour: 23, minutes: 59 }})
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
+ });