fix: date formatting

This commit is contained in:
chenos 2021-08-04 20:11:49 +08:00
parent 02890f550d
commit e41926e2b9
2 changed files with 15 additions and 3 deletions

View File

@ -1,5 +1,5 @@
import moment from 'moment'
import { connect, mapProps, mapReadPretty } from '@formily/react'
import { connect, mapProps, mapReadPretty, useField, useFieldSchema } from '@formily/react'
import { DatePicker as AntdDatePicker } from 'antd'
import {
DatePickerProps as AntdDatePickerProps,
@ -34,6 +34,8 @@ const mapDateFormat = function () {
return props['showTime'] ? 'YYYY-MM-DD HH:mm:ss' : 'YYYY-MM-DD'
}
return (props: any) => {
const field = useField();
const fieldSchema = useFieldSchema();
const format = props['format'] || getDefaultFormat(props)
const onChange = props.onChange
return {

View File

@ -15,6 +15,7 @@ import { Tag, Space, Popover } from 'antd'
import cls from 'classnames'
import { formatMomentValue, usePrefixCls } from '@formily/antd/esm/__builtins__'
import { FullscreenOutlined } from '@ant-design/icons';
import moment from 'moment';
const PlaceholderContext = createContext<string>('N/A')
@ -287,11 +288,20 @@ const Cascader: React.FC<CascaderProps> = observer((props) => {
)
})
const DatePicker: React.FC<DatePickerProps> = (props) => {
const DatePicker: React.FC<DatePickerProps> = (props: any) => {
const placeholder = usePlaceholder()
const prefixCls = usePrefixCls('description-text', props)
const getDefaultFormat = () => {
const { dateFormat, showTime, timeFormat } = props;
let format = dateFormat;
if (showTime) {
format += ` ${timeFormat}`;
}
return format || props.format;
}
const getLabels = () => {
const labels = formatMomentValue(props.value, props.format, placeholder)
const d = moment(props.value);
const labels = formatMomentValue(d.isValid() ? d : null, getDefaultFormat(), placeholder)
return isArr(labels) ? labels.join('~') : labels
}
return <div className={cls(prefixCls, props.className)}>{getLabels()}</div>