diff --git a/packages/amis/__tests__/renderers/Form/inputDate.test.tsx b/packages/amis/__tests__/renderers/Form/inputDate.test.tsx index 5e776356c..54352a595 100644 --- a/packages/amis/__tests__/renderers/Form/inputDate.test.tsx +++ b/packages/amis/__tests__/renderers/Form/inputDate.test.tsx @@ -522,6 +522,27 @@ test('Renderer:inputDate disabledDate', async () => { expect(tuesdayCell).not.toHaveClass('rdtDisabled'); }); +test('Renderer:inputDate defaultValue with formula', async () => { + const {container} = await setup([ + { + type: 'input-date', + name: 'date', + label: '日期', + valueFormat: 'YYYY-MM-DD', + value: '${ DATE("2021-12-06 08:20:00") }' + } + ]); + + await wait(300); + const input = container.querySelector( + '.cxd-DatePicker-input' + )! as HTMLInputElement; + + expect(input).toBeInTheDocument(); + expect(input.value).toBe('2021-12-06'); + +}); + test('Renderer:inputDate setValue actions with special words', async () => { const {container, submitBtn, onSubmit, getByText} = await setup([ { diff --git a/packages/amis/src/renderers/Form/InputDate.tsx b/packages/amis/src/renderers/Form/InputDate.tsx index 34e21b4fe..b57770b00 100644 --- a/packages/amis/src/renderers/Form/InputDate.tsx +++ b/packages/amis/src/renderers/Form/InputDate.tsx @@ -4,7 +4,8 @@ import { FormControlProps, FormBaseControl, resolveEventData, - str2function + str2function, + normalizeDate } from 'amis-core'; import cx from 'classnames'; import {filterDate, isPureVariable, resolveVariableAndFilter} from 'amis-core'; @@ -404,7 +405,8 @@ export default class DateControl extends React.PureComponent< data, format, valueFormat, - utc + utc, + changeMotivation } = props; if (defaultValue && value === defaultValue) { @@ -412,6 +414,11 @@ export default class DateControl extends React.PureComponent< setPrinstineValue( (utc ? moment.utc(date) : date).format(valueFormat || format) ); + } else if (changeMotivation === 'formulaChanged' && defaultValue && value) { + const date = normalizeDate(value, valueFormat || format); + if (date && date.format(valueFormat || format) !== value) { + setPrinstineValue(date.format(valueFormat || format)); + } } let schedulesData = props.schedules; diff --git a/packages/amis/src/renderers/Form/InputDateRange.tsx b/packages/amis/src/renderers/Form/InputDateRange.tsx index f7003bec3..c93d823e7 100644 --- a/packages/amis/src/renderers/Form/InputDateRange.tsx +++ b/packages/amis/src/renderers/Form/InputDateRange.tsx @@ -179,6 +179,7 @@ export default class DateRangeControl extends React.Component { ) ); } + // todo 支持值格式的自动纠正 } componentDidUpdate(prevProps: DateRangeProps) {