Skip to content

Commit 411219e

Browse files
committed
feat(ui-date-input): add feature to disable dates and access the input's ref
1 parent ef77281 commit 411219e

File tree

4 files changed

+55
-4
lines changed

4 files changed

+55
-4
lines changed

packages/ui-date-input/src/DateInput/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
describes: DateInput
33
---
44

5-
> _Note:_ you can now try the updated (but still experimental) [`DateInput2`](/#DateInput2) which is easier to configure for developers, has a better UX, better accessibility features and a year picker. We recommend using that instead of `DateInput` which will be deprecated in the future.
5+
> _Note:_ we recommend to update to the new [`DateInput2`](/#DateInput2) which is easier to configure for developers, has a better UX, better accessibility features and a year picker. `DateInput` will be deprecated in the future.
66
77
The `DateInput` component provides a visual interface for inputting date data.
88

packages/ui-date-input/src/DateInput2/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,3 +305,38 @@ render(<Example />)
305305
### Date format hint
306306

307307
If the `placeholder` property is undefined it will display a hint for the date format (like `DD/MM/YYYY`). Usually it is recommended to leave it as it is for a better user experience.
308+
309+
### Disabling dates
310+
311+
You can use the `disabledDates` prop to disable specific dates. It accepts either an array of ISO8601 date strings or a function. Keep in mind that this will only disable the dates in the calendar and does not prevent the user the enter them into the input field. To validate those values please use the `onRequestValidateDate` prop.
312+
313+
```js
314+
---
315+
type: example
316+
---
317+
const Example = () => {
318+
const [inputValue, setInputValue] = useState('2/5/2025')
319+
const [dateString, setDateString] = useState('')
320+
return (
321+
<DateInput2
322+
renderLabel="Choose a date"
323+
disabledDates={['2025-02-11', '2025-02-12', '2025-02-13']}
324+
screenReaderLabels={{
325+
calendarIcon: 'Calendar',
326+
nextMonthButton: 'Next month',
327+
prevMonthButton: 'Previous month'
328+
}}
329+
value={inputValue}
330+
locale="en-us"
331+
width="20rem"
332+
onChange={(e, inputValue, dateString) => {
333+
setInputValue(inputValue)
334+
setDateString(dateString)
335+
}}
336+
invalidDateErrorMessage="Invalid date"
337+
/>
338+
)
339+
}
340+
341+
render(<Example />)
342+
```

packages/ui-date-input/src/DateInput2/index.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,6 @@ function parseLocaleDate(
131131
---
132132
category: components
133133
---
134-
135-
@module experimental
136134
**/
137135
const DateInput2 = ({
138136
renderLabel,
@@ -152,8 +150,10 @@ const DateInput2 = ({
152150
placeholder,
153151
dateFormat,
154152
onRequestValidateDate,
153+
disabledDates,
155154
renderCalendarIcon,
156155
margin,
156+
inputRef,
157157
...rest
158158
}: DateInput2Props) => {
159159
const localeContext = useContext(ApplyLocaleContext)
@@ -275,9 +275,11 @@ const DateInput2 = ({
275275
}
276276

277277
const selectedDate = parseDate(value)[1]
278+
278279
return (
279280
<TextInput
280281
{...passthroughProps(rest)}
282+
inputRef={inputRef}
281283
renderLabel={renderLabel}
282284
onChange={handleInputChange}
283285
onBlur={handleBlur}
@@ -318,6 +320,7 @@ const DateInput2 = ({
318320
withYearPicker={withYearPicker}
319321
onDateSelected={handleDateSelected}
320322
selectedDate={selectedDate}
323+
disabledDates={disabledDates}
321324
visibleMonth={selectedDate}
322325
locale={getLocale()}
323326
timezone={getTimezone()}

packages/ui-date-input/src/DateInput2/props.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,17 @@ type DateInput2OwnProps = {
175175
* Margin around the component. Accepts a `Spacing` token. See token values and example usage in [this guide](https://instructure.design/#layout-spacing).
176176
*/
177177
margin?: Spacing
178+
/*
179+
* Specify which date(s) will be shown as disabled in the calendar.
180+
* You can either supply an array of ISO8601 timeDate strings or
181+
* a function that will be called for each date shown in the calendar.
182+
*/
183+
disabledDates?: string[] | ((isoDateToCheck: string) => boolean)
184+
185+
/**
186+
* A function that provides a reference to the inner input element
187+
*/
188+
inputRef?: (inputElement: HTMLInputElement | null) => void
178189
}
179190

180191
type PropKeys = keyof DateInput2OwnProps
@@ -207,7 +218,9 @@ const propTypes: PropValidators<PropKeys> = {
207218
dateFormat: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
208219
onRequestValidateDate: PropTypes.func,
209220
renderCalendarIcon: PropTypes.oneOfType([PropTypes.node, PropTypes.func]),
210-
margin: PropTypes.string
221+
margin: PropTypes.string,
222+
disabledDates: PropTypes.oneOfType([PropTypes.array, PropTypes.func]),
223+
inputRef: PropTypes.func
211224
}
212225

213226
export type { DateInput2Props }

0 commit comments

Comments
 (0)