Skip to content

Commit

Permalink
feat(DatePicker): add disableDates
Browse files Browse the repository at this point in the history
closes #3787
  • Loading branch information
gizeasy committed Oct 21, 2024
1 parent 9e52d2f commit 4909547
Show file tree
Hide file tree
Showing 14 changed files with 116 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
DateTimePropType,
MoveType,
} from '##/components/DateTime';
import { DateTimePropDisableDates } from '##/components/DateTime/helpers/types';
import { Direction, Popover } from '##/components/Popover';
import { useFlag } from '##/hooks/useFlag';
import { useForkRef } from '##/hooks/useForkRef';
Expand Down Expand Up @@ -49,6 +50,7 @@ export type DatePickerDropdownProps = PropsWithHTMLAttributesAndRef<
multiplicityMinutes?: number;
multiplicityHours?: number;
timeFor?: 'start' | 'end';
disableDates?: DateTimePropDisableDates;
},
HTMLDivElement
>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export const DatePickerTypeDate: DatePickerTypeComponent<'date'> = forwardRef(
dropdownOpen,
ignoreOutsideClicksRefs,
disabled,
disableDates,
inputRef: inputRefProp,
...otherProps
} = props;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export const DatePickerTypeDateRange: DatePickerTypeComponent<'date-range'> =
dropdownOpen,
ignoreOutsideClicksRefs,
disabled,
disableDates,
...fieldProps
} = props;

Expand Down Expand Up @@ -226,6 +227,7 @@ export const DatePickerTypeDateRange: DatePickerTypeComponent<'date-range'> =
onChange={hadleChange}
renderAdditionalControls={renderAdditionalControls}
zIndex={getDropdownZIndex(props.style)}
disableDates={disableDates}
/>
</>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export const DatePickerTypeDateTime: DatePickerTypeComponent<'date-time'> =
ignoreOutsideClicksRefs,
disabled,
inputRef: inputRefProp,
disableDates,
...otherProps
} = props;

Expand Down Expand Up @@ -141,6 +142,7 @@ export const DatePickerTypeDateTime: DatePickerTypeComponent<'date-time'> =
multiplicitySeconds={multiplicitySeconds}
multiplicityMinutes={multiplicityMinutes}
zIndex={getDropdownZIndex(props.style)}
disableDates={disableDates}
/>
</>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export const DatePickerTypeDateTimeRange: DatePickerTypeComponent<'date-time-ran
dropdownOpen,
ignoreOutsideClicksRefs,
disabled,
disableDates,
...fieldProps
} = props;

Expand Down Expand Up @@ -210,6 +211,7 @@ export const DatePickerTypeDateTimeRange: DatePickerTypeComponent<'date-time-ran
multiplicitySeconds={multiplicitySeconds}
multiplicityHours={multiplicityHours}
zIndex={getDropdownZIndex(props.style)}
disableDates={disableDates}
/>
</>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export const DatePickerTypeMonth: DatePickerTypeComponent<'month'> = forwardRef(
ignoreOutsideClicksRefs,
disabled,
inputRef: inputRefProp,
disableDates,
...otherProps
} = props;

Expand Down Expand Up @@ -122,6 +123,7 @@ export const DatePickerTypeMonth: DatePickerTypeComponent<'month'> = forwardRef(
renderAdditionalControls={renderAdditionalControls}
onChangeCurrentVisibleDate={setCurrentVisibleDate}
zIndex={getDropdownZIndex(props.style)}
disableDates={disableDates}
/>
</>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export const DatePickerTypeMonthRange: DatePickerTypeComponent<'month-range'> =
dropdownOpen,
ignoreOutsideClicksRefs,
disabled,
disableDates,
...fieldProps
} = props;

Expand Down Expand Up @@ -222,6 +223,7 @@ export const DatePickerTypeMonthRange: DatePickerTypeComponent<'month-range'> =
onChange={hadleChange}
renderAdditionalControls={renderAdditionalControls}
zIndex={getDropdownZIndex(props.style)}
disableDates={disableDates}
/>
</>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export const DatePickerTypeTime: DatePickerTypeComponent<'time'> = forwardRef(
ignoreOutsideClicksRefs,
disabled,
inputRef: inputRefProp,
disableDates,
...otherProps
} = props;

Expand Down Expand Up @@ -106,6 +107,7 @@ export const DatePickerTypeTime: DatePickerTypeComponent<'time'> = forwardRef(
multiplicitySeconds={multiplicitySeconds}
multiplicityMinutes={multiplicityMinutes}
zIndex={getDropdownZIndex(props.style)}
disableDates={disableDates}
/>
</>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export const DatePickerTypeYear: DatePickerTypeComponent<'year'> = forwardRef(
disabled,
ignoreOutsideClicksRefs,
inputRef: inputRefProp,
disableDates,
...otherProps
} = props;

Expand Down Expand Up @@ -122,6 +123,7 @@ export const DatePickerTypeYear: DatePickerTypeComponent<'year'> = forwardRef(
renderAdditionalControls={renderAdditionalControls}
zIndex={getDropdownZIndex(props.style)}
onChangeCurrentVisibleDate={setCurrentVisibleDate}
disableDates={disableDates}
/>
</>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export const DatePickerTypeYearRange: DatePickerTypeComponent<'date-range'> =
dropdownOpen,
ignoreOutsideClicksRefs,
disabled,
disableDates,
...fieldProps
} = props;

Expand Down Expand Up @@ -215,6 +216,7 @@ export const DatePickerTypeYearRange: DatePickerTypeComponent<'date-range'> =
onChange={hadleChange}
renderAdditionalControls={renderAdditionalControls}
zIndex={getDropdownZIndex(props.style)}
disableDates={disableDates}
/>
</>
);
Expand Down
45 changes: 45 additions & 0 deletions src/components/DatePicker/__stand__/DatePicker.dev.stand.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
import { DatePickerExampleMulti } from './examples/DatePickerExampleMulti/DatePickerExampleMulti';
import { DatePickerExampleDropdownOpen } from './examples/DatePickerExampleDropdownOpen/DatePickerExampleDropdownOpen';
import { DatePickerExampleTwoIcons } from './examples/DatePickerExampleTwoFields/DatePickerExampleTwoFields';
import { DatePickerExampleDisableDates } from './examples/DatePickerExampleDisableDates/DatePickerExampleDisableDates';
import { DatePickerExampleWithClearButton } from './examples/DatePickerExampleWithClearButton/DatePickerExampleWithClearButton';

```tsx
Expand Down Expand Up @@ -388,6 +389,50 @@ export const DatePickerExampleAdditionalControls = () => {

<DatePickerExampleAdditionalControls />

## Отключение интервалов

Имеется возможность отключить интервалы или конкретные даты для выбора пользователем.

```tsx
import React, { useState } from 'react';
import { DatePicker } from '@consta/uikit/DatePicker';

const getDisableDates = (): Array<Date | [Date, Date]> => {
const date = new Date();
const year = date.getFullYear();
const month = date.getMonth();
const day = date.getDate();
return [
// интервал времени текущей даты с 12:34:20 - 16:10:41
[
new Date(year, month, day, 12, 34, 20),
new Date(year, month, day, 16, 10, 41),
],
// слелдующий день
new Date(year, month, day + 1),
// отключаем с 1го по 12ое включительно
[new Date(year, month, 1), new Date(year, month, 13)],
];
};

const disableDates = getDisableDates();

export const DatePickerExampleDisableDates = () => {
const [value, setValue] = useState<Date | null>();

return (
<DatePicker
type="date-time"
value={value}
disableDates={disableDates}
onChange={setValue}
/>
);
};
```

<DatePickerExampleDisableDates />

## Управление состоянием выпадающего списка

Для управления состоянием выпадающего списка используйте следующие свойства:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Example } from '@consta/stand';
import React, { useState } from 'react';

import { DatePicker } from '../../../DatePicker';

const getDisableDates = (): Array<Date | [Date, Date]> => {
const date = new Date();
const year = date.getFullYear();
const month = date.getMonth();
const day = date.getDate();
return [
// интервал времени текущей даты с 12:34:20 - 16:10:41
[
new Date(year, month, day, 12, 34, 20),
new Date(year, month, day, 16, 10, 41),
],
// слелдующий день
new Date(year, month, day + 1),
// отключаем с 1го по 12ое включительно
[new Date(year, month, 1), new Date(year, month, 13)],
];
};

const disableDates = getDisableDates();

export const DatePickerExampleDisableDates = () => {
const [value, setValue] = useState<Date | null>();

return (
<Example col={1}>
<DatePicker
type="date-time"
value={value}
disableDates={disableDates}
onChange={setValue}
/>
</Example>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@ export const DatePickerExampleTypeDate = () => {
const [value, setValue] = useState<Date | null>(null);
return (
<Example col={1}>
<DatePicker
type="date"
value={value}
onChange={setValue}
style={{ zIndex: 2 }}
/>
<DatePicker type="date" value={value} onChange={setValue} />
</Example>
);
};
Expand All @@ -21,12 +16,7 @@ export const DatePickerExampleTypeDateRange = () => {
const [value, setValue] = useState<[Date?, Date?] | null>(null);
return (
<Example col={1}>
<DatePicker
type="date-range"
value={value}
onChange={setValue}
style={{ zIndex: 2 }}
/>
<DatePicker type="date-range" value={value} onChange={setValue} />
</Example>
);
};
Expand All @@ -35,12 +25,7 @@ export const DatePickerExampleTypeDateTime = () => {
const [value, setValue] = useState<Date | null>(null);
return (
<Example col={1}>
<DatePicker
type="date-time"
value={value}
onChange={setValue}
style={{ zIndex: 2 }}
/>
<DatePicker type="date-time" value={value} onChange={setValue} />
</Example>
);
};
Expand All @@ -49,12 +34,7 @@ export const DatePickerExampleTypeDateTimeRange = () => {
const [value, setValue] = useState<[Date?, Date?] | null>(null);
return (
<Example col={1}>
<DatePicker
type="date-time-range"
value={value}
onChange={setValue}
style={{ zIndex: 2 }}
/>
<DatePicker type="date-time-range" value={value} onChange={setValue} />
</Example>
);
};
Expand Down Expand Up @@ -91,12 +71,7 @@ export const DatePickerExampleTypeMonthRange = () => {
const [value, setValue] = useState<[Date?, Date?] | null>(null);
return (
<Example col={1}>
<DatePicker
type="month-range"
value={value}
onChange={setValue}
style={{ zIndex: 2 }}
/>
<DatePicker type="month-range" value={value} onChange={setValue} />
</Example>
);
};
Expand All @@ -105,12 +80,7 @@ export const DatePickerExampleTypeYear = () => {
const [value, setValue] = useState<Date | null>();
return (
<Example col={1}>
<DatePicker
type="year"
value={value}
onChange={setValue}
style={{ zIndex: 2 }}
/>
<DatePicker type="year" value={value} onChange={setValue} />
</Example>
);
};
Expand All @@ -119,12 +89,7 @@ export const DatePickerExampleTypeYearRange = () => {
const [value, setValue] = useState<[Date?, Date?] | null>(null);
return (
<Example col={1}>
<DatePicker
type="year-range"
value={value}
onChange={setValue}
style={{ zIndex: 2 }}
/>
<DatePicker type="year-range" value={value} onChange={setValue} />
</Example>
);
};
7 changes: 6 additions & 1 deletion src/components/DatePicker/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import { Locale } from 'date-fns';

import { DateRange } from '../../utils/types/Date';
import { PropsWithHTMLAttributesAndRef } from '../../utils/types/PropsWithHTMLAttributes';
import { DateTimePropView, dateTimePropViewDefault } from '../DateTime/helpers';
import {
DateTimePropDisableDates,
DateTimePropView,
dateTimePropViewDefault,
} from '../DateTime/helpers';
import {
TextFieldPropForm,
TextFieldPropSize,
Expand Down Expand Up @@ -110,6 +114,7 @@ export type DatePickerProps<TYPE extends DatePickerPropType = 'date'> =
id?: string;
name?: DatePickerPropName<TYPE>;
disabled?: boolean;
disableDates?: DateTimePropDisableDates;
dropdownClassName?: string;
dropdownRef?: React.Ref<HTMLDivElement>;
size?: TextFieldPropSize;
Expand Down

0 comments on commit 4909547

Please sign in to comment.