@payfit/unity-components
Version:
86 lines (85 loc) • 3.9 kB
TypeScript
import { CalendarDate } from '@internationalized/date';
import { StandardSchemaV1 } from '@standard-schema/spec';
import { ForwardedRef, ReactNode } from 'react';
import { FieldPath, FieldValues } from 'react-hook-form';
import { Schema } from '../../hooks/use-form.types.js';
import { DatePickerProps } from '../date-picker/DatePicker.js';
import { LabelProps } from '../label/Label.js';
export interface DatePickerFieldProps extends Pick<LabelProps, 'isRequired' | 'requiredVariant'>, DatePickerProps {
/** The name of the field, which should match the form schema. */
name: string;
/** The label for the date picker field. */
label: string;
/** The default value of the date picker field. */
defaultValue?: CalendarDate;
/** Helper text to display below the date picker field. */
helperText?: ReactNode;
/** Feedback text to display below the date picker field. */
feedbackText?: ReactNode;
/** A contextual link to display below the date picker field. */
contextualLink?: ReactNode;
/** Whether the field is in an invalid state. */
isInvalid?: boolean;
/** Whether the field is in a loading state. */
isLoading?: boolean;
/** Whether the field is disabled. */
isDisabled?: boolean;
/** Whether the field is read-only. */
isReadOnly?: boolean;
/** The minimum selectable date. */
minValue?: CalendarDate;
/** The maximum selectable date. */
maxValue?: CalendarDate;
/** Function to determine if a date should be disabled. */
isDateUnavailable?: (date: CalendarDate) => boolean;
/** Handler called when the clear button is pressed. */
onClearButtonPress?: () => void;
/** Handler called when the calendar overlay opens or closes. */
onOpenChange?: (isOpen: boolean) => void;
/** Handler called when the field loses focus. */
onBlur?: () => void;
/** Handler called when the field gains focus. */
onFocus?: () => void;
}
export type TypedDatePickerFieldProps<TFieldValues extends FieldValues = FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>> = Omit<DatePickerFieldProps, 'name'> & {
name: TName;
};
type DatePickerFieldComponent = {
<TSchema extends Schema>(props: TypedDatePickerFieldProps<StandardSchemaV1.InferOutput<TSchema>> & {
ref?: ForwardedRef<HTMLDivElement>;
}): React.JSX.Element;
displayName?: string;
};
/**
* The `DatePickerField` component renders a full field that can accept date input, integrating with the `Form` component automatically.
* It provides a calendar overlay for date selection and supports direct text input for dates.
* @example
* ```tsx
* const schema = z.object({
* birthDate: z.custom<CalendarDate>(),
* startDate: z.custom<CalendarDate>()
* })
*
* function MyForm() {
* const { Form } = useUnityForm(schema)
*
* return (
* <Form action={handleSubmit}>
* <DatePickerField<typeof schema>
* name="birthDate" // Only "birthDate" | "startDate" allowed
* label="Birth Date"
* />
* </Form>
* )
* }
* ```
* @remarks
* [API & Docs](https://master--66fe6715241b661107117e47.chromatic.com/?path=/docs/forms-datepickerfield--docs) • [Design Guidelines](https://www.payfit.design/24f360409/p/719ac2-date-picker)
* @note The schema type parameter is needed to ensure type safety with the form's schema. If you omit it, the `name` prop will not be type-safe.
* @note
* This component requires `@internationalized/date` as a peer dependency. Make sure to install it in your project: `yarn add @internationalized/date`
* @deprecated React Hook Form components are deprecated. Use the TanStack Form version instead.
* @see Storybook docs: https://unity-components.payfit.io/?path=/docs/forms-introduction-to-unity-forms--docs
*/
declare const DatePickerField: DatePickerFieldComponent;
export { DatePickerField };