File tree Expand file tree Collapse file tree 5 files changed +54
-3
lines changed Expand file tree Collapse file tree 5 files changed +54
-3
lines changed Original file line number Diff line number Diff line change @@ -22,7 +22,7 @@ import PickerPanel, {
2222 PickerPanelTimeProps ,
2323} from './PickerPanel' ;
2424import PickerTrigger from './PickerTrigger' ;
25- import { formatValue , isEqual } from './utils/dateUtil' ;
25+ import { formatValue , isEqual , parseValue } from './utils/dateUtil' ;
2626import getDataOrAriaProps , { toArray } from './utils/miscUtil' ;
2727import PanelContext , { ContextOperationRefProps } from './PanelContext' ;
2828import { 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 }
Original file line number Diff line number Diff line change @@ -19,6 +19,7 @@ import {
1919 isSameWeek ,
2020 isSameQuarter ,
2121 formatValue ,
22+ parseValue ,
2223} from './utils/dateUtil' ;
2324import useValueTexts from './hooks/useValueTexts' ;
2425import 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
Original file line number Diff line number Diff 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+ }
Original file line number Diff line number Diff 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' , ( ) => {
Original file line number Diff line number Diff 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' , ( ) => {
You can’t perform that action at this time.
0 commit comments