Skip to content

Commit

Permalink
fix!(DatePicker): add renderInputElement prop (#745)
Browse files Browse the repository at this point in the history
* fix!(DatePicker): change `renderInputElement` prop to be a function

This allows users to use datepicker hooks in their custom input component.
Currently, if `useDatepicker` hook is used in the custom input element,
react will throw (since the custom component is instantiated without a datepicker parent)

* feat: ensure backwards compatibility for DatePicker inputElement prop
  • Loading branch information
karrui authored Aug 20, 2024
1 parent 240c029 commit 945006f
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
4 changes: 2 additions & 2 deletions react/src/DatePicker/DatePicker.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ const CustomInputButton = forwardRef<object, 'button'>((_props, ref) => {

export const CustomInput = Template.bind({})
CustomInput.args = {
inputElement: <CustomInputButton />,
renderInputElement: () => <CustomInputButton />,
}

export const MobileCustomInput = Template.bind({})
MobileCustomInput.args = {
inputElement: <CustomInputButton />,
renderInputElement: () => <CustomInputButton />,
}
MobileCustomInput.parameters = getMobileViewParameters()

Expand Down
3 changes: 3 additions & 0 deletions react/src/DatePicker/DatePickerContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ interface DatePickerContextReturn {
colorScheme?: ThemingProps<'DatePicker'>['colorScheme']
size?: ThemingProps<'DatePicker'>['size']
disclosureProps: UseDisclosureReturn
renderInputElement?: () => React.ReactNode
inputElement?: React.ReactNode
innerRef?: React.Ref<HTMLElement>
calendarProps: Pick<
Expand Down Expand Up @@ -114,6 +115,7 @@ const useProvideDatePicker = ({
size,
experimental_forceIosNumberKeyboard,
inputElement,
renderInputElement,
innerRef,
...props
}: DatePickerProviderProps): DatePickerContextReturn => {
Expand Down Expand Up @@ -287,6 +289,7 @@ const useProvideDatePicker = ({
calendarProps,
inputPattern,
inputElement,
renderInputElement,
innerRef,
}
}
5 changes: 3 additions & 2 deletions react/src/DatePicker/components/DatePickerWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ export const DatePickerWrapper = ({ children }: PropsWithChildren) => {
initialFocusRef,
closeCalendarOnChange,
isMobile,
renderInputElement,
inputElement,
} = useDatePicker()

const inputToRender = useMemo(() => {
return inputElement ?? <DatePickerInput />
}, [inputElement])
return renderInputElement?.() ?? inputElement ?? <DatePickerInput />
}, [inputElement, renderInputElement])

if (isMobile) {
return (
Expand Down
9 changes: 9 additions & 0 deletions react/src/DatePicker/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,14 @@ export interface DatePickerBaseProps
refocusOnClose?: boolean
/** date-fns's Locale of the date to be applied if provided. */
locale?: Locale
/**
* @deprecated use `renderInputElement` instead. If `renderInputElement` is provided,
* that will take precedence over `inputElement`.
*/
inputElement?: React.ReactElement
/**
* If provided, the value will be used instead of the default `DatePickerInput` component.
* @returns
*/
renderInputElement?: () => React.ReactElement
}

0 comments on commit 945006f

Please sign in to comment.