55 * https://opensource.org/licenses/MIT
66 */
77
8+ import { t } from "@app/locale"
89import { EL_DATE_FORMAT } from "@i18n/element"
910import { ElementDatePickerShortcut } from "@src/element-ui/date"
1011import { getDatePickerIconSlots } from "@src/element-ui/rtl"
1112import { ElDatePicker } from "element-plus"
1213import { defineComponent , PropType , ref , Ref } from "vue"
1314
15+ const clearShortcut = ( ) : ElementDatePickerShortcut => ( {
16+ text : t ( msg => msg . button . clear ) ,
17+ value : [ new Date ( 0 ) , new Date ( 0 ) ] ,
18+ } )
19+
1420const _default = defineComponent ( {
1521 props : {
1622 defaultRange : Object as PropType < [ Date , Date ] > ,
@@ -27,6 +33,18 @@ const _default = defineComponent({
2733 change : ( _value : [ Date , Date ] ) => true
2834 } ,
2935 setup ( props , ctx ) {
36+ const handleChange = ( newVal : [ Date , Date ] ) => {
37+ const [ start , end ] = newVal || [ ]
38+ const isClearChosen = start ?. getTime ?.( ) === 0 && end ?. getTime ?.( ) === 0
39+ if ( isClearChosen ) newVal = null
40+ ctx . emit ( "change" , dateRange . value = newVal )
41+ }
42+ const shortcuts = ( ) => {
43+ const { shortcuts : value , clearable } = props
44+ if ( ! value ?. length || ! clearable ) return value
45+ return [ ...value , clearShortcut ( ) ]
46+ }
47+
3048 const dateRange : Ref < [ Date , Date ] > = ref ( props . defaultRange || [ undefined , undefined ] )
3149 return ( ) => < span class = "filter-item" >
3250 < ElDatePicker
@@ -35,8 +53,8 @@ const _default = defineComponent({
3553 type = "daterange"
3654 rangeSeparator = "-"
3755 disabledDate = { props . disabledDate }
38- shortcuts = { props . shortcuts }
39- onUpdate :modelValue = { ( newVal : [ Date , Date ] ) => ctx . emit ( "change" , dateRange . value = newVal ) }
56+ shortcuts = { shortcuts ( ) }
57+ onUpdate :modelValue = { ( newVal : [ Date , Date ] ) => handleChange ( newVal ) }
4058 startPlaceholder = { props . startPlaceholder }
4159 endPlaceholder = { props . endPlaceholder }
4260 clearable = { props . clearable }
0 commit comments