Skip to content

Commit 7b4965b

Browse files
authored
fix: clear value when format is function (#144)
* fix: should can clear value when format is function * chore: parse value
1 parent c753f06 commit 7b4965b

File tree

5 files changed

+54
-3
lines changed

5 files changed

+54
-3
lines changed

src/Picker.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import PickerPanel, {
2222
PickerPanelTimeProps,
2323
} from './PickerPanel';
2424
import PickerTrigger from './PickerTrigger';
25-
import { formatValue, isEqual } from './utils/dateUtil';
25+
import { formatValue, isEqual, parseValue } from './utils/dateUtil';
2626
import getDataOrAriaProps, { toArray } from './utils/miscUtil';
2727
import PanelContext, { ContextOperationRefProps } from './PanelContext';
2828
import { CustomFormat, PickerMode } from './interface';
@@ -225,7 +225,11 @@ function InnerPicker<DateType>(props: PickerProps<DateType>) {
225225
const [text, triggerTextChange, resetText] = useTextValueMapping({
226226
valueTexts,
227227
onTextChange: newText => {
228-
const inputDate = generateConfig.locale.parse(locale.locale, newText, formatList as string[]);
228+
const inputDate = parseValue(newText, {
229+
locale,
230+
formatList,
231+
generateConfig,
232+
});
229233
if (inputDate && (!disabledDate || !disabledDate(inputDate))) {
230234
setSelectedValue(inputDate);
231235
}

src/RangePicker.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
isSameWeek,
2020
isSameQuarter,
2121
formatValue,
22+
parseValue,
2223
} from './utils/dateUtil';
2324
import useValueTexts from './hooks/useValueTexts';
2425
import useTextValueMapping from './hooks/useTextValueMapping';
@@ -516,7 +517,11 @@ function InnerRangePicker<DateType>(props: RangePickerProps<DateType>) {
516517
);
517518

518519
const onTextChange = (newText: string, index: 0 | 1) => {
519-
const inputDate = generateConfig.locale.parse(locale.locale, newText, formatList as string[]);
520+
const inputDate = parseValue(newText, {
521+
locale,
522+
formatList,
523+
generateConfig,
524+
});
520525

521526
const disabledFunc = index === 0 ? disabledStartDate : disabledEndDate;
522527

src/utils/dateUtil.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,22 @@ export function formatValue<DateType>(
209209
? format(value)
210210
: generateConfig.locale.format(locale.locale, value, format);
211211
}
212+
213+
export function parseValue<DateType>(
214+
value: string,
215+
{
216+
generateConfig,
217+
locale,
218+
formatList,
219+
}: {
220+
generateConfig: GenerateConfig<DateType>;
221+
locale: Locale;
222+
formatList: Array<string | CustomFormat<DateType>>;
223+
},
224+
) {
225+
if (!value || typeof formatList[0] === 'function') {
226+
return null;
227+
}
228+
229+
return generateConfig.locale.parse(locale.locale, value, formatList as string[]);
230+
}

tests/picker.spec.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,7 @@ describe('Picker.Basic', () => {
729729
it('custom format', () => {
730730
const wrapper = mount(
731731
<MomentPicker
732+
allowClear
732733
defaultValue={getMoment('2020-09-17')}
733734
format={[(val: Moment) => `custom format:${val.format('YYYYMMDD')}`, 'YYYY-MM-DD']}
734735
/>,
@@ -738,6 +739,11 @@ describe('Picker.Basic', () => {
738739
wrapper.selectCell(24);
739740
wrapper.closePicker();
740741
expect(wrapper.find('input').prop('value')).toEqual('custom format:20200924');
742+
743+
// clear
744+
const clearNode = wrapper.find('.rc-picker-clear');
745+
expect(clearNode.simulate.bind(clearNode, 'mouseUp')).not.toThrow();
746+
expect(wrapper.find('input').prop('value')).toEqual('');
741747
});
742748

743749
it('panelRender', () => {

tests/range.spec.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,6 +1256,7 @@ describe('Picker.Range', () => {
12561256
it('custom format', () => {
12571257
const wrapper = mount(
12581258
<MomentRangePicker
1259+
allowClear
12591260
format={[(val: Moment) => `custom format:${val.format('YYYYMMDD')}`, 'YYYY-MM-DD']}
12601261
defaultValue={[getMoment('2020-09-17'), getMoment('2020-10-17')]}
12611262
/>,
@@ -1296,6 +1297,22 @@ describe('Picker.Range', () => {
12961297
.last()
12971298
.prop('value'),
12981299
).toEqual('custom format:20201024');
1300+
1301+
// clear
1302+
const clearNode = wrapper.find('.rc-picker-clear');
1303+
expect(clearNode.simulate.bind(clearNode, 'mouseUp')).not.toThrow();
1304+
expect(
1305+
wrapper
1306+
.find('input')
1307+
.first()
1308+
.prop('value'),
1309+
).toEqual('');
1310+
expect(
1311+
wrapper
1312+
.find('input')
1313+
.last()
1314+
.prop('value'),
1315+
).toEqual('');
12991316
});
13001317

13011318
describe('auto open', () => {

0 commit comments

Comments
 (0)