Demos
Empty
<Field.DateOfBirth onChange={(value, additionalArgs) => { { const { day, month, year } = additionalArgs || {} console.log('onChange', value, { day, month, year, }) } }} onDayChange={(day) => console.log('onDayChange', day)} onMonthChange={(month) => console.log('onMonthChange', month)} onYearChange={(year) => console.log('onYearChange', year)} />
Label
<Field.DateOfBirth label="Label text" onChange={(value) => console.log('onChange', value)} />
Label and value
<Field.DateOfBirth label="Label text" value="2000-05-17" onChange={(value) => console.log('onChange', value)} />
With help
<Field.DateOfBirth label="Label text" value="2000-05-17" help={{ title: 'Help is available', content: 'The real point is that we all need help somewhere along life’s path whether we think we will or not. And, if you are the one giving and helping, just remember this: no matter what happens later, you will always be secure in the fact knowing that you have remained strong and true to assist those that need your help.', }} onChange={(value) => console.log('onChange', value)} />
Disabled
<Field.DateOfBirth value="2000-05-17" label="Label text" onChange={(value) => console.log('onChange', value)} disabled />
Error
<Field.DateOfBirth label="Label text" onChange={(value) => console.log('onChange', value)} error={new Error('This is what is wrong...')} />
Validation - Required
<Field.DateOfBirth label="Label text" onChange={(value) => console.log('onChange', value)} required validateInitially />
Extend validation with custom validation function
You can extend the existing validation (dateOfBirthValidator) with your own validation function.
const firstDigitIs1Validator = (value: string) => { if (value.substring(0, 4) !== '1990') { return new Error('Has to be born in the year 1990!') } } // Keep the default validator and add a custom year rule. const myValidator: DateOfBirthValidator = (value, { validators }) => { const { dateOfBirthValidator } = validators return [dateOfBirthValidator, firstDigitIs1Validator] } render( <Field.DateOfBirth required value="2000-05-17" // @ts-expect-error -- strictFunctionTypes onBlurValidator={myValidator} validateInitially /> )
Path usage
<Form.Handler onSubmit={console.log} data={{ dob: '2000-05-17', }} > <Form.Card> <Field.DateOfBirth path="/dob" /> <Value.DateOfBirth path="/dob" showEmpty /> <Tools.Log /> </Form.Card> <Form.SubmitButton /> </Form.Handler>
Transform in and out
You can use transformIn and transformOut to transform data between external and internal formats.
const transformOut = (internal, additionalArgs) => { if (additionalArgs) { const { year, month, day } = additionalArgs return { year, month, day, } } } const transformIn = (external) => { if (external) { const { year, month, day } = external return `${year}-${month}-${day}` } } render( <Form.Handler defaultData={{ myField: { year: '1990', month: '05', day: '15', }, }} > <Form.Card> <Field.DateOfBirth path="/myField" transformOut={transformOut} transformIn={transformIn} label="Transform in and out" /> <Tools.Log /> </Form.Card> </Form.Handler> )