Merge pull request #8842 from 2betop/fix-date-defaultValue

fix: 修复inputDate 配置 valueFormat + 表达式初始值时值不匹配 valueFormat Close: #8652
This commit is contained in:
hsm-lv 2023-11-23 19:54:22 +08:00 committed by GitHub
commit af68cce8d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 2 deletions

View File

@ -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([
{

View File

@ -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;

View File

@ -179,6 +179,7 @@ export default class DateRangeControl extends React.Component<DateRangeProps> {
)
);
}
// todo 支持值格式的自动纠正
}
componentDidUpdate(prevProps: DateRangeProps) {