Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[EuiDatePicker] Fix year dropdown when using minDate or maxDate #5069

Merged
merged 12 commits into from
Aug 26, 2021
54 changes: 54 additions & 0 deletions src-docs/src/views/date_picker/date_picker_example.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ import Range from './range';
const rangeSource = require('!!raw-loader!./range');
const rangeHtml = renderToHtml(Range);

import RangeRestricted from './range_restricted';
const rangeRestrictedSource = require('!!raw-loader!./range_restricted');

import MinMax from './min_max';
const minMaxSource = require('!!raw-loader!./min_max');
const minMaxHtml = renderToHtml(MinMax);
Expand Down Expand Up @@ -131,6 +134,35 @@ const rangeSnippet = `<EuiDatePickerRange
}
/>`;

const rangeRestrictedSnippet = `<EuiDatePickerRange
startDateControl={
<EuiDatePicker
selected={startDate}
onChange={setStartDate}
startDate={startDate}
endDate={endDate}
minDate={minDate}
maxDate={endDate}
isInvalid={isInvalid}
aria-label="Start date"
showTimeSelect
/>
}
endDateControl={
<EuiDatePicker
selected={endDate}
onChange={setEndDate}
startDate={startDate}
endDate={endDate}
minDate={startDate}
maxDate={maxDate}
isInvalid={isInvalid}
aria-label="End date"
showTimeSelect
/>
}
/>`;

const minMaxSnippet = [
`<EuiDatePicker
showTimeSelect
Expand Down Expand Up @@ -320,6 +352,28 @@ export const DatePickerExample = {
snippet: rangeSnippet,
props: { EuiDatePickerRange },
},
{
source: [
{
type: GuideSectionTypes.JS,
code: rangeRestrictedSource,
},
],
text: (
<p>
<h3>
Dynamic <EuiCode>minDate</EuiCode> and <EuiCode>maxDate</EuiCode>
</h3>
By using <EuiCode>minDate</EuiCode> and <EuiCode>maxDate</EuiCode>,
and updating the values based on <EuiCode>startDate</EuiCode> and{' '}
<EuiCode>endDate</EuiCode>, users get immediate feedback on what range
values are allowed.
</p>
),
demo: <RangeRestricted />,
snippet: rangeRestrictedSnippet,
thompsongl marked this conversation as resolved.
Show resolved Hide resolved
props: { EuiDatePickerRange },
},
{
title: 'Only allow specific dates and times',
source: [
Expand Down
46 changes: 46 additions & 0 deletions src-docs/src/views/date_picker/range_restricted.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import React, { useState } from 'react';

import moment from 'moment';

import { EuiDatePicker, EuiDatePickerRange } from '../../../../src/components';

export default () => {
const minDate = moment().subtract(2, 'y');
const maxDate = moment();
const [startDate, setStartDate] = useState(minDate);
const [endDate, setEndDate] = useState(maxDate);

const isInvalid =
startDate > endDate || startDate < minDate || endDate > maxDate;

return (
<EuiDatePickerRange
startDateControl={
<EuiDatePicker
selected={startDate}
onChange={setStartDate}
startDate={startDate}
endDate={endDate}
minDate={minDate}
maxDate={endDate}
isInvalid={isInvalid}
aria-label="Start date"
showTimeSelect
/>
}
endDateControl={
<EuiDatePicker
selected={endDate}
onChange={setEndDate}
startDate={startDate}
endDate={endDate}
minDate={startDate}
maxDate={maxDate}
isInvalid={isInvalid}
aria-label="End date"
showTimeSelect
/>
}
/>
);
};