Skip to content

Commit

Permalink
fix(DatePicker): week select with year edge week not working as expect (
Browse files Browse the repository at this point in the history
#6808)

* fix: iso week selection

* test: add test case
  • Loading branch information
zombieJ authored Dec 31, 2024
1 parent 12ca364 commit a593db1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
9 changes: 4 additions & 5 deletions src/components/date-picker/date-picker-week-utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { ReactNode } from 'react'
import dayjs from 'dayjs'
import isLeapYear from 'dayjs/plugin/isLeapYear'
import isoWeek from 'dayjs/plugin/isoWeek'
import isoWeeksInYear from 'dayjs/plugin/isoWeeksInYear'
import isLeapYear from 'dayjs/plugin/isLeapYear'
import type { ReactNode } from 'react'
import { PickerColumn } from '../picker'
import type { DatePickerFilter } from './date-picker-utils'

Expand Down Expand Up @@ -123,13 +123,12 @@ export function convertDateToStringArray(
}

export function convertStringArrayToDate<
T extends string | number | null | undefined
T extends string | number | null | undefined,
>(value: T[]): Date {
const yearString = value[0] ?? '1900'
const weekString = value[1] ?? '1'
const weekdayString = value[2] ?? '1'
const day = dayjs()
.year(parseInt(yearString as string))
const day = dayjs(`${parseInt(yearString as string)}-01-01`)
.isoWeek(parseInt(weekString as string))
.isoWeekday(parseInt(weekdayString as string))
.hour(0)
Expand Down
25 changes: 19 additions & 6 deletions src/components/date-picker/tests/date-picker.test.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import dayjs from 'dayjs'
import * as React from 'react'
import {
render,
testA11y,
act,
fireEvent,
waitFor,
render,
screen,
sleep,
act,
testA11y,
waitFor,
waitForElementToBeRemoved,
} from 'testing'
import * as React from 'react'
import DatePicker from '../'
import dayjs from 'dayjs'
import Button from '../../button'
import { convertStringArrayToDate } from '../date-picker-week-utils'

const classPrefix = `adm-picker`

Expand Down Expand Up @@ -205,4 +206,16 @@ describe('DatePicker', () => {
expect(res.toDateString()).toEqual(now.toDateString())
expect(res.tillNow).toBeTruthy()
})

test('convertStringArrayToDate of week should correct', () => {
// jest mock today is `2024-12-30`
jest.useFakeTimers({
now: new Date('2024-12-30'),
})

const date = convertStringArrayToDate(['2024', '1', '1'])
expect(date.getFullYear()).toBe(2024)
expect(date.getMonth()).toBe(0)
expect(date.getDate()).toBe(1)
})
})

0 comments on commit a593db1

Please sign in to comment.