Skip to content

Commit

Permalink
fix(Calendar): fix incorrect week numbers calculation (#3471)
Browse files Browse the repository at this point in the history
* fix(Calendar): fix incorrect week numbers calculation

* fix(Calendar): fix incorrect week numbers calculation

* test(DatePicker): update tests for showWeekNumbers
  • Loading branch information
simonguo committed Dec 1, 2023
1 parent 88021ee commit 646e728
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/Calendar/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const Table: RsRefForwardingComponent<'div', TableProps> = React.forwardRef(
<Component role="grid" {...rest} ref={ref} className={classes}>
<TableHeaderRow />
{rows.map((week, index) => (
<TableRow key={index} weekendDate={week} />
<TableRow key={index} weekendDate={week} rowIndex={index + 1} />
))}
</Component>
);
Expand Down
9 changes: 6 additions & 3 deletions src/Calendar/TableRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { RsRefForwardingComponent, WithAsProps } from '../@types/common';
import TableCell from './TableCell';
export interface TableRowProps extends WithAsProps {
weekendDate?: Date;
rowIndex?: number;
}

const TableRow: RsRefForwardingComponent<'div', TableRowProps> = React.forwardRef(
Expand All @@ -16,6 +17,7 @@ const TableRow: RsRefForwardingComponent<'div', TableRowProps> = React.forwardRe
className,
classPrefix = 'calendar-table',
weekendDate = new Date(),
rowIndex,
...rest
} = props;
const {
Expand Down Expand Up @@ -99,12 +101,13 @@ const TableRow: RsRefForwardingComponent<'div', TableRowProps> = React.forwardRe
};

const classes = merge(className, prefix('row'));
const week = format(weekendDate, isoWeek ? 'I' : 'w', { firstWeekContainsDate: 4 });

return (
<Component {...rest} ref={ref} role="row" className={classes}>
<Component {...rest} ref={ref} role="row" aria-rowindex={rowIndex} className={classes}>
{showWeekNumbers && (
<div className={prefix('cell-week-number')} role="rowheader">
{format(weekendDate, isoWeek ? 'I' : 'w')}
<div role="rowheader" aria-label={`Week ${week}`} className={prefix('cell-week-number')}>
{week}
</div>
)}
{renderDays()}
Expand Down
10 changes: 10 additions & 0 deletions src/Calendar/test/CalendarSpec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,14 @@ describe('Calendar', () => {
expect(onMonthChangeSpy).to.have.been.not.called;
expect(onToggleMonthDropdownSpy).to.have.been.called;
});

it('Should render the correct week numbers', () => {
const { rerender } = render(<Calendar value={new Date('2020-12-01')} showWeekNumbers />);

expect(screen.queryByRole('rowheader', { name: 'Week 53' })).to.be.exist;

rerender(<Calendar value={new Date('2022-12-01')} showWeekNumbers />);

expect(screen.queryByRole('rowheader', { name: 'Week 53' })).to.not.exist;
});
});
4 changes: 2 additions & 2 deletions src/DatePicker/test/DatePickerSpec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -804,10 +804,10 @@ describe('DatePicker', () => {
it('Should render week numbers given `showWeekNumbers=true`', () => {
render(<DatePicker defaultOpen calendarDefaultDate={new Date('12/15/2021')} showWeekNumbers />);

[49, 50, 51, 52, 1, 2].forEach(weekOrder => {
[48, 49, 50, 51, 52, 1].forEach(weekOrder => {
expect(screen.getByRole('grid', { name: 'Dec 2021' })).to.contain(
screen.getByRole('rowheader', {
name: `${weekOrder}`
name: `Week ${weekOrder}`
})
);
});
Expand Down

1 comment on commit 646e728

@vercel
Copy link

@vercel vercel bot commented on 646e728 Dec 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

rsuite-nextjs – ./docs

rsuite-nextjs-rsuite.vercel.app
rsuite.vercel.app
rsuitejs.com
rsuite-nextjs-git-main-rsuite.vercel.app

Please sign in to comment.