DateField Experimental
DateField is used when the user has to select a date. Compared to DatePicker, DateField has no supporting calendar to select a date, the user must input date values with a numeric keyboard.
DateField is distributed within the "gestalt-datepicker" package and must be installed separately.
Props
Localization
Be sure to localize all text strings. Note that localization can lengthen text by 20 to 30 percent.
Date format locales
DateField supports multiple locales. Adjust the date format to each date-fns locale.
The following locale examples show the different locale format variants.
Note that locale data from date-fns is external to gestalt-datepicker, it's not an internal dependency. Add date-fns to your app's dependencies.
import { DateField } from 'gestalt-datepicker';
import { it } from 'date-fns/locale';
<DateField localeData={it}/>
Use the SelectList to try out different locales by passing in the localeData
prop.
Variants
Controlled component
DateField is a controlled component. Use value
, onChange
, onClearInput
and onError
to implement it correctly.
import { useState } from 'react'; import { Box, Flex } from 'gestalt'; import { DateField } from 'gestalt-datepicker'; export default function Example() { const [dateValue, setDateValue] = useState(null); const [errorText, setErrorText] = useState(null); return ( <Flex alignItems="center" gap={4} height="100%" justifyContent="center" width="100%" > <Box width={400}> <DateField errorMessage={errorText || undefined} helperText="Enter your date of birth" id="mainExample" label="Date of birth" name="bday_datefield" onChange={({ value }) => { setDateValue(value); }} onClearInput={() => setDateValue(null)} onError={({ errorMessage }) => setErrorText(errorMessage)} value={dateValue} /> </Box> </Flex> ); }
Error messaging
DateField can communicate input errors to the user. Use onError
and errorMessage
to implement it correctly.
import { useState } from 'react'; import { Box, Flex } from 'gestalt'; import { DateField } from 'gestalt-datepicker'; export default function Example() { const [dateValue, setDateValue] = useState(null); const [errorText, setErrorText] = useState(null); return ( <Flex alignItems="center" gap={4} height="100%" justifyContent="center" width="100%" > <Box width={400}> <DateField disableRange="disableFuture" errorMessage={errorText || undefined} helperText="Enter your date of birth" id="errorExample" label="Date of birth" name="bday_datefield" onChange={({ value }) => { setDateValue(value); }} onClearInput={() => { setErrorText(null); setDateValue(null); }} onError={({ errorMessage, value }) => { const date = value ? new Date(value) : null; if (errorMessage === 'invalidDate') return; if ( errorMessage === 'disableFuture' || (date && date.getFullYear() === 1) ) setErrorText('Please, select a valid birth date'); if (date && date.getFullYear() > 1) setErrorText(null); }} value={dateValue} /> </Box> </Flex> ); }
States
DateField supports disabled
and readOnly
states.
import { Box, Flex } from 'gestalt'; import { DateField } from 'gestalt-datepicker'; export default function Example() { return ( <Flex alignItems="center" direction="column" gap={4} height="100%" justifyContent="center" width="100%" > <Box width={400}> <DateField disabled id="disabled_example" label="Date of birth" name="bday_datefield" onChange={() => {}} onClearInput={() => {}} onError={() => {}} value={new Date('1995-12-17T03:24:00')} /> </Box> <Box width={400}> <DateField id="readonly_example" label="Date of birth" name="bday_datefield" onChange={() => {}} onClearInput={() => {}} onError={() => {}} readOnly value={new Date('1995-12-17T03:24:00')} /> </Box> </Flex> ); }
Disable past & future dates
DateField supports disabling future & past dates from being selected.
import { useState } from 'react'; import { Box, Flex } from 'gestalt'; import { DateField } from 'gestalt-datepicker'; export default function Example() { const [dateValueDisableFuture, setDateValueDisableFuture] = useState(null); const [dateValueDisablePast, setDatealueDisablePast] = useState(null); const [errorTextDisableFuture, setErrorTextDisableFuture] = useState(null); const [errorTextDisablePast, setErrorTextDisablePast] = useState(null); return ( <Flex alignItems="center" direction="column" gap={4} height="100%" justifyContent="center" width="100%" > <Box width={400}> <DateField disableRange="disableFuture" errorMessage={ (errorTextDisableFuture && errorTextDisableFuture === 'disableFuture' && 'Please, enter a valid past date') || undefined } helperText="Enter your date of birth" id="disableFuture" label="Date of birth" name="bday_datefield" onChange={({ value }) => { setDateValueDisableFuture(value); }} onClearInput={() => setDateValueDisableFuture(null)} onError={({ errorMessage }) => setErrorTextDisableFuture(errorMessage) } value={dateValueDisableFuture} /> </Box> <Box width={400}> <DateField disableRange="disablePast" errorMessage={ (errorTextDisablePast && errorTextDisablePast === 'disablePast' && 'Please, enter a valid future date') || undefined } helperText="Enter an activation date for your campaign" id="disablePast" label="Campaign activation date" name="bday_datefield" onChange={({ value }) => { setDatealueDisablePast(value); }} onClearInput={() => setDatealueDisablePast(null)} onError={({ errorMessage }) => setErrorTextDisablePast(errorMessage)} value={dateValueDisablePast} /> </Box> </Flex> ); }
External handlers
DateField consumes external handlers from GlobalEventsHandlerProvider.
Handlers:
- onRender: executed when DateField mounts for the first time
See GlobalEventsHandlerProvider for more information.
Internal documentation
Related
DatePicker
Use DatePicker if the user is allowed to pick a date from a calendar popup.