Skip to content

Commit

Permalink
fix: 修复inputDate 配置 valueFormat + 表达式初始值时值不匹配 valueFormat Close: #8652
Browse files Browse the repository at this point in the history
  • Loading branch information
2betop committed Nov 21, 2023
1 parent 3e651e9 commit ae6e03e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
20 changes: 20 additions & 0 deletions packages/amis/__tests__/renderers/Form/inputDate.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
11 changes: 9 additions & 2 deletions packages/amis/src/renderers/Form/InputDate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -404,14 +405,20 @@ export default class DateControl extends React.PureComponent<
data,
format,
valueFormat,
utc
utc,
changeMotivation
} = props;

if (defaultValue && value === defaultValue) {
const date = filterDate(defaultValue, data, valueFormat || format);
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;
Expand Down
1 change: 1 addition & 0 deletions packages/amis/src/renderers/Form/InputDateRange.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ export default class DateRangeControl extends React.Component<DateRangeProps> {
)
);
}
// todo 支持值格式的自动纠正
}

componentDidUpdate(prevProps: DateRangeProps) {
Expand Down

0 comments on commit ae6e03e

Please sign in to comment.