Skip to content

Commit fd6aaae

Browse files
committed
refactor: datepicker required
1 parent 8e85a18 commit fd6aaae

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
"@zag-js/date-picker": patch
3+
---
4+
5+
Add support for `required` and `invalid` props in date-picker
6+
7+
- Add `required` prop to mark the date picker as required for form validation
8+
- Add `invalid` prop to mark the date picker as invalid
9+
- Both props are now properly passed to the input element with appropriate ARIA attributes
10+
- Exposed `disabled` and `invalid` in the date picker API for better state access

packages/machines/date-picker/src/date-picker.connect.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,9 @@ export function connect<T extends PropTypes>(
6464
const hoveredValue = context.get("hoveredValue")
6565
const hoveredRangeValue = hoveredValue ? adjustStartAndEndDate([selectedValue[0], hoveredValue]) : []
6666

67-
const disabled = prop("disabled")
68-
const readOnly = prop("readOnly")
67+
const disabled = Boolean(prop("disabled"))
68+
const readOnly = Boolean(prop("readOnly"))
69+
const invalid = Boolean(prop("invalid"))
6970
const interactive = computed("isInteractive")
7071

7172
const min = prop("min")
@@ -243,6 +244,8 @@ export function connect<T extends PropTypes>(
243244
return {
244245
focused,
245246
open,
247+
disabled,
248+
invalid,
246249
inline: !!prop("inline"),
247250
view: context.get("view"),
248251
getRangePresetValue(preset) {
@@ -790,6 +793,9 @@ export function connect<T extends PropTypes>(
790793
"data-state": open ? "open" : "closed",
791794
readOnly,
792795
disabled,
796+
required: prop("required"),
797+
"aria-invalid": ariaAttr(invalid),
798+
"data-invalid": dataAttr(invalid),
793799
placeholder: prop("placeholder") || getInputPlaceholder(locale),
794800
defaultValue: computed("valueAsString")[index],
795801
onBeforeInput(event) {

packages/machines/date-picker/src/date-picker.props.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export const props = createProps<DatePickerProps>()([
2222
"id",
2323
"ids",
2424
"inline",
25+
"invalid",
2526
"isDateUnavailable",
2627
"locale",
2728
"max",
@@ -36,6 +37,7 @@ export const props = createProps<DatePickerProps>()([
3637
"defaultOpen",
3738
"positioning",
3839
"readOnly",
40+
"required",
3941
"selectionMode",
4042
"startOfWeek",
4143
"timeZone",

packages/machines/date-picker/src/date-picker.types.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,14 @@ export interface DatePickerProps extends DirectionProperty, CommonProperties {
116116
* Whether the calendar is read-only.
117117
*/
118118
readOnly?: boolean | undefined
119+
/**
120+
* Whether the date picker is required
121+
*/
122+
required?: boolean | undefined
123+
/**
124+
* Whether the date picker is invalid
125+
*/
126+
invalid?: boolean | undefined
119127
/**
120128
* Whether day outside the visible range can be selected.
121129
* @default false
@@ -504,6 +512,14 @@ export interface DatePickerApi<T extends PropTypes = PropTypes> {
504512
* Whether the date picker is open
505513
*/
506514
open: boolean
515+
/**
516+
* Whether the date picker is disabled
517+
*/
518+
disabled: boolean
519+
/**
520+
* Whether the date picker is invalid
521+
*/
522+
invalid: boolean
507523
/**
508524
* Whether the date picker is rendered inline
509525
*/

0 commit comments

Comments
 (0)