diff --git a/packages/li-p2/src/components/Formily/FilterConfiguration/FilterModal/FilterContent/DateItem/index.tsx b/packages/li-p2/src/components/Formily/FilterConfiguration/FilterModal/FilterContent/DateItem/index.tsx index 334ad39d..2b49c7d8 100644 --- a/packages/li-p2/src/components/Formily/FilterConfiguration/FilterModal/FilterContent/DateItem/index.tsx +++ b/packages/li-p2/src/components/Formily/FilterConfiguration/FilterModal/FilterContent/DateItem/index.tsx @@ -18,34 +18,20 @@ export interface DateItemProps { const DateItem: React.FC = (props) => { const prefixCls = usePrefixCls('formily-filter-setting-modal-date-item'); const [wrapSSR, hashId] = useStyle(prefixCls); - const { value: defaultValue, format, onChange } = props; - - const timer = useMemo(() => { - return defaultValue.value; - }, [defaultValue.value]); - - const [granularity, setGranularity] = useState(DEFAULT_OPTIONS[0]); - + const { value: outterValue, format, onChange } = props; + const dateValue = outterValue.value; // 粒度 options - const options = useMemo(() => { - if (format) { - const _options = getOptions(format); + const options = useMemo(() => (format ? getOptions(format) : []), [format]); - const _granularity = _options.find( - (item) => defaultValue.granularity && [item.granularity, item.value].includes(defaultValue.granularity), - ); - if (_granularity) { - setGranularity(_granularity); - } - return _options; - } - return []; - }, [defaultValue, format]); + const [granularity, setGranularity] = useState(() => { + const _granularity = options.find((item) => [item.granularity, item.value].includes(outterValue.granularity)); + return _granularity || DEFAULT_OPTIONS[0]; + }); // 时间类型 const onDateTypeChange = (e: 'date' | 'range') => { - const _times = timer ? getTimeFormat(timer[0], granularity?.value) : undefined; - onChange({ ...defaultValue, value: _times, params: { ...defaultValue.params, dateType: e } }); + const _times = dateValue ? getTimeFormat(dateValue[0], granularity?.value) : undefined; + onChange({ ...outterValue, value: _times, params: { ...outterValue.params, dateType: e } }); }; // 选择粒度 @@ -54,11 +40,11 @@ const DateItem: React.FC = (props) => { if (_granularity) { const _value: FilterDateConfigType = { - ...defaultValue, + ...outterValue, granularity: _granularity.granularity, - value: timer ? getTimeFormat(timer, _granularity?.value) : undefined, + value: dateValue ? getTimeFormat(dateValue, _granularity?.value) : undefined, params: { - ...defaultValue.params, + ...outterValue.params, format: _granularity.value, }, }; @@ -71,7 +57,7 @@ const DateItem: React.FC = (props) => { const onValueChange = (val: any) => { const { value } = val; onChange({ - ...defaultValue, + ...outterValue, value, }); }; @@ -80,7 +66,7 @@ const DateItem: React.FC = (props) => { <>
日期类型
- onDateTypeChange(e.target.value)}> + onDateTypeChange(e.target.value)}> 单日期 日期区间 @@ -91,7 +77,7 @@ const DateItem: React.FC = (props) => { -
+