@patternfly/react-core 5.0.0-alpha.51 → 5.0.0-alpha.52

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@patternfly/react-core",
3
- "version": "5.0.0-alpha.51",
3
+ "version": "5.0.0-alpha.52",
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",
@@ -70,5 +70,5 @@
70
70
  "react": "^17 || ^18",
71
71
  "react-dom": "^17 || ^18"
72
72
  },
73
- "gitHead": "1b1bde2cf964d501b24a499274099e1de0b60425"
73
+ "gitHead": "5eea8522248a6f8caa1d7cb27f5f5b95ca823b7b"
74
74
  }
@@ -373,9 +373,22 @@ export class TimePicker extends React.Component<TimePickerProps, TimePickerState
373
373
  onToggle = (isOpen: boolean) => {
374
374
  // on close, parse and validate input
375
375
  this.setState(prevState => {
376
- const { timeRegex, isInvalid } = prevState;
377
- const { delimiter, is24Hour, includeSeconds } = this.props;
378
- const time = parseTime(prevState.timeState, timeRegex, delimiter, !is24Hour, includeSeconds);
376
+ const { timeRegex, isInvalid, timeState } = prevState;
377
+ const { delimiter, is24Hour, includeSeconds, onChange } = this.props;
378
+ const time = parseTime(timeState, timeRegex, delimiter, !is24Hour, includeSeconds);
379
+
380
+ // Call onChange when Enter is pressed in input and timeoption does not exist in menu
381
+ if (onChange && !isOpen && time !== timeState) {
382
+ onChange(
383
+ null,
384
+ time,
385
+ getHours(time, timeRegex),
386
+ getMinutes(time, timeRegex),
387
+ getSeconds(time, timeRegex),
388
+ this.isValid(time)
389
+ );
390
+ }
391
+
379
392
  return {
380
393
  isTimeOptionsOpen: isOpen,
381
394
  timeState: time,
@@ -414,7 +427,6 @@ export class TimePicker extends React.Component<TimePickerProps, TimePickerState
414
427
  onInputChange = (newTime: string, event: React.FormEvent<HTMLInputElement>) => {
415
428
  const { onChange } = this.props;
416
429
  const { timeRegex } = this.state;
417
-
418
430
  if (onChange) {
419
431
  onChange(
420
432
  event,
@@ -86,6 +86,28 @@ describe('TimePicker', () => {
86
86
  expects: { hour: 23, minutes: 59, seconds: null }
87
87
  });
88
88
  });
89
+
90
+ // Disabling because this test does not work on CI
91
+ xtest('should call onChange when pressing Enter', async () => {
92
+ const onChange = jest.fn();
93
+ const user = userEvent.setup();
94
+
95
+ render(<TimePicker onChange={onChange} aria-label="time picker" />);
96
+
97
+ // Take into account timezones when tests are ran
98
+ const isPM = new Date().getHours() > 12;
99
+
100
+ await user.type(screen.getByLabelText('time picker'), `11:11`);
101
+ await user.keyboard('[Enter]');
102
+ expect(onChange).toHaveBeenLastCalledWith(
103
+ expect.any(Object),
104
+ `11:11 ${isPM ? 'PM' : 'AM'}`,
105
+ isPM ? 23 : 11,
106
+ 11,
107
+ null,
108
+ true
109
+ );
110
+ });
89
111
  });
90
112
 
91
113
  describe('test isInvalid', () => {
@@ -18,34 +18,35 @@ DateRangePicker = () => {
18
18
  const [from, setFrom] = React.useState();
19
19
  const [to, setTo] = React.useState();
20
20
 
21
- const toValidator = date => isValidDate(from) && date >= from ? '' : 'To date must be less than from date';
22
- const onFromChange = (_str, date) => {
21
+ const toValidator = (date) =>
22
+ isValidDate(from) && date >= from ? '' : 'The "to" date must be after the "from" date';
23
+
24
+ const onFromChange = (_event, _value, date) => {
23
25
  setFrom(new Date(date));
24
26
  if (isValidDate(date)) {
25
27
  date.setDate(date.getDate() + 1);
26
28
  setTo(yyyyMMddFormat(date));
27
- }
28
- else {
29
+ } else {
29
30
  setTo('');
30
31
  }
31
32
  };
32
33
 
34
+ const onToChange = (_event, _value, date) => {
35
+ if (isValidDate(date)) {
36
+ setTo(yyyyMMddFormat(date));
37
+ }
38
+ };
39
+
33
40
  return (
34
41
  <Split>
35
42
  <SplitItem>
36
- <DatePicker
37
- onChange={onFromChange}
38
- aria-label="Start date"
39
- placeholder="YYYY-MM-DD"
40
- />
41
- </SplitItem>
42
- <SplitItem style={{ padding: '6px 12px 0 12px' }}>
43
- to
43
+ <DatePicker onChange={onFromChange} aria-label="Start date" placeholder="YYYY-MM-DD" />
44
44
  </SplitItem>
45
+ <SplitItem style={{ padding: '6px 12px 0 12px' }}>to</SplitItem>
45
46
  <SplitItem>
46
47
  <DatePicker
47
48
  value={to}
48
- onChange={date => setTo(date)}
49
+ onChange={onToChange}
49
50
  isDisabled={!isValidDate(from)}
50
51
  rangeStart={from}
51
52
  validators={[toValidator]}
@@ -55,23 +56,34 @@ DateRangePicker = () => {
55
56
  </SplitItem>
56
57
  </Split>
57
58
  );
58
- }
59
+ };
59
60
  ```
60
61
 
61
62
  ### Date and time range picker
62
63
 
63
64
  ```js
64
- import { Flex, FlexItem, InputGroup, DatePicker, isValidDate, TimePicker, yyyyMMddFormat, updateDateTime } from '@patternfly/react-core';
65
+ import {
66
+ Flex,
67
+ FlexItem,
68
+ InputGroup,
69
+ DatePicker,
70
+ isValidDate,
71
+ TimePicker,
72
+ yyyyMMddFormat,
73
+ updateDateTime
74
+ } from '@patternfly/react-core';
65
75
 
66
76
  DateTimeRangePicker = () => {
67
77
  const [from, setFrom] = React.useState();
68
78
  const [to, setTo] = React.useState();
69
79
 
70
- const toValidator = date => {
71
- return isValidDate(from) && yyyyMMddFormat(date) >= yyyyMMddFormat(from) ? '' : 'To date must after from date';
80
+ const toValidator = (date) => {
81
+ return isValidDate(from) && yyyyMMddFormat(date) >= yyyyMMddFormat(from)
82
+ ? ''
83
+ : 'The "to" date must be after the "from" date';
72
84
  };
73
-
74
- const onFromDateChange = (inputDate, newFromDate) => {
85
+
86
+ const onFromDateChange = (_event, inputDate, newFromDate) => {
75
87
  if (isValidDate(from) && isValidDate(newFromDate) && inputDate === yyyyMMddFormat(newFromDate)) {
76
88
  newFromDate.setHours(from.getHours());
77
89
  newFromDate.setMinutes(from.getMinutes());
@@ -80,7 +92,7 @@ DateTimeRangePicker = () => {
80
92
  setFrom(new Date(newFromDate));
81
93
  }
82
94
  };
83
-
95
+
84
96
  const onFromTimeChange = (_event, time, hour, minute) => {
85
97
  if (isValidDate(from)) {
86
98
  const updatedFromDate = new Date(from);
@@ -90,16 +102,16 @@ DateTimeRangePicker = () => {
90
102
  }
91
103
  };
92
104
 
93
- const onToDateChange = (inputDate, newToDate) => {
105
+ const onToDateChange = (_event, inputDate, newToDate) => {
94
106
  if (isValidDate(to) && isValidDate(newToDate) && inputDate === yyyyMMddFormat(newToDate)) {
95
107
  newToDate.setHours(to.getHours());
96
108
  newToDate.setMinutes(to.getMinutes());
97
109
  }
98
- if (isValidDate(newToDate) && inputDate === yyyyMMddFormat(newToDate)){
110
+ if (isValidDate(newToDate) && inputDate === yyyyMMddFormat(newToDate)) {
99
111
  setTo(newToDate);
100
112
  }
101
113
  };
102
-
114
+
103
115
  const onToTimeChange = (_event, time, hour, minute) => {
104
116
  if (isValidDate(to)) {
105
117
  const updatedToDate = new Date(to);
@@ -110,24 +122,14 @@ DateTimeRangePicker = () => {
110
122
  };
111
123
 
112
124
  return (
113
- <Flex direction={{default: 'column', lg: 'row'}}>
125
+ <Flex direction={{ default: 'column', lg: 'row' }}>
114
126
  <FlexItem>
115
127
  <InputGroup>
116
- <DatePicker
117
- onChange={onFromDateChange}
118
- aria-label="Start date"
119
- placeholder="YYYY-MM-DD"
120
- />
121
- <TimePicker
122
- aria-label="Start time"
123
- style={{width: '150px'}}
124
- onChange={onFromTimeChange}
125
- />
128
+ <DatePicker onChange={onFromDateChange} aria-label="Start date" placeholder="YYYY-MM-DD" />
129
+ <TimePicker aria-label="Start time" style={{ width: '150px' }} onChange={onFromTimeChange} />
126
130
  </InputGroup>
127
131
  </FlexItem>
128
- <FlexItem>
129
- to
130
- </FlexItem>
132
+ <FlexItem>to</FlexItem>
131
133
  <FlexItem>
132
134
  <InputGroup>
133
135
  <DatePicker
@@ -139,19 +141,22 @@ DateTimeRangePicker = () => {
139
141
  aria-label="End date"
140
142
  placeholder="YYYY-MM-DD"
141
143
  />
142
- <TimePicker style={{width: '150px'}} onChange={onToTimeChange} isDisabled={!isValidDate(from)}/>
144
+ <TimePicker style={{ width: '150px' }} onChange={onToTimeChange} isDisabled={!isValidDate(from)} />
143
145
  </InputGroup>
144
146
  </FlexItem>
145
147
  </Flex>
146
148
  );
147
- }
149
+ };
148
150
  ```
149
151
 
150
-
151
152
  ### Date and time pickers in modal
153
+
152
154
  Modals trap focus and watch a few document level events. In order to place a date picker in a modal:
155
+
153
156
  - To avoid the modal's escape press event handler from overruling the date picker's escape press handlers, use the `DatePickerRef` to close the calendar when it is open and the escape key is pressed.
154
157
  - Append the calendar to the modal to keep it as close to the date picker in the DOM while maintaining correct layouts visually
155
158
  In order to place a time picker in the modal, its menu must be appended to the time picker's parent.
159
+
156
160
  ```ts file="./examples/DateTimePickerInModal.tsx"
161
+
157
162
  ```