@patternfly/react-core 4.276.11 → 4.276.12
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 +11 -0
- package/components/DatePicker/DatePicker.d.ts +11 -2
- package/components/DatePicker/DatePicker.d.ts.map +1 -1
- package/components/DatePicker/DatePicker.js +15 -9
- package/components/DatePicker/DatePicker.js.map +1 -1
- package/dist/esm/components/DatePicker/DatePicker.d.ts +11 -2
- package/dist/esm/components/DatePicker/DatePicker.d.ts.map +1 -1
- package/dist/esm/components/DatePicker/DatePicker.js +15 -9
- package/dist/esm/components/DatePicker/DatePicker.js.map +1 -1
- package/dist/js/components/DatePicker/DatePicker.d.ts +11 -2
- package/dist/js/components/DatePicker/DatePicker.d.ts.map +1 -1
- package/dist/js/components/DatePicker/DatePicker.js +15 -9
- package/dist/js/components/DatePicker/DatePicker.js.map +1 -1
- package/dist/umd/react-core.min.js +1 -1
- package/package.json +2 -2
- package/src/components/DatePicker/DatePicker.tsx +31 -9
- package/src/components/DatePicker/examples/DatePicker.md +12 -1
- package/src/components/DatePicker/examples/DatePickerRequired.tsx +6 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@patternfly/react-core",
|
|
3
|
-
"version": "4.276.
|
|
3
|
+
"version": "4.276.12",
|
|
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": "^16.8 || ^17 || ^18",
|
|
71
71
|
"react-dom": "^16.8 || ^17 || ^18"
|
|
72
72
|
},
|
|
73
|
-
"gitHead": "
|
|
73
|
+
"gitHead": "713e587e02f5484dde8049ee378dcd53501acc13"
|
|
74
74
|
}
|
|
@@ -11,6 +11,14 @@ import { useImperativeHandle } from 'react';
|
|
|
11
11
|
import { KeyTypes } from '../../helpers';
|
|
12
12
|
import { isValidDate } from '../../helpers/datetimeUtils';
|
|
13
13
|
|
|
14
|
+
/** Props that customize the requirement of a date */
|
|
15
|
+
export interface DatePickerRequiredObject {
|
|
16
|
+
/** Flag indicating the date is required. */
|
|
17
|
+
isRequired?: boolean;
|
|
18
|
+
/** Error message to display when the text input is empty and the isRequired prop is also passed in. */
|
|
19
|
+
emptyDateText?: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
14
22
|
/** The main date picker component. */
|
|
15
23
|
|
|
16
24
|
export interface DatePickerProps
|
|
@@ -31,7 +39,7 @@ export interface DatePickerProps
|
|
|
31
39
|
className?: string;
|
|
32
40
|
/** How to format the date in the text input. */
|
|
33
41
|
dateFormat?: (date: Date) => string;
|
|
34
|
-
/** How to
|
|
42
|
+
/** How to parse the date in the text input. */
|
|
35
43
|
dateParse?: (value: string) => Date;
|
|
36
44
|
/** Helper text to display alongside the date picker. */
|
|
37
45
|
helperText?: React.ReactNode;
|
|
@@ -39,7 +47,7 @@ export interface DatePickerProps
|
|
|
39
47
|
inputProps?: TextInputProps;
|
|
40
48
|
/** Flag indicating the date picker is disabled. */
|
|
41
49
|
isDisabled?: boolean;
|
|
42
|
-
/** Error message to display when the text input
|
|
50
|
+
/** Error message to display when the text input contains a non-empty value in an invalid format. */
|
|
43
51
|
invalidFormatText?: string;
|
|
44
52
|
/** Callback called every time the text input loses focus. */
|
|
45
53
|
onBlur?: (event: any, value: string, date?: Date) => void;
|
|
@@ -49,6 +57,8 @@ export interface DatePickerProps
|
|
|
49
57
|
placeholder?: string;
|
|
50
58
|
/** Props to pass to the popover that contains the calendar month component. */
|
|
51
59
|
popoverProps?: Partial<Omit<PopoverProps, 'appendTo'>>;
|
|
60
|
+
/** Options to customize the requirement of a date */
|
|
61
|
+
requiredDateOptions?: DatePickerRequiredObject;
|
|
52
62
|
/** Functions that returns an error message if a date is invalid. */
|
|
53
63
|
validators?: ((date: Date) => string)[];
|
|
54
64
|
/** Value of the text input. */
|
|
@@ -94,6 +104,7 @@ const DatePickerBase = (
|
|
|
94
104
|
onChange = (): any => undefined,
|
|
95
105
|
onBlur = (): any => undefined,
|
|
96
106
|
invalidFormatText = 'Invalid date',
|
|
107
|
+
requiredDateOptions,
|
|
97
108
|
helperText,
|
|
98
109
|
appendTo = 'parent',
|
|
99
110
|
popoverProps,
|
|
@@ -120,6 +131,7 @@ const DatePickerBase = (
|
|
|
120
131
|
const style = { '--pf-c-date-picker__input--c-form-control--width-chars': widthChars, ...styleProps };
|
|
121
132
|
const buttonRef = React.useRef<HTMLButtonElement>();
|
|
122
133
|
const datePickerWrapperRef = React.useRef<HTMLDivElement>();
|
|
134
|
+
const emptyDateText = requiredDateOptions?.emptyDateText || 'Date cannot be blank';
|
|
123
135
|
|
|
124
136
|
React.useEffect(() => {
|
|
125
137
|
setValue(valueProp);
|
|
@@ -151,17 +163,22 @@ const DatePickerBase = (
|
|
|
151
163
|
};
|
|
152
164
|
|
|
153
165
|
const onInputBlur = (event: any) => {
|
|
154
|
-
if (pristine) {
|
|
155
|
-
return;
|
|
156
|
-
}
|
|
157
166
|
const newValueDate = dateParse(value);
|
|
158
|
-
|
|
159
|
-
|
|
167
|
+
const dateIsValid = isValidDate(newValueDate);
|
|
168
|
+
const onBlurDateArg = dateIsValid ? new Date(newValueDate) : undefined;
|
|
169
|
+
onBlur(event, value, onBlurDateArg);
|
|
170
|
+
|
|
171
|
+
if (dateIsValid) {
|
|
160
172
|
setError(newValueDate);
|
|
161
|
-
}
|
|
162
|
-
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
if (!dateIsValid && !pristine) {
|
|
163
176
|
setErrorText(invalidFormatText);
|
|
164
177
|
}
|
|
178
|
+
|
|
179
|
+
if (!dateIsValid && pristine && requiredDateOptions?.isRequired) {
|
|
180
|
+
setErrorText(emptyDateText);
|
|
181
|
+
}
|
|
165
182
|
};
|
|
166
183
|
|
|
167
184
|
const onDateClick = (newValueDate: Date) => {
|
|
@@ -235,6 +252,10 @@ const DatePickerBase = (
|
|
|
235
252
|
return false;
|
|
236
253
|
}
|
|
237
254
|
setPopoverOpen(false);
|
|
255
|
+
// If datepicker is required and the popover is opened without the text input
|
|
256
|
+
// first receiving focus, we want to validate that the text input is not blank upon
|
|
257
|
+
// closing the popover
|
|
258
|
+
requiredDateOptions?.isRequired && !value && setErrorText(emptyDateText);
|
|
238
259
|
if (event.key === KeyTypes.Escape && popoverOpen) {
|
|
239
260
|
event.stopPropagation();
|
|
240
261
|
}
|
|
@@ -250,6 +271,7 @@ const DatePickerBase = (
|
|
|
250
271
|
<InputGroup>
|
|
251
272
|
<TextInput
|
|
252
273
|
isDisabled={isDisabled}
|
|
274
|
+
isRequired={requiredDateOptions?.isRequired}
|
|
253
275
|
aria-label={ariaLabel}
|
|
254
276
|
placeholder={placeholder}
|
|
255
277
|
validated={errorText.trim() ? 'error' : 'default'}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
id: Date picker
|
|
3
3
|
section: components
|
|
4
4
|
cssPrefix: pf-c-date-picker
|
|
5
|
-
propComponents: ['DatePicker', 'CalendarFormat', 'DatePickerRef']
|
|
5
|
+
propComponents: ['DatePicker', 'CalendarFormat', 'DatePickerRef', 'DatePickerRequiredObject']
|
|
6
6
|
beta: true
|
|
7
7
|
---
|
|
8
8
|
|
|
@@ -13,6 +13,17 @@ beta: true
|
|
|
13
13
|
```ts file="./DatePickerBasic.tsx"
|
|
14
14
|
```
|
|
15
15
|
|
|
16
|
+
### Required
|
|
17
|
+
|
|
18
|
+
To require users to select a date before continuing, use the `requiredDateOptions.isRequired` property.
|
|
19
|
+
|
|
20
|
+
A required date picker will be invalid when the text input is empty and either the text input loses focus or the date picker popover is closed.
|
|
21
|
+
|
|
22
|
+
The error message can be customized via the `requiredDateOptions.emptyDateText` property.
|
|
23
|
+
|
|
24
|
+
```ts file="./DatePickerRequired.tsx"
|
|
25
|
+
```
|
|
26
|
+
|
|
16
27
|
### American format
|
|
17
28
|
|
|
18
29
|
```ts file="./DatePickerAmerican.tsx"
|