## Feature Examples

### Size
- description: Adjust the size using the `size` prop. It supports 3 sizes: - Use `large` when the date input needs emphasis. - For most common cases, use the default `medium`. - In more dense and compact layouts, use `small`.
- example:
```jsx
<StorybookComponents.Stack flexDirection="column">
  <DatePicker
    placeholderText="Large"
    size="large"
    width="240px"
    onChange={() => {}}
  />
  <DatePicker
    placeholderText="Medium"
    size="medium"
    width="240px"
    onChange={() => {}}
  />
  <DatePicker
    placeholderText="Small"
    size="small"
    width="240px"
    onChange={() => {}}
  />
</StorybookComponents.Stack>;
```

### Status
- description: Control the status using the `status` prop. There are 3 states: - `error` highlights invalid dates. - `warning` highlights dates that can't be validated or might impact the user negatively. - `loading` shows that the server is loading the selected value.
- example:
```jsx
<StorybookComponents.Stack flexDirection="column">
  <DatePicker
    value={new Date()}
    status="error"
    width="240px"
    onChange={() => {}}
  />
  <DatePicker
    value={new Date()}
    status="warning"
    width="240px"
    onChange={() => {}}
  />
  <DatePicker
    value={new Date()}
    status="loading"
    width="240px"
    onChange={() => {}}
  />
</StorybookComponents.Stack>;
```

### Status message
- description: Add text that explains the status or what action the user should take with the `statusMessage` prop. Showing the status message inline, directly below the date picker is preferred in all default cases. - To add an accessible inline message, wrap the component in a `<FormField/>` and add the `statusMessage`. - To add a status message in a tooltip that requires users to hover on the icon, use the `statusMessage` prop. View more inline status message examples in `<FormField/>`.
- example:
```jsx
<StorybookComponents.Stack flexDirection="column">
  <StorybookComponents.Stack flexDirection="column" gap="12px">
    <Text secondary>For all default cases:</Text>
    <FormField status="error" statusMessage="This is an error message.">
      <DatePicker
        placeholderText="See message below"
        width="240px"
        onChange={() => {}}
      />
    </FormField>
  </StorybookComponents.Stack>
  <StorybookComponents.Stack flexDirection="column" gap="12px">
    <Text secondary>For narrow layouts only:</Text>
    <DatePicker
      placeholderText="Hover on status icon"
      status="error"
      statusMessage="This is an error message."
      width="240px"
      onChange={() => {}}
    />
  </StorybookComponents.Stack>
</StorybookComponents.Stack>;
```

### Read-only and disabled
- description: Control the input interaction with these props: - Use `readOnly` to display dates that cannot be overwritten (but can still be copied). - Disable all input interactions with the `disabled` prop. Use it to highlight an unavailable selection.
- example:
```jsx
<StorybookComponents.Stack flexDirection="column">
  <DatePicker
    value={new Date()}
    placeholderText="Select a date"
    readOnly
    width="240px"
    onChange={() => {}}
  />
  <DatePicker
    placeholderText="Select date"
    disabled
    width="240px"
    onChange={() => {}}
  />
</StorybookComponents.Stack>;
```

### Clear button
- description: Use the `clearButton` prop to add a button that clears the selected date easily.
- example:
```jsx
() => {
  const [value, setValue] = React.useState(new Date());

  return (
    <DatePicker
      placeholderText="Select date"
      value={value}
      clearButton
      onClear={() => setValue(null)}
      onChange={setValue}
      width="240px"
    />
  );
};
```

### Locale and date style
- description: Set appropriate formatting per country with `locale` and `dateStyle` props. `dateStyle` supports 2 values: - In most cases, the default style should be `short.` - Give the month more emphasis with the `medium` prop. - Show day and month in more detail with the `long` prop.
- example:
```jsx
<StorybookComponents.Stack flexDirection="column">
  <DatePicker
    value={new Date()}
    locale="en"
    dateStyle="short"
    width="240px"
    onChange={() => {}}
  />
  <DatePicker
    value={new Date()}
    locale="en"
    dateStyle="medium"
    width="240px"
    onChange={() => {}}
  />
  <DatePicker
    value={new Date()}
    locale="en"
    dateStyle="long"
    width="240px"
    onChange={() => {}}
  />
</StorybookComponents.Stack>;
```

### First day of the week
- description: Use `firstDayOfWeek` to adjust which day of the week is shown first in the calendar view. Overwrite the default value for Sunday (0) with a number representing any other day of the week (Monday = 1, Tuesday = 2, etc.).
- example:
```jsx
<StorybookComponents.Stack flexDirection="column">
  <DatePicker
    placeholderText="First day is Sunday"
    firstDayOfWeek={0}
    width="240px"
    onChange={() => {}}
  />
  <DatePicker
    placeholderText="First day is Monday"
    firstDayOfWeek={1}
    width="240px"
    onChange={() => {}}
  />
</StorybookComponents.Stack>;
```

### Year and month selections
- description: Make it easier for users to select dates far in the past or future with the `showMonthDropdown` and `showYearDropdown`. These dropdowns will show inside the calendar view.
- example:
```jsx
<DatePicker
  value={new Date()}
  showMonthDropdown
  showYearDropdown
  width="240px"
  onChange={() => {}}
/>;
```

### Excluding dates
- description: Control the date ranges that users can select with the following props: - Stop users from selecting dates in the past with `excludePastDates`. - Don't allow users to select a specific set or range of dates with `filterDate` .
- example:
```jsx
<StorybookComponents.Stack flexDirection="column">
  <DatePicker
    placeholderText="Select a date in the future"
    excludePastDates
    width="50%"
    onChange={() => {}}
  />
  <DatePicker
    placeholderText="Select one of the available dates"
    filterDate={(date) => date < new Date()}
    width="50%"
    onChange={() => {}}
  />
</StorybookComponents.Stack>;
```

## Developer Examples

### Built-in date validation
- description: The component's built-in validation feature covers these cases: - `formatError` - when a user types in a date that is invalid and impossible to parse. - `outOfBoundsError` - when a user types in a date that is out of the accepted range. This validation is triggered when the `excludePastDates` or `filterDate` props are enabled. Get informed about a validation event by using the `onValidate` callback function. It provides these parameters: `validationType`, input `value` and `format`.
- example:
```jsx
() => {
  const [date, setDate] = React.useState(new Date());
  const [error, setError] = React.useState('');

  const handleValidation = ({ validationType, value, format }) => {
    if (validationType === 'formatError') {
      setError(`Dates must be in this format: ${format}`);
    } else if (validationType === 'outOfBoundsError') {
      setError('Enter a date that is in the future');
    } else if (validationType === 'valid') {
      setError('');
    }
  };

  return (
    <FormField
      status={error ? 'error' : undefined}
      statusMessage={
        error
          ? error
          : 'Try entering invalid date (e.g. 88/10/2023) or a past date (e.g. 08/10/2000)'
      }
    >
      <DatePicker
        value={date}
        onChange={setDate}
        placeholderText="MM/DD/YYYY"
        onValidate={handleValidation}
        width="240px"
        excludePastDates
      />
    </FormField>
  );
};
```

### Custom date validations
- description: To apply custom validations to the component, use `onChange`, `value` and `status` together with the `statusMessage` prop.
- example:
```jsx
() => {
  const [date, setDate] = React.useState(new Date());
  const [error, setError] = React.useState('');

  const handleValidation = ({ value }) => {
    if (new Date(value) <= new Date()) {
      setError('Enter a date that is in the future');
    } else {
      setError('');
    }
  };

  return (
    <FormField
      status={error ? 'error' : undefined}
      statusMessage={
        error ? error : 'Try entering a past date (e.g. 08/10/2000)'
      }
    >
      <DatePicker
        value={date}
        onChange={setDate}
        placeholderText="MM/DD/YYYY"
        onValidate={handleValidation}
        width="240px"
        excludePastDates
      />
    </FormField>
  );
};
```

### Masked DatePicker using use-mask-input
- description: _Requires use of external package. In this example `use-mask-input` package is used._
- example:
```jsx
() => {
  const { withMask } = require('use-mask-input');
  const maskRef = React.useMemo(
    () =>
      withMask('99/99/9999', {
        placeholder: '_',
        showMaskOnHover: false,
      }),
    [],
  );
  return <DatePicker inputRef={maskRef} onChange={() => {}} />;
};
```

## Common Use Case Examples

### Settings panel
- description: Use the date picker to control the design of calendar components on the user's site.
- example:
```jsx
() => {
  const [selectedOption, setSelectedOption] = React.useState(0);
  const options = [
    { id: 0, value: 'Monday' },
    { id: 1, value: 'Sunday' },
  ];
  return (
    <SidePanel
      maxHeight="576px"
      width="288px"
      skin="floating"
      onCloseButtonClick={() => {}}
    >
      <SidePanel.Header title="Calendar Settings" />
      <SidePanel.Content noPadding>
        <SidePanel.Field>
          <FormField label="Week starts on">
            <Dropdown
              value=""
              size="small"
              options={options}
              onSelect={(option) => setSelectedOption(Number(option.id))}
              selectedId={selectedOption}
            />
          </FormField>
        </SidePanel.Field>
        <SidePanel.Field>
          <FieldSet
            legend="Set active days"
            legendSize="small"
            legendPlacement="top"
            direction="vertical"
            gap="small"
          >
            <Checkbox size="small" checked>
              Monday
            </Checkbox>
            <Checkbox size="small" checked>
              Tuesday
            </Checkbox>
            <Checkbox size="small" checked>
              Wednesday
            </Checkbox>
            <Checkbox size="small" checked>
              Thursday
            </Checkbox>
            <Checkbox size="small" checked>
              Friday
            </Checkbox>
            <Checkbox size="small">Saturday</Checkbox>
            <Checkbox size="small">Sunday</Checkbox>
          </FieldSet>
        </SidePanel.Field>
        <SidePanel.Section title="Available dates">
          <SidePanel.Field>
            <FieldSet
              legend="Available dates"
              alignment="end"
              direction="horizontal"
              legendPlacement="none"
            >
              <FormField label="From">
                <DatePicker
                  size="small"
                  width="auto"
                  placeholderText="Start"
                  popoverProps={{ appendTo: 'window' }}
                  onChange={() => {}}
                />
              </FormField>
              <FormField label="To">
                <DatePicker
                  size="small"
                  width="auto"
                  placeholderText="End"
                  popoverProps={{ appendTo: 'window' }}
                  onChange={() => {}}
                />
              </FormField>
              <Tooltip content="Delete">
                <IconButton size="small" priority="secondary">
                  <Icons.DeleteSmall />
                </IconButton>
              </Tooltip>
            </FieldSet>
          </SidePanel.Field>
          <SidePanel.Field>
            <TextButton size="small" prefixIcon={<Icons.AddSmall />}>
              Add Available Dates
            </TextButton>
          </SidePanel.Field>
        </SidePanel.Section>
        <SidePanel.Section title="Unavailable dates">
          <SidePanel.Field>
            <FieldSet
              legend="Unavailable dates"
              alignment="end"
              direction="horizontal"
              legendPlacement="none"
            >
              <FormField label="From">
                <DatePicker
                  size="small"
                  width="auto"
                  placeholderText="Start"
                  popoverProps={{ appendTo: 'window' }}
                  onChange={() => {}}
                />
              </FormField>
              <FormField label="To">
                <DatePicker
                  size="small"
                  width="auto"
                  placeholderText="End"
                  popoverProps={{ appendTo: 'window' }}
                  onChange={() => {}}
                />
              </FormField>
              <Tooltip content="Delete">
                <IconButton size="small" priority="secondary">
                  <Icons.DeleteSmall />
                </IconButton>
              </Tooltip>
            </FieldSet>
          </SidePanel.Field>
          <SidePanel.Field>
            <TextButton size="small" prefixIcon={<Icons.AddSmall />}>
              Add Unavailable Dates
            </TextButton>
          </SidePanel.Field>
        </SidePanel.Section>
      </SidePanel.Content>
    </SidePanel>
  );
};
```

### Together with time input
- description: Use the date picker together with `<TimeInput/>` for scheduling appointments, events and more.
- example:
```jsx
() => {
  const [startDate, setStartDate] = React.useState(new Date());
  const [endDate, setEndDate] = React.useState(new Date());

  return (
    <Layout cols={12}>
      <Cell span={6}>
        <FormField label="Start date" statusMessage="mm/dd/yyyy">
          <Box gap={2}>
            <DatePicker
              value={startDate}
              onChange={setStartDate}
              placeholderText="Select date"
              width="80%"
            />
            <TimeInput />
          </Box>
        </FormField>
      </Cell>
      <Cell span={6}>
        <FormField label="End date" statusMessage="mm/dd/yyyy">
          <Box gap={2}>
            <DatePicker
              value={endDate}
              onChange={setEndDate}
              placeholderText="Select date"
              width="80%"
            />
            <TimeInput />
          </Box>
        </FormField>
      </Cell>
    </Layout>
  );
};
```

## Accessibility Examples

### Helper text
- description: To help users enter the correct date format, use helper text below the date picker which describes the expected date format. This will help prevent user errors.
- example:
```jsx
<FormField label="Label" statusMessage="mm/dd/yyyy">
  <DatePicker
    placeholderText="Date"
    size="medium"
    width="240px"
    onChange={() => {}}
  />
</FormField>;
```
