Skip to content

Commit

Permalink
Split up EuiDatePicker and EuiDatePickerRange
Browse files Browse the repository at this point in the history
  • Loading branch information
cee-chen committed Nov 23, 2024
1 parent 7fd91ad commit 45f3d95
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 74 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
link:
type: doc
id: forms_date_picker
Original file line number Diff line number Diff line change
Expand Up @@ -224,49 +224,6 @@ export default () => {
};
```

## Date picker range

To create a single date range control, use **EuiDatePickerRange** and pass individual **EuiDatePicker** components into the `startDateControl` and `endDateControl` props. You can control the state of both inputs as direct props on **EuiDatePickerRange** as well as control each individually. Date specific props need to applied to the individual components.

```tsx interactive
import React, { useState } from 'react';
import { EuiDatePicker, EuiDatePickerRange } from '@elastic/eui';
import moment from 'moment';

export default () => {
const [startDate, setStartDate] = useState(moment());
const [endDate, setEndDate] = useState(moment().add(11, 'd'));

return (
/* DisplayToggles wrapper for Docs only */
<DisplayToggles canPrepend={true} canAppend={true}>
<EuiDatePickerRange
startDateControl={
<EuiDatePicker
selected={startDate}
onChange={(date) => date && setStartDate(date)}
startDate={startDate}
endDate={endDate}
aria-label="Start date"
showTimeSelect
/>
}
endDateControl={
<EuiDatePicker
selected={endDate}
onChange={(date) => date && setEndDate(date)}
startDate={startDate}
endDate={endDate}
aria-label="End date"
showTimeSelect
/>
}
/>
</DisplayToggles>
);
};
```

### Dynamic `minDate` and `maxDate`

By using `minDate` and `maxDate`, and updating the values based on `startDate` and `endDate`, users get immediate feedback on what range values are allowed.
Expand Down Expand Up @@ -512,15 +469,14 @@ export default () => {
};
```

## Date picker inline
## Inline display

Use the `inline` prop to display the date picker directly in the page instead of inside a popover. This prop works for both **EuiDatePicker** as well as **EuiDatePickerRange**. If you do not need the default inline shadow effect, apply the `shadow={false}` prop.
Use the `inline` prop to display the date picker directly in the page instead of inside a popover. If you do not need the default inline shadow effect, apply the `shadow={false}` prop.

```tsx interactive
import React, { useState } from 'react';
import {
EuiDatePicker,
EuiDatePickerRange,
EuiFlexGroup,
EuiSwitch,
EuiSpacer,
Expand Down Expand Up @@ -558,33 +514,6 @@ export default () => {
shadow={shadow}
/>
</DisplayToggles>
<EuiSpacer />
<DisplayToggles spacerSize="s" canCompressed={false} canFullWidth={false}>
<EuiDatePickerRange
inline
shadow={shadow}
startDateControl={
<EuiDatePicker
aria-label="Start date"
selected={startDate}
onChange={(date) => date && setStartDate(date)}
startDate={startDate}
endDate={endDate}
showTimeSelect={showTimeSelect}
/>
}
endDateControl={
<EuiDatePicker
aria-label="End date"
selected={endDate}
onChange={(date) => date && setEndDate(date)}
startDate={startDate}
endDate={endDate}
showTimeSelect={showTimeSelect}
/>
}
/>
</DisplayToggles>
</>
);
};
Expand Down Expand Up @@ -681,4 +610,3 @@ import { css } from '@emotion/css';
import docgen from '@elastic/eui-docgen/dist/components/date_picker';

<PropTable definition={docgen.EuiDatePicker} />
<PropTable definition={docgen.EuiDatePickerRange} />
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
---
slug: /forms/date-picker/range
id: forms_date_picker_range
---

# Date picker range

To create a single date range control, use **EuiDatePickerRange** and pass individual [**EuiDatePicker**](../../date-picker) components into the `startDateControl` and `endDateControl` props. You can control the state of both inputs as direct props on **EuiDatePickerRange** as well as control each individually. Date specific props need to applied to the individual components.

```tsx interactive
import React, { useState } from 'react';
import { EuiDatePicker, EuiDatePickerRange } from '@elastic/eui';
import moment from 'moment';

export default () => {
const [startDate, setStartDate] = useState(moment());
const [endDate, setEndDate] = useState(moment().add(11, 'd'));

return (
/* DisplayToggles wrapper for Docs only */
<DisplayToggles canPrepend={true} canAppend={true}>
<EuiDatePickerRange
startDateControl={
<EuiDatePicker
selected={startDate}
onChange={(date) => date && setStartDate(date)}
startDate={startDate}
endDate={endDate}
aria-label="Start date"
showTimeSelect
/>
}
endDateControl={
<EuiDatePicker
selected={endDate}
onChange={(date) => date && setEndDate(date)}
startDate={startDate}
endDate={endDate}
aria-label="End date"
showTimeSelect
/>
}
/>
</DisplayToggles>
);
};
```

:::tip
If you need even more functionality such as relative time, suggested date ranges, and refresh intervals, consider using [**EuiSuperDatePicker**](../../../templates/super-date-picker).
:::

## Inline display

Use the `inline` prop to display the date picker directly in the page instead of inside a popover. If you do not need the default inline shadow effect, apply the `shadow={false}` prop.

```tsx interactive
import React, { useState } from 'react';
import {
EuiDatePicker,
EuiDatePickerRange,
EuiFlexGroup,
EuiSwitch,
EuiSpacer,
} from '@elastic/eui';
import moment from 'moment';

export default () => {
const [shadow, setShadow] = useState(true);
const [showTimeSelect, setShowTimeSelect] = useState(false);

const [startDate, setStartDate] = useState(moment());
const [endDate, setEndDate] = useState(moment().add(11, 'd'));

return (
<>
<EuiFlexGroup>
<EuiSwitch
label="Show shadow"
checked={shadow}
onChange={() => setShadow((toggle) => !toggle)}
/>
<EuiSwitch
label="Show time select"
checked={showTimeSelect}
onChange={() => setShowTimeSelect((toggle) => !toggle)}
/>
</EuiFlexGroup>
<EuiSpacer />
<DisplayToggles spacerSize="s" canCompressed={false} canFullWidth={false}>
<EuiDatePickerRange
inline
shadow={shadow}
startDateControl={
<EuiDatePicker
aria-label="Start date"
selected={startDate}
onChange={(date) => date && setStartDate(date)}
startDate={startDate}
endDate={endDate}
showTimeSelect={showTimeSelect}
/>
}
endDateControl={
<EuiDatePicker
aria-label="End date"
selected={endDate}
onChange={(date) => date && setEndDate(date)}
startDate={startDate}
endDate={endDate}
showTimeSelect={showTimeSelect}
/>
}
/>
</DisplayToggles>
</>
);
};
```

## Props

import docgen from '@elastic/eui-docgen/dist/components/date_picker';

<PropTable definition={docgen.EuiDatePickerRange} />

0 comments on commit 45f3d95

Please sign in to comment.