Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
hyoban committed Aug 17, 2023
2 parents 0f0257f + 035d287 commit 1c9a08e
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 19 deletions.
28 changes: 12 additions & 16 deletions src/Calendar/Calendar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ export interface CalendarProps extends WithAsProps {
/** Callback fired before the value changed */
onChange?: (date: Date) => void;

/** Callback fired before the month changed */
/**
* Callback fired before the month changed
* @todo-Doma Change signature to `onMonthChange(year: number, month: number, reason: string)`?
*/
onMonthChange?: (date: Date) => void;

/** Callback fired before the date selected */
Expand Down Expand Up @@ -71,8 +74,12 @@ const Calendar: RsRefForwardingComponent<typeof CalendarContainer, CalendarProps
(nextValue: Date) => {
setCalendarDate(nextValue);
onChange?.(nextValue);

if (!isSameMonth(nextValue, calendarDate)) {
onMonthChange?.(nextValue);
}
},
[setCalendarDate, onChange]
[setCalendarDate, onChange, calendarDate, onMonthChange]
);

const handleClickToday = useCallback(() => {
Expand All @@ -87,17 +94,6 @@ const Calendar: RsRefForwardingComponent<typeof CalendarContainer, CalendarProps
[handleChange, onSelect]
);

// Trigger onMonthChange when the month changes
const handleMonthChange = useCallback(
(nextValue: Date) => {
if (!isSameMonth(nextValue, calendarDate)) {
handleChange(nextValue);
onMonthChange?.(nextValue);
}
},
[calendarDate, handleChange, onMonthChange]
);

const { prefix, merge, withClassPrefix } = useClassNames(classPrefix);

const renderToolbar = useCallback(
Expand Down Expand Up @@ -130,9 +126,9 @@ const Calendar: RsRefForwardingComponent<typeof CalendarContainer, CalendarProps
renderToolbar={renderToolbar}
renderCell={customRenderCell}
cellClassName={cellClassName}
onMoveForward={handleMonthChange}
onMoveBackward={handleMonthChange}
onChangeMonth={handleMonthChange}
onMoveForward={handleChange}
onMoveBackward={handleChange}
onChangeMonth={handleChange}
onSelect={handleSelect}
/>
);
Expand Down
2 changes: 2 additions & 0 deletions src/Calendar/TableRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ const TableRow: RsRefForwardingComponent<'div', TableRowProps> = React.forwardRe
? isStartSelected || isEndSelected
: DateUtils.isSameDay(thisDate, selected);

// TODO-Doma Move those logic that's for DatePicker/DateRangePicker to a separate component
// Calendar is not supposed to be reused this way
let inRange = false;
// for Selected
if (selectedStartDate && selectedEndDate) {
Expand Down
24 changes: 22 additions & 2 deletions src/Calendar/test/CalendarSpec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,21 @@ describe('Calendar', () => {
expect(cells).to.deep.equal(['26', '27', '28', '29', '30', '31', '1', '2', '3', '4', '5', '6']);
});

it('Should call `onMonthChange` callback', () => {
it('Should call `onMonthChange` callback when the display month changes', () => {
const onMonthChangeSpy = sinon.spy();

render(<Calendar defaultValue={new Date('2023-01-01')} onMonthChange={onMonthChangeSpy} />);
const { rerender } = render(
<Calendar defaultValue={new Date(2023, 0, 1)} onMonthChange={onMonthChangeSpy} />
);

// Change month with Next/Previous month button
fireEvent.click(screen.getByRole('button', { name: 'Next month' }));
expect(onMonthChangeSpy).to.have.been.calledOnce;

fireEvent.click(screen.getByRole('button', { name: 'Previous month' }));
expect(onMonthChangeSpy).to.have.been.calledTwice;

// Change month with Month dropdown
fireEvent.click(screen.getByRole('button', { name: 'Select month' }));

fireEvent.click(
Expand All @@ -120,6 +124,22 @@ describe('Calendar', () => {
);

expect(onMonthChangeSpy).to.have.been.calledThrice;

// Change month by clicking on a date in a different month
rerender(<Calendar value={new Date(2023, 0, 1)} onMonthChange={onMonthChangeSpy} />);
fireEvent.click(screen.getByTitle('01 Feb 2023')); // TODO-Doma Add accessible name to the button via aria-label
expect(onMonthChangeSpy).to.have.callCount(4);
expect((onMonthChangeSpy.getCall(3).args[0] as Date).getFullYear()).to.equal(2023);
expect((onMonthChangeSpy.getCall(3).args[0] as Date).getMonth()).to.equal(1);

// Change month with "Today" button
const clock = sinon.useFakeTimers(new Date(2023, 0, 1));
rerender(<Calendar value={new Date(2023, 1, 1)} onMonthChange={onMonthChangeSpy} />);
fireEvent.click(screen.getByRole('button', { name: 'Today' }));
expect(onMonthChangeSpy).to.have.callCount(5);
expect((onMonthChangeSpy.getCall(4).args[0] as Date).getFullYear()).to.equal(2023);
expect((onMonthChangeSpy.getCall(4).args[0] as Date).getMonth()).to.equal(0);
clock.restore();
});

it('Should not call `onMonthChange` callback when same month is clicked', () => {
Expand Down
1 change: 0 additions & 1 deletion src/Dropdown/styles/mixin.less
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
padding-right: @dropdown-toggle-padding-right;
// Fixed: Content is not centered when customizing renderTitle.
display: inline-block;
cursor: pointer;
}

// Horizontal dividers
Expand Down
4 changes: 4 additions & 0 deletions src/utils/test/useElementResizeSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import { renderHook } from '@test/testUtils';
import useElementResize from '../useElementResize';
import { act, waitFor } from '@testing-library/react';

afterEach(() => {
sinon.restore();
});

describe('[utils] useElementResize', () => {
it('should be defined', () => {
expect(useElementResize).to.be.exist;
Expand Down

0 comments on commit 1c9a08e

Please sign in to comment.