diff --git a/packages/amis-core/src/renderers/wrapControl.tsx b/packages/amis-core/src/renderers/wrapControl.tsx index 48088da1a..2db441040 100644 --- a/packages/amis-core/src/renderers/wrapControl.tsx +++ b/packages/amis-core/src/renderers/wrapControl.tsx @@ -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) { diff --git a/packages/amis-core/src/utils/formula.ts b/packages/amis-core/src/utils/formula.ts index 48b35916e..273dca10b 100644 --- a/packages/amis-core/src/utils/formula.ts +++ b/packages/amis-core/src/utils/formula.ts @@ -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 ( diff --git a/packages/amis/__tests__/renderers/Date.test.tsx b/packages/amis/__tests__/renderers/Date.test.tsx index 4549df058..3aa8a2ad2 100644 --- a/packages/amis/__tests__/renderers/Date.test.tsx +++ b/packages/amis/__tests__/renderers/Date.test.tsx @@ -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(); +});