From 7d5bd11953a44bca9743c77960c80350224658e7 Mon Sep 17 00:00:00 2001 From: Wxh16144 Date: Fri, 10 Mar 2023 16:44:29 +0800 Subject: [PATCH 1/5] feat: optimize time selector panel step precision logic (#596) * test: add case * chore: optimize time selector panel step precision logic * test: update snapshot * feat(type): update TS type * test: add case * fix: fix negative number error * perf: update performance * Update tests/picker.spec.tsx Co-authored-by: MadCcc <1075746765@qq.com> --------- Co-authored-by: MadCcc <1075746765@qq.com> (cherry picked from commit 92699847dcd5e4d3cbfb19682049758c6ae1824e) # Conflicts: # src/interface.ts # tests/__snapshots__/picker.spec.tsx.snap --- src/panels/TimePanel/TimeBody.tsx | 3 ++- tests/picker.spec.tsx | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/panels/TimePanel/TimeBody.tsx b/src/panels/TimePanel/TimeBody.tsx index 186182a4e..5a9f7c9da 100644 --- a/src/panels/TimePanel/TimeBody.tsx +++ b/src/panels/TimePanel/TimeBody.tsx @@ -24,7 +24,8 @@ function generateUnits( disabledUnits: number[] | undefined, ) { const units: Unit[] = []; - for (let i = start; i <= end; i += step) { + const integerStep = step >= 1 ? step | 0 : 1; + for (let i = start; i <= end; i += integerStep) { units.push({ label: leftPad(i, 2), value: i, diff --git a/tests/picker.spec.tsx b/tests/picker.spec.tsx index d044b1536..3838ebad5 100644 --- a/tests/picker.spec.tsx +++ b/tests/picker.spec.tsx @@ -581,6 +581,25 @@ describe('Picker.Basic', () => { ); spy.mockRestore(); }); + + // https://github.com/ant-design/ant-design/issues/40914 + ['hour', 'minute', 'second'].forEach((unit, index) => { + it(`should show integer when step is not integer (${unit})`, () => { + const props = { + [`${unit}Step`]: 5.5, + }; + const { container } = render(); + openPicker(container); + expect(document.querySelectorAll('.rc-picker-time-panel-column')[index]).toMatchSnapshot(); + }); + }); + + it('should work when hourStep < 0', () => { + // @ts-ignore + const { container } = render(); + openPicker(container); + expect(document.querySelectorAll('.rc-picker-time-panel-column')[0].children.length).toBe(24); + }); }); it('pass data- & aria- & role', () => { From ee70112ad7c9c53b478f04b0b0bbe3fce166c648 Mon Sep 17 00:00:00 2001 From: wuxh Date: Wed, 12 Apr 2023 18:41:43 +0800 Subject: [PATCH 2/5] test: update case --- tests/__snapshots__/picker.spec.tsx.snap | 355 +++++++++++++++++++++++ tests/picker.spec.tsx | 15 +- 2 files changed, 362 insertions(+), 8 deletions(-) diff --git a/tests/__snapshots__/picker.spec.tsx.snap b/tests/__snapshots__/picker.spec.tsx.snap index ef67cdf5b..74e08db3d 100644 --- a/tests/__snapshots__/picker.spec.tsx.snap +++ b/tests/__snapshots__/picker.spec.tsx.snap @@ -115,3 +115,358 @@ exports[`Picker.Basic should render correctly in rtl 1`] = ` `; + +exports[`Picker.Basic time step should show integer when step is not integer (hour) 1`] = ` +
    +
  • +
    + 00 +
    +
  • +
  • +
    + 05 +
    +
  • +
  • +
    + 10 +
    +
  • +
  • +
    + 15 +
    +
  • +
  • +
    + 20 +
    +
  • +
+`; + +exports[`Picker.Basic time step should show integer when step is not integer (minute) 1`] = ` +
    +
  • +
    + 00 +
    +
  • +
  • +
    + 05 +
    +
  • +
  • +
    + 10 +
    +
  • +
  • +
    + 15 +
    +
  • +
  • +
    + 20 +
    +
  • +
  • +
    + 25 +
    +
  • +
  • +
    + 30 +
    +
  • +
  • +
    + 35 +
    +
  • +
  • +
    + 40 +
    +
  • +
  • +
    + 45 +
    +
  • +
  • +
    + 50 +
    +
  • +
  • +
    + 55 +
    +
  • +
+`; + +exports[`Picker.Basic time step should show integer when step is not integer (second) 1`] = ` +
    +
  • +
    + 00 +
    +
  • +
  • +
    + 05 +
    +
  • +
  • +
    + 10 +
    +
  • +
  • +
    + 15 +
    +
  • +
  • +
    + 20 +
    +
  • +
  • +
    + 25 +
    +
  • +
  • +
    + 30 +
    +
  • +
  • +
    + 35 +
    +
  • +
  • +
    + 40 +
    +
  • +
  • +
    + 45 +
    +
  • +
  • +
    + 50 +
    +
  • +
  • +
    + 55 +
    +
  • +
+`; diff --git a/tests/picker.spec.tsx b/tests/picker.spec.tsx index 3838ebad5..8d5d1b9e1 100644 --- a/tests/picker.spec.tsx +++ b/tests/picker.spec.tsx @@ -584,21 +584,20 @@ describe('Picker.Basic', () => { // https://github.com/ant-design/ant-design/issues/40914 ['hour', 'minute', 'second'].forEach((unit, index) => { - it(`should show integer when step is not integer (${unit})`, () => { + it.only(`should show integer when step is not integer (${unit})`, () => { const props = { [`${unit}Step`]: 5.5, }; - const { container } = render(); - openPicker(container); - expect(document.querySelectorAll('.rc-picker-time-panel-column')[index]).toMatchSnapshot(); + const wrapper = mount(); + wrapper.openPicker(); + expect(wrapper.find('.rc-picker-time-panel-column').at(index)).toMatchSnapshot(); }); }); it('should work when hourStep < 0', () => { - // @ts-ignore - const { container } = render(); - openPicker(container); - expect(document.querySelectorAll('.rc-picker-time-panel-column')[0].children.length).toBe(24); + const wrapper = mount(); + wrapper.openPicker(); + expect(wrapper.find('.rc-picker-time-panel-column').at(0).children()).toHaveLength(24); }); }); From 7f4529164cd81919a1a58c6b8964a876be81c2a9 Mon Sep 17 00:00:00 2001 From: wuxh Date: Wed, 12 Apr 2023 18:54:23 +0800 Subject: [PATCH 3/5] test: update case --- tests/picker.spec.tsx | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/tests/picker.spec.tsx b/tests/picker.spec.tsx index 8d5d1b9e1..bac7c1f3b 100644 --- a/tests/picker.spec.tsx +++ b/tests/picker.spec.tsx @@ -584,13 +584,18 @@ describe('Picker.Basic', () => { // https://github.com/ant-design/ant-design/issues/40914 ['hour', 'minute', 'second'].forEach((unit, index) => { - it.only(`should show integer when step is not integer (${unit})`, () => { + it(`should show integer when step is not integer (${unit})`, () => { const props = { [`${unit}Step`]: 5.5, }; const wrapper = mount(); wrapper.openPicker(); - expect(wrapper.find('.rc-picker-time-panel-column').at(index)).toMatchSnapshot(); + + const column = wrapper.find('.rc-picker-time-panel-column').at(index); + const cells = column.find('.rc-picker-time-panel-cell-inner'); + cells.forEach((cell) => { + expect(Number.isInteger(Number(cell.text()))); + }); }); }); From f763d9522f83ae4b6ca4198d8792e8ab5298e829 Mon Sep 17 00:00:00 2001 From: wuxh Date: Wed, 12 Apr 2023 18:55:22 +0800 Subject: [PATCH 4/5] chore: remove snap --- tests/__snapshots__/picker.spec.tsx.snap | 355 ----------------------- 1 file changed, 355 deletions(-) diff --git a/tests/__snapshots__/picker.spec.tsx.snap b/tests/__snapshots__/picker.spec.tsx.snap index 74e08db3d..ef67cdf5b 100644 --- a/tests/__snapshots__/picker.spec.tsx.snap +++ b/tests/__snapshots__/picker.spec.tsx.snap @@ -115,358 +115,3 @@ exports[`Picker.Basic should render correctly in rtl 1`] = ` `; - -exports[`Picker.Basic time step should show integer when step is not integer (hour) 1`] = ` -
    -
  • -
    - 00 -
    -
  • -
  • -
    - 05 -
    -
  • -
  • -
    - 10 -
    -
  • -
  • -
    - 15 -
    -
  • -
  • -
    - 20 -
    -
  • -
-`; - -exports[`Picker.Basic time step should show integer when step is not integer (minute) 1`] = ` -
    -
  • -
    - 00 -
    -
  • -
  • -
    - 05 -
    -
  • -
  • -
    - 10 -
    -
  • -
  • -
    - 15 -
    -
  • -
  • -
    - 20 -
    -
  • -
  • -
    - 25 -
    -
  • -
  • -
    - 30 -
    -
  • -
  • -
    - 35 -
    -
  • -
  • -
    - 40 -
    -
  • -
  • -
    - 45 -
    -
  • -
  • -
    - 50 -
    -
  • -
  • -
    - 55 -
    -
  • -
-`; - -exports[`Picker.Basic time step should show integer when step is not integer (second) 1`] = ` -
    -
  • -
    - 00 -
    -
  • -
  • -
    - 05 -
    -
  • -
  • -
    - 10 -
    -
  • -
  • -
    - 15 -
    -
  • -
  • -
    - 20 -
    -
  • -
  • -
    - 25 -
    -
  • -
  • -
    - 30 -
    -
  • -
  • -
    - 35 -
    -
  • -
  • -
    - 40 -
    -
  • -
  • -
    - 45 -
    -
  • -
  • -
    - 50 -
    -
  • -
  • -
    - 55 -
    -
  • -
-`; From ccd04c327cb9a1a90965a2d5d16669065f2d99a0 Mon Sep 17 00:00:00 2001 From: wuxh Date: Wed, 12 Apr 2023 19:08:04 +0800 Subject: [PATCH 5/5] test: update case --- tests/picker.spec.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/picker.spec.tsx b/tests/picker.spec.tsx index bac7c1f3b..6905e0882 100644 --- a/tests/picker.spec.tsx +++ b/tests/picker.spec.tsx @@ -594,7 +594,7 @@ describe('Picker.Basic', () => { const column = wrapper.find('.rc-picker-time-panel-column').at(index); const cells = column.find('.rc-picker-time-panel-cell-inner'); cells.forEach((cell) => { - expect(Number.isInteger(Number(cell.text()))); + expect(Number.isInteger(Number(cell.text()))).toBeTruthy(); }); }); });