mirror of
https://gitee.com/baidu/amis.git
synced 2024-12-04 21:08:55 +08:00
fix: 修复日期默认值优先级过高的问题 (#7573)
This commit is contained in:
parent
69582f8d88
commit
33cd873f89
@ -18,8 +18,7 @@ import {
|
|||||||
isNeedFormula,
|
isNeedFormula,
|
||||||
isExpression,
|
isExpression,
|
||||||
FormulaExec,
|
FormulaExec,
|
||||||
replaceExpression,
|
replaceExpression
|
||||||
isNowFormula
|
|
||||||
} from '../utils/formula';
|
} from '../utils/formula';
|
||||||
import {IIRendererStore, IRendererStore} from '../store';
|
import {IIRendererStore, IRendererStore} from '../store';
|
||||||
import {ScopedContext, IScopedContext} from '../Scoped';
|
import {ScopedContext, IScopedContext} from '../Scoped';
|
||||||
@ -363,41 +362,30 @@ export function wrapControl<
|
|||||||
} else if (
|
} else if (
|
||||||
model &&
|
model &&
|
||||||
typeof props.defaultValue !== 'undefined' &&
|
typeof props.defaultValue !== 'undefined' &&
|
||||||
isExpression(props.defaultValue)
|
isExpression(props.defaultValue) &&
|
||||||
) {
|
(!isEqual(props.defaultValue, prevProps.defaultValue) ||
|
||||||
let nowFormulaChecked = false;
|
|
||||||
// 渲染器中的 defaultValue 优先(备注: SchemaRenderer中会将 value 改成 defaultValue)
|
|
||||||
if (
|
|
||||||
!isEqual(props.defaultValue, prevProps.defaultValue) ||
|
|
||||||
(props.data !== prevProps.data &&
|
(props.data !== prevProps.data &&
|
||||||
(isNeedFormula(
|
isNeedFormula(
|
||||||
props.defaultValue,
|
props.defaultValue,
|
||||||
props.data,
|
props.data,
|
||||||
prevProps.data
|
prevProps.data
|
||||||
) ||
|
)))
|
||||||
(nowFormulaChecked = isNowFormula(props.defaultValue))))
|
) {
|
||||||
|
const curResult = FormulaExec['formula'](
|
||||||
|
props.defaultValue,
|
||||||
|
props.data
|
||||||
|
);
|
||||||
|
const prevResult = FormulaExec['formula'](
|
||||||
|
prevProps.defaultValue,
|
||||||
|
prevProps.data
|
||||||
|
);
|
||||||
|
if (
|
||||||
|
!isEqual(curResult, prevResult) &&
|
||||||
|
!isEqual(curResult, model.tmpValue)
|
||||||
) {
|
) {
|
||||||
const curResult = FormulaExec['formula'](
|
// 识别上下文变动、自身数值变动、公式运算结果变动
|
||||||
props.defaultValue,
|
model.changeTmpValue(curResult, 'formulaChanged');
|
||||||
props.data
|
props.onChange?.(curResult, model.name, false);
|
||||||
);
|
|
||||||
const prevResult = FormulaExec['formula'](
|
|
||||||
prevProps.defaultValue,
|
|
||||||
prevProps.data
|
|
||||||
);
|
|
||||||
if (
|
|
||||||
!isEqual(curResult, prevResult) &&
|
|
||||||
!isEqual(curResult, model.tmpValue)
|
|
||||||
) {
|
|
||||||
// 识别上下文变动、自身数值变动、公式运算结果变动
|
|
||||||
model.changeTmpValue(curResult, 'formulaChanged');
|
|
||||||
props.onChange?.(curResult, model.name, false);
|
|
||||||
} else if (nowFormulaChecked) {
|
|
||||||
const nowData = props.data[model.name];
|
|
||||||
// now 表达式,计算后的值永远相同
|
|
||||||
model.changeTmpValue(nowData, 'formulaChanged');
|
|
||||||
props.onChange?.(nowData, model.name, false);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else if (model) {
|
} else if (model) {
|
||||||
const valueByName = getVariable(props.data, model.name);
|
const valueByName = getVariable(props.data, model.name);
|
||||||
|
@ -225,7 +225,8 @@ export function isNeedFormula(
|
|||||||
const variables = FormulaExec.collect(expression);
|
const variables = FormulaExec.collect(expression);
|
||||||
return variables.some(
|
return variables.some(
|
||||||
(variable: string) =>
|
(variable: string) =>
|
||||||
FormulaExec.var(variable, prevData) !== FormulaExec.var(variable, curData)
|
FormulaExec.var(variable, prevData) !==
|
||||||
|
FormulaExec.var(variable, curData)
|
||||||
);
|
);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.warn(
|
console.warn(
|
||||||
@ -238,11 +239,6 @@ export function isNeedFormula(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isNowFormula(expression: string): boolean {
|
|
||||||
const block = expression.split(/\${|\||}/).filter(item => item);
|
|
||||||
return block[1] === 'now';
|
|
||||||
}
|
|
||||||
|
|
||||||
// 将 \${xx} 替换成 ${xx}
|
// 将 \${xx} 替换成 ${xx}
|
||||||
export function replaceExpression(expression: any): any {
|
export function replaceExpression(expression: any): any {
|
||||||
if (
|
if (
|
||||||
|
@ -427,7 +427,7 @@ test('doAction:form reload default', async () => {
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
await wait(200); // 等待 initApi 加载完
|
await wait(500); // 等待 initApi 加载完
|
||||||
expect(
|
expect(
|
||||||
(container.querySelector('[name="author"]') as HTMLInputElement).value
|
(container.querySelector('[name="author"]') as HTMLInputElement).value
|
||||||
).toEqual('fex');
|
).toEqual('fex');
|
||||||
@ -527,7 +527,7 @@ test('doAction:form reload with data', async () => {
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
await wait(200);
|
await wait(500);
|
||||||
const author: HTMLInputElement = container.querySelector('[name="author"]')!;
|
const author: HTMLInputElement = container.querySelector('[name="author"]')!;
|
||||||
expect(author).toBeInTheDocument();
|
expect(author).toBeInTheDocument();
|
||||||
fireEvent.change(author, {
|
fireEvent.change(author, {
|
||||||
@ -698,7 +698,7 @@ test('doAction:form clear', async () => {
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
await wait(200);
|
await wait(500);
|
||||||
await waitFor(() => {
|
await waitFor(() => {
|
||||||
expect(getByText('清空表单')).toBeInTheDocument();
|
expect(getByText('清空表单')).toBeInTheDocument();
|
||||||
});
|
});
|
||||||
|
@ -96,7 +96,7 @@ test('EventAction:inputRange', async () => {
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
await wait(200);
|
await wait(500);
|
||||||
const inputs = container.querySelector('.cxd-InputRange-input input')!;
|
const inputs = container.querySelector('.cxd-InputRange-input input')!;
|
||||||
|
|
||||||
// input change
|
// input change
|
||||||
|
@ -2,7 +2,7 @@ import React = require('react');
|
|||||||
import {render, fireEvent} from '@testing-library/react';
|
import {render, fireEvent} from '@testing-library/react';
|
||||||
import '../../src';
|
import '../../src';
|
||||||
import {render as amisRender} from '../../src';
|
import {render as amisRender} from '../../src';
|
||||||
import {makeEnv} from '../helper';
|
import {makeEnv, wait} from '../helper';
|
||||||
import moment from 'moment';
|
import moment from 'moment';
|
||||||
import {act} from 'react-test-renderer';
|
import {act} from 'react-test-renderer';
|
||||||
|
|
||||||
@ -89,3 +89,44 @@ test('Renderer:date reset', async () => {
|
|||||||
// 重制后的日期 等于初始化的日期
|
// 重制后的日期 等于初始化的日期
|
||||||
expect(inputElement?.value === defaultValue).toBeTruthy();
|
expect(inputElement?.value === defaultValue).toBeTruthy();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('Renderer:date defaultValue', async () => {
|
||||||
|
const fetcher = jest.fn().mockImplementation(() =>
|
||||||
|
Promise.resolve({
|
||||||
|
data: {
|
||||||
|
status: 0,
|
||||||
|
data: {updateTime: 1680255708}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
const {container, getByText} = render(
|
||||||
|
amisRender(
|
||||||
|
{
|
||||||
|
type: 'form',
|
||||||
|
initApi: {
|
||||||
|
url: '/amis/initData',
|
||||||
|
method: 'GET'
|
||||||
|
},
|
||||||
|
title: '编辑',
|
||||||
|
body: [
|
||||||
|
{
|
||||||
|
type: 'input-date',
|
||||||
|
label: '日期',
|
||||||
|
name: 'updateTime',
|
||||||
|
format: 'YYYY-MM-DD',
|
||||||
|
value: '${NOW()}'
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{},
|
||||||
|
makeEnv({
|
||||||
|
fetcher
|
||||||
|
})
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
await wait(500);
|
||||||
|
const inputElement = container.querySelector('input[type="text"]') as any;
|
||||||
|
expect(inputElement?.value).toBe('2023-03-31'); // 默认值的优先级没有接口返回的高,所以应该是 2023-03-31
|
||||||
|
});
|
||||||
|
@ -78,7 +78,7 @@ test('Renderer:input-formula', async () => {
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
await wait(200);
|
await wait(500);
|
||||||
expect(container).toMatchSnapshot();
|
expect(container).toMatchSnapshot();
|
||||||
|
|
||||||
await findByDisplayValue('SUM(1 + 2)');
|
await findByDisplayValue('SUM(1 + 2)');
|
||||||
@ -180,7 +180,7 @@ test('Renderer:input-formula button', async () => {
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
await wait(200);
|
await wait(500);
|
||||||
expect(container).toMatchSnapshot();
|
expect(container).toMatchSnapshot();
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -260,6 +260,6 @@ test('Renderer:input-formula input-group', async () => {
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
await wait(200);
|
await wait(500);
|
||||||
expect(container).toMatchSnapshot();
|
expect(container).toMatchSnapshot();
|
||||||
});
|
});
|
||||||
|
@ -62,7 +62,7 @@ test('Renderer:input table', async () => {
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
await wait(300);
|
await wait(500);
|
||||||
replaceReactAriaIds(container);
|
replaceReactAriaIds(container);
|
||||||
expect(container).toMatchSnapshot();
|
expect(container).toMatchSnapshot();
|
||||||
});
|
});
|
||||||
@ -178,7 +178,7 @@ test('Renderer:input table add', async () => {
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
await wait(200);
|
await wait(500);
|
||||||
const add = await findByText(/新增/);
|
const add = await findByText(/新增/);
|
||||||
|
|
||||||
fireEvent.click(add);
|
fireEvent.click(add);
|
||||||
|
Loading…
Reference in New Issue
Block a user