Skip to content

Commit

Permalink
patch: pr7736 (#7903)
Browse files Browse the repository at this point in the history
* feat(Calendar): add doneButtonShow, doneButtonDisabled props flags (#7736)

* feat(Calendar): add doneButtonShow, doneButtonDisabled props flags

* test(Calendar): add screenshots

* test(Calendar): update screenshots

* test(Calendar): update screenshots

* fix(CalendarTime): fix formatting

(cherry picked from commit 5775640)

* fix(CalendarTime): run prettier
  • Loading branch information
EldarMuhamethanov authored Nov 6, 2024
1 parent 72d104a commit e975d9f
Show file tree
Hide file tree
Showing 16 changed files with 109 additions and 31 deletions.
12 changes: 12 additions & 0 deletions packages/vkui/src/components/Calendar/Calendar.e2e-playground.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ export const CalendarPlayground = (props: ComponentPlaygroundProps) => {
enableTime: [true, false],
doneButtonText: [undefined, 'Done'],
},
{
value: [new Date('1970-05-05')],
enableTime: [true],
doneButtonText: ['Done'],
doneButtonShow: [true, false],
},
{
value: [new Date('1970-05-05')],
enableTime: [true],
doneButtonShow: [true],
doneButtonDisabled: [true],
},
{
value: [new Date('1970-05-05')],
nextMonthIcon: [
Expand Down
14 changes: 12 additions & 2 deletions packages/vkui/src/components/Calendar/Calendar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@ import styles from './Calendar.module.css';

export interface CalendarProps
extends Omit<HTMLAttributesWithRootRef<HTMLDivElement>, 'onChange'>,
Pick<CalendarTimeProps, 'changeHoursLabel' | 'changeMinutesLabel'>,
Pick<
CalendarTimeProps,
| 'changeHoursLabel'
| 'changeMinutesLabel'
| 'doneButtonText'
| 'doneButtonDisabled'
| 'doneButtonShow'
>,
Pick<
CalendarHeaderProps,
| 'prevMonthLabel'
Expand Down Expand Up @@ -42,7 +49,6 @@ export interface CalendarProps
disableFuture?: boolean;
enableTime?: boolean;
disablePickers?: boolean;
doneButtonText?: string;
changeDayLabel?: string;
weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6;
showNeighboringMonth?: boolean;
Expand Down Expand Up @@ -88,6 +94,8 @@ export const Calendar = ({
onClose,
enableTime = false,
doneButtonText,
doneButtonDisabled,
doneButtonShow,
weekStartsOn = 1,
disablePickers,
changeHoursLabel = 'Изменить час',
Expand Down Expand Up @@ -234,6 +242,8 @@ export const Calendar = ({
onChange={onChange}
onClose={onClose}
doneButtonText={doneButtonText}
doneButtonDisabled={doneButtonDisabled}
doneButtonShow={doneButtonShow}
changeHoursLabel={changeHoursLabel}
changeMinutesLabel={changeMinutesLabel}
isDayDisabled={minDateTime || maxDateTime ? isDayDisabled : undefined}
Expand Down
9 changes: 9 additions & 0 deletions packages/vkui/src/components/Calendar/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { lightFormat } from 'date-fns';
const Example = () => {
const [value, setValue] = useState(() => new Date());
const [enableTime, setEnableTime] = useState(false);
const [doneButtonShow, setDoneButtonShow] = useState(true);
const [disablePast, setDisablePast] = useState(false);
const [disableFuture, setDisableFuture] = useState(false);
const [disablePickers, setDisablePickers] = useState(false);
Expand All @@ -25,6 +26,13 @@ const Example = () => {
Включено
</Checkbox>
</FormItem>
{enableTime && (
<FormItem top="Показывать кнопку 'Готово'">
<Checkbox checked={doneButtonShow} onChange={(e) => setDoneButtonShow(e.target.checked)}>
Включено
</Checkbox>
</FormItem>
)}
<FormItem top="Запрет выбора прошлых дат">
<Checkbox checked={disablePast} onChange={(e) => setDisablePast(e.target.checked)}>
Включено
Expand Down Expand Up @@ -107,6 +115,7 @@ const Example = () => {
disablePast={disablePast}
disableFuture={disableFuture}
disablePickers={disablePickers}
doneButtonShow={doneButtonShow}
showNeighboringMonth={showNeighboringMonth}
size={size}
listenDayChangesForUpdate={listenDayChangesForUpdate}
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
inline-size: 77px;
}

.CalendarTime__withoutDone {
justify-content: center;
}

.CalendarTime__divider {
margin-inline: 6px;
color: var(--vkui--color_text_primary);
Expand Down
31 changes: 31 additions & 0 deletions packages/vkui/src/components/CalendarTime/CalendarTime.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,35 @@ describe('CalendarTime', () => {

expect(onChange).toHaveBeenCalledTimes(0);
});

it('should hide done button with doneButtonShow=false', () => {
const onChange = jest.fn();
const buttonText = 'Текст';
render(
<CalendarTime
onChange={onChange}
value={dayDate}
doneButtonShow={false}
doneButtonText={buttonText}
/>,
);
expect(screen.queryByText(buttonText)).toBeFalsy();
});

it('should disable done button with doneButtonDisabled=false', () => {
const onChange = jest.fn();
const buttonText = 'Текст';
render(
<CalendarTime
onChange={onChange}
value={dayDate}
doneButtonText={buttonText}
doneButtonDisabled={true}
/>,
);
const text = screen.queryByText(buttonText);
expect(text).toBeTruthy();
const button = text!.closest('button');
expect(button!.disabled).toBeTruthy();
});
});
30 changes: 21 additions & 9 deletions packages/vkui/src/components/CalendarTime/CalendarTime.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from 'react';
import { classNames } from '@vkontakte/vkjs';
import { setHours, setMinutes } from 'date-fns';
import { AdaptivityProvider } from '../AdaptivityProvider/AdaptivityProvider';
import { Button } from '../Button/Button';
Expand All @@ -8,6 +9,8 @@ import styles from './CalendarTime.module.css';
export interface CalendarTimeProps {
value: Date;
doneButtonText?: string;
doneButtonShow?: boolean;
doneButtonDisabled?: boolean;
changeHoursLabel?: string;
changeMinutesLabel?: string;
onChange?: (value: Date) => void;
Expand All @@ -33,12 +36,14 @@ for (let i = 0; i < 60; i += 1) {

export const CalendarTime = ({
value,
doneButtonText = 'Готово',
onChange,
onClose,
changeHoursLabel,
changeMinutesLabel,
isDayDisabled,
doneButtonText = 'Готово',
doneButtonDisabled = false,
doneButtonShow = true,
}: CalendarTimeProps): React.ReactNode => {
const localHours = isDayDisabled
? hours.map((hour) => {
Expand All @@ -64,7 +69,12 @@ export const CalendarTime = ({
);

return (
<div className={styles['CalendarTime']}>
<div
className={classNames(
styles['CalendarTime'],
!doneButtonShow && styles['CalendarTime__withoutDone'],
)}
>
<div className={styles['CalendarTime__picker']}>
<AdaptivityProvider sizeY="compact">
<CustomSelect
Expand All @@ -88,13 +98,15 @@ export const CalendarTime = ({
/>
</AdaptivityProvider>
</div>
<div className={styles['CalendarTime__button']}>
<AdaptivityProvider sizeY="compact">
<Button mode="secondary" onClick={onClose} size="l">
{doneButtonText}
</Button>
</AdaptivityProvider>
</div>
{doneButtonShow && (
<div className={styles['CalendarTime__button']}>
<AdaptivityProvider sizeY="compact">
<Button mode="secondary" onClick={onClose} size="l" disabled={doneButtonDisabled}>
{doneButtonText}
</Button>
</AdaptivityProvider>
</div>
)}
</div>
);
};

0 comments on commit e975d9f

Please sign in to comment.