From ae6e03e306fa69c2c8443380ebf740aa494992e9 Mon Sep 17 00:00:00 2001 From: 2betop <2betop.cn@gmail.com> Date: Tue, 21 Nov 2023 20:59:09 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8DinputDate=20=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=20valueFormat=20+=20=E8=A1=A8=E8=BE=BE=E5=BC=8F?= =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=80=BC=E6=97=B6=E5=80=BC=E4=B8=8D=E5=8C=B9?= =?UTF-8?q?=E9=85=8D=20valueFormat=20Close:=20#8652?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../renderers/Form/inputDate.test.tsx | 20 +++++++++++++++++++ .../amis/src/renderers/Form/InputDate.tsx | 11 ++++++++-- .../src/renderers/Form/InputDateRange.tsx | 1 + 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/packages/amis/__tests__/renderers/Form/inputDate.test.tsx b/packages/amis/__tests__/renderers/Form/inputDate.test.tsx index cb0fab3a3..8627bb960 100644 --- a/packages/amis/__tests__/renderers/Form/inputDate.test.tsx +++ b/packages/amis/__tests__/renderers/Form/inputDate.test.tsx @@ -521,3 +521,23 @@ test('Renderer:inputDate disabledDate', async () => { expect(mondayCell).toHaveClass('rdtDisabled'); 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'); +}); diff --git a/packages/amis/src/renderers/Form/InputDate.tsx b/packages/amis/src/renderers/Form/InputDate.tsx index 8d726db22..a33521316 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 fb77d9173..baabf7718 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) {