fix: now表达式作为form组件默认值时,增加特殊逻辑,处理数据更新 (#5783)

This commit is contained in:
meerkat 2023-03-10 15:44:02 +08:00 committed by GitHub
parent d79f35c22c
commit 6433561545
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 80 additions and 3 deletions

View File

@ -18,7 +18,8 @@ import {
isNeedFormula,
isExpression,
FormulaExec,
replaceExpression
replaceExpression,
isNowFormula
} from '../utils/formula';
import {IIRendererStore, IRendererStore} from '../store';
import {ScopedContext, IScopedContext} from '../Scoped';
@ -362,11 +363,17 @@ export function wrapControl<
typeof props.defaultValue !== 'undefined' &&
isExpression(props.defaultValue)
) {
const nowFormulaChecked = isNowFormula(props.defaultValue);
// 渲染器中的 defaultValue 优先(备注: SchemaRenderer中会将 value 改成 defaultValue
if (
!isEqual(props.defaultValue, prevProps.defaultValue) ||
(props.data !== prevProps.data &&
isNeedFormula(props.defaultValue, props.data, prevProps.data))
(isNeedFormula(
props.defaultValue,
props.data,
prevProps.data
) ||
nowFormulaChecked))
) {
const curResult = FormulaExec['formula'](
props.defaultValue,
@ -385,6 +392,13 @@ export function wrapControl<
if (props.onChange) {
props.onChange(curResult, model.name, false);
}
} else if (nowFormulaChecked) {
const nowData = props.data[model.name];
// now 表达式,计算后的值永远相同
model.changeTmpValue(nowData);
if (props.onChange) {
props.onChange(nowData, model.name, false);
}
}
}
} else if (model) {

View File

@ -238,6 +238,11 @@ export function isNeedFormula(
}
}
export function isNowFormula(expression: string): boolean {
const block = expression.split(/\${|\||}/).filter(item => item);
return block[1] === 'now';
}
// 将 \${xx} 替换成 ${xx}
export function replaceExpression(expression: any): any {
if (

View File

@ -1,9 +1,10 @@
import React = require('react');
import {render} from '@testing-library/react';
import {render, fireEvent} from '@testing-library/react';
import '../../src';
import {render as amisRender} from '../../src';
import {makeEnv} from '../helper';
import moment from 'moment';
import {act} from 'react-test-renderer';
test('Renderer:date', async () => {
const {container} = render(
@ -31,3 +32,60 @@ test('Renderer:date', async () => {
expect(container).toMatchSnapshot();
});
test('Renderer:date reset', async () => {
const {container, getByText} = render(
amisRender(
{
type: 'form',
title: '表单',
body: [
{
label:
'本月第一天$ {_|now|dateModify:startOf:month|date:YYYY-MM-DD}',
type: 'input-date',
name: 'month',
value: '${_|now|dateModify:startOf:month|date:YYYY-MM-DD}',
minDate: '',
maxDate: '',
format: 'YYYY-MM-DD',
inputFormat: 'YYYY-MM-DD',
valueFormat: 'YYYY-MM-DD'
},
{
type: 'reset',
label: '重置',
actionType: 'reset',
dialog: {
title: '系统提示',
body: '对你点击了'
},
id: 'u:862532a9f698',
level: 'dark'
}
],
id: 'u:7c281facfdbb'
},
{},
makeEnv({})
)
);
const inputElement = container.querySelector('input[type="text"]') as any;
const defaultValue = inputElement?.value;
// 打开日期选择器
fireEvent.click(inputElement);
// 更换月份
fireEvent.click(container.querySelector('[icon="right-arrow"]')!);
// 选择一个新的日期
fireEvent.click(container.querySelector('td[data-value="1"]')!);
// 新选的日期与旧的不同
expect(inputElement?.value !== defaultValue).toBeTruthy();
// 点击重制
await act(() => {
fireEvent.click(container.querySelector('button[type="reset"]')!);
});
// 重制后的日期 等于初始化的日期
expect(inputElement?.value === defaultValue).toBeTruthy();
});