Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🧑‍💻 Datepicker type: allow standard html attributes #3509

Merged
merged 5 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ export const DatePicker = forwardRef<HTMLDivElement, DatePickerProps>(
disabled: isDisabled,
readOnly: isReadOnly,
formatOptions,
helperProps,
variant,
...props
}: DatePickerProps,
forwardedRef: RefObject<HTMLDivElement>,
Expand Down Expand Up @@ -119,7 +121,8 @@ export const DatePicker = forwardRef<HTMLDivElement, DatePickerProps>(
const { locale } = useLocale()

const dateCreateProps = {
...props,
helperProps,
variant,
isDisabled,
value: _value,
hideTimeZone: true,
Expand All @@ -138,7 +141,7 @@ export const DatePicker = forwardRef<HTMLDivElement, DatePickerProps>(
const { groupProps, buttonProps, fieldProps, calendarProps } =
useDatePicker(dateCreateProps, pickerState, ref)

const helperProps = pickerState.displayValidation.isInvalid
const helperPropsInvalid = pickerState.displayValidation.isInvalid
? {
text: pickerState.displayValidation.validationErrors.join('\n'),
color: tokens.colors.interactive.warning__text.rgba,
Expand All @@ -154,6 +157,7 @@ export const DatePicker = forwardRef<HTMLDivElement, DatePickerProps>(
return (
<DatePickerProvider timezone={timezone} formatOptions={formatOptions}>
<FieldWrapper
{...props}
isOpen={isOpen}
readonly={fieldProps.isReadOnly}
pickerRef={pickerRef}
Expand All @@ -170,8 +174,8 @@ export const DatePicker = forwardRef<HTMLDivElement, DatePickerProps>(
}
disabled={isDisabled}
readOnly={isReadOnly}
color={pickerState.isInvalid ? 'warning' : props.variant}
helperProps={helperProps ?? props.helperProps}
color={pickerState.isInvalid ? 'warning' : variant}
helperProps={helperPropsInvalid ?? helperProps}
>
<DateField
fieldProps={fieldProps}
Expand All @@ -195,7 +199,7 @@ export const DatePicker = forwardRef<HTMLDivElement, DatePickerProps>(
})}
/>
}
variant={props.variant}
variant={variant}
/>
</FieldWrapper>
</DatePickerProvider>
Expand Down
155 changes: 78 additions & 77 deletions packages/eds-core-react/src/components/Datepicker/props.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MutableRefObject, ReactNode } from 'react'
import { MutableRefObject, ReactNode, HTMLAttributes } from 'react'
import { CalendarState, RangeCalendarState } from '@react-stately/calendar'
import { Variants } from '../types'
import { HelperTextProps } from '../InputWrapper/HelperText'
Expand All @@ -14,82 +14,83 @@ export type HeaderFooterProps<T = CalendarState | RangeCalendarState> = {
state: T
}

export type DatePickerProps = Partial<{
/**
* The variant / state of the datepicker field
* @default undefined
*/
variant: Variants
/**
* Whether the datepicker field is disabled
* @default false
*/
disabled?: boolean
// eslint-disable-next-line react/no-unused-prop-types
readOnly?: boolean
/**
* Props to set the helper text
*/
helperProps?: HelperTextProps
/**
* Controlled value
*/
value: Date | null
/**
* The function to call when the datepicker field value changes
*/
onChange: (date: Date | null) => void
/**
* The field label
*/
label: string
/**
* minValue sets a limit to how far back the datepicker allows you to go
* note: this does not prevent you from typing in a date that is before the minValue
* @default undefined
*/
minValue: Date
/**
* maxValue sets a limit to how far back the datepicker allows you to go
* note: this does not prevent you from typing in a date that is after the maxValue
* @default undefined
*/
maxValue: Date
/**
* isDateUnavailable is used to disable specific dates (e.g. weekends)
*/
isDateUnavailable?: (v: Date) => boolean
/**
* An optional footer to display below the calendar, useful for e.g. presets
*/
Footer: (props: HeaderFooterProps<CalendarState>) => ReactNode
/**
* An optional header to display above the calendar, can be used to override the default
* The component is fed with enough props to be able to control the calendar-view
*/
Header: (props: HeaderFooterProps<CalendarState>) => ReactNode
/**
* Whether to allow picking the time as well as the date
*/
showTimeInput?: boolean
/**
* Uncontrolled value
*/
defaultValue?: Date | null
/**
* Timezone to use for the datepicker
*/
timezone: string
/**
* Granularity of the time field if enabled
*/
granularity?: 'hour' | 'minute' | 'second'
/**
* Format options for the datepicker input field
* Only applies when input is blurred
*/
formatOptions?: DateFormatterOptions
}>
export type DatePickerProps = Omit<HTMLAttributes<HTMLDivElement>, 'onChange'> &
Partial<{
/**
* The variant / state of the datepicker field
* @default undefined
*/
variant: Variants
/**
* Whether the datepicker field is disabled
* @default false
*/
disabled?: boolean
// eslint-disable-next-line react/no-unused-prop-types
readOnly?: boolean
/**
* Props to set the helper text
*/
helperProps?: HelperTextProps
/**
* Controlled value
*/
value: Date | null
/**
* The function to call when the datepicker field value changes
*/
onChange: (date: Date | null) => void
/**
* The field label
*/
label: string
/**
* minValue sets a limit to how far back the datepicker allows you to go
* note: this does not prevent you from typing in a date that is before the minValue
* @default undefined
*/
minValue: Date
/**
* maxValue sets a limit to how far back the datepicker allows you to go
* note: this does not prevent you from typing in a date that is after the maxValue
* @default undefined
*/
maxValue: Date
/**
* isDateUnavailable is used to disable specific dates (e.g. weekends)
*/
isDateUnavailable?: (v: Date) => boolean
/**
* An optional footer to display below the calendar, useful for e.g. presets
*/
Footer: (props: HeaderFooterProps<CalendarState>) => ReactNode
/**
* An optional header to display above the calendar, can be used to override the default
* The component is fed with enough props to be able to control the calendar-view
*/
Header: (props: HeaderFooterProps<CalendarState>) => ReactNode
/**
* Whether to allow picking the time as well as the date
*/
showTimeInput?: boolean
/**
* Uncontrolled value
*/
defaultValue?: Date | null
/**
* Timezone to use for the datepicker
*/
timezone: string
/**
* Granularity of the time field if enabled
*/
granularity?: 'hour' | 'minute' | 'second'
/**
* Format options for the datepicker input field
* Only applies when input is blurred
*/
formatOptions?: DateFormatterOptions
}>

export type DateTimePickerProps = Omit<
DatePickerProps,
Expand Down