Skip to content

Commit

Permalink
fix(DatePicker2): support defaultValue for quarter, close #3006
Browse files Browse the repository at this point in the history
  • Loading branch information
mwb-27 committed Nov 28, 2024
1 parent 1849be8 commit 298c10c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 7 deletions.
37 changes: 37 additions & 0 deletions components/date-picker2/__tests__/index-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1257,6 +1257,43 @@ describe('Picker', () => {
findInput().simulate('keydown', { keyCode: KEYCODE.ENTER });
assert(getStrValue(wrapper) === '12/02/2020');
});

// fix https://github.com/alibaba-fusion/next/issues/3006
it('Support defaultValue for quarter', () => {
let defaultValue = ['2021-Q2', '2021-Q1']
wrapper = mount(
<RangePicker
defaultValue={defaultValue}
mode='quarter'
/>
);
assert.deepEqual(getStrValue(), ['2021-Q2', '2021-Q1']);

defaultValue = ['2021-4-1', '2021-8-1']
wrapper = mount(
<RangePicker
defaultValue={defaultValue}
mode='quarter'
/>
);
assert.deepEqual(getStrValue(), ['2021-Q2', '2021-Q3']);

defaultValue = '2021-Q3'
wrapper = mount(
<QuarterPicker
defaultValue={defaultValue}
/>
);
assert(getStrValue() === '2021-Q3');

defaultValue = '2021-7-1'
wrapper = mount(
<QuarterPicker
defaultValue={defaultValue}
/>
);
assert(getStrValue() === '2021-Q3');
})
});
});

Expand Down
8 changes: 7 additions & 1 deletion components/date-picker2/picker.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,18 @@ class Picker extends React.Component {
*/
getInitValue = () => {
const { props } = this;
const { type, value, defaultValue } = props;
const { type, value, defaultValue, format } = props;

let val = type === DATE_PICKER_TYPE.RANGE ? [null, null] : null;

val = 'value' in props ? value : 'defaultValue' in props ? defaultValue : val;

// 2024-Q2这类季度字符,dayjs必须传入format才能正常初始化
const regex = /^(?<year>\d{4})-Q(?<quarter>[1-4])$/;
if ((type === DATE_PICKER_TYPE.RANGE && val && regex.exec(val[0]) && regex.exec(val[1])) || regex.exec(val)) {
return this.checkValue(val, false, format);
}

return this.checkValue(val);
};

Expand Down
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 298c10c

Please sign in to comment.