Skip to content

Commit

Permalink
feat: 隐藏数据中添加 __changeReason 字段并补充文档说明
Browse files Browse the repository at this point in the history
  • Loading branch information
2betop committed Sep 20, 2024
1 parent 10eb959 commit 0d22954
Show file tree
Hide file tree
Showing 21 changed files with 320 additions and 48 deletions.
75 changes: 75 additions & 0 deletions docs/zh-CN/concepts/datascope-and-datachain.md
Original file line number Diff line number Diff line change
Expand Up @@ -456,3 +456,78 @@ url 中的参数会进入顶层数据域,比如下面的例子,可以点击[
"body": "${word}"
}
```

## 隐藏数据

数据中还有以下字段不会被枚举到,但是可以读取:

- `__prev` 修改前的值
- `__changeReason` 修改原因
- `__changeReason.type` 修改原因类型
- `input` 用户输入
- `api` api 接口返回触发
- `formula` 公式计算触发
- `hide` 隐藏属性变化触发
- `init` 表单项初始化触发
- `action` 事件动作触发
- `__super` 数据链的上一级

> `__changeReason` 字段在 amis 6.9.0 版本开始支持
```schema
{
"data": {
"name": "amis"
},
"type": "form",
id: "form_data",
"actions": [
{
type: "button",
label: "接口获取",
actionType: "ajax",
api: {
"method": "get",
url: "/api/mock2/form/saveForm",
mockResponse: {
status: 200,
data: {
name: "amis-demo"
}
}
}
},
{
type: "button",
label: "设置值",
onEvent: {
click: {
actions: [
{
"actionType": "setValue",
"componentId": "form_data",
"args": {
"value": {
"name": "amis-demo2"
}
}
}
]
}
}
},
],
"body": [
{
type: "input-text",
name: "name",
label: "姓名"
},
{
type: "tpl",
tpl: "当前值:${name}<br />修改前的值:${__prev.name}<br />变化原因:${__changeReason|json}"
}
]
}
```
21 changes: 15 additions & 6 deletions packages/amis-core/src/WithStore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,8 @@ export function HocStoreFactory(renderer: {
...this.formatData(props.data)
}),
(props.updatePristineAfterStoreDataReInit ??
props.dataUpdatedAt !== prevProps.dataUpdatedAt) === false
props.dataUpdatedAt !== prevProps.dataUpdatedAt) === false,
props.data?.__changeReason
);
}
} else if (
Expand Down Expand Up @@ -293,7 +294,9 @@ export function HocStoreFactory(renderer: {
))
}),
(props.updatePristineAfterStoreDataReInit ??
props.dataUpdatedAt !== prevProps.dataUpdatedAt) === false
props.dataUpdatedAt !== prevProps.dataUpdatedAt) === false,

props.data?.__changeReason
);
} else if (props.data && (props.data as any).__super) {
store.initData(
Expand Down Expand Up @@ -325,13 +328,15 @@ export function HocStoreFactory(renderer: {
))
}),
(props.updatePristineAfterStoreDataReInit ??
props.dataUpdatedAt !== prevProps.dataUpdatedAt) === false
props.dataUpdatedAt !== prevProps.dataUpdatedAt) === false,
props.data?.__changeReason
);
} else {
store.initData(
createObject(props.scope, props.data),
(props.updatePristineAfterStoreDataReInit ??
props.dataUpdatedAt !== prevProps.dataUpdatedAt) === false
props.dataUpdatedAt !== prevProps.dataUpdatedAt) === false,
props.data?.__changeReason
);
}
} else if (
Expand All @@ -358,7 +363,9 @@ export function HocStoreFactory(renderer: {
(props.updatePristineAfterStoreDataReInit ??
props.dataUpdatedAt !== prevProps.dataUpdatedAt) === false ||
(store.storeType === 'FormStore' &&
prevProps.store?.storeType === 'CRUDStore')
prevProps.store?.storeType === 'CRUDStore'),

props.data?.__changeReason
);
}
// nextProps.data.__super !== props.data.__super) &&
Expand All @@ -376,7 +383,9 @@ export function HocStoreFactory(renderer: {
...store.data
}),
(props.updatePristineAfterStoreDataReInit ??
props.dataUpdatedAt !== prevProps.dataUpdatedAt) === false
props.dataUpdatedAt !== prevProps.dataUpdatedAt) === false,

props.data?.__changeReason
);
}
}
Expand Down
41 changes: 34 additions & 7 deletions packages/amis-core/src/renderers/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {
ClassName,
BaseApiObject,
SchemaExpression,
SchemaClassName
SchemaClassName,
DataChangeReason
} from '../types';
import {filter, evalExpression} from '../utils/tpl';
import getExprProperties from '../utils/filter-schema';
Expand Down Expand Up @@ -633,7 +634,9 @@ export default class Form extends React.Component<FormProps, object> {
successMessage: fetchSuccess,
errorMessage: fetchFailed,
onSuccess: (json: Payload, data: any) => {
store.setValues(data);
store.setValues(data, undefined, undefined, undefined, {
type: 'api'
});

if (
!isEffectiveApi(initAsyncApi, store.data) ||
Expand Down Expand Up @@ -968,7 +971,9 @@ export default class Form extends React.Component<FormProps, object> {
setValues(value: any, replace?: boolean) {
const {store} = this.props;
this.flush();
store.setValues(value, undefined, replace);
store.setValues(value, undefined, replace, undefined, {
type: 'action'
});
}

async submit(
Expand Down Expand Up @@ -1056,13 +1061,23 @@ export default class Form extends React.Component<FormProps, object> {
value: any,
name: string,
submit: boolean,
changePristine = false
changePristine = false,
changeReason?: DataChangeReason
) {
const {store, formLazyChange, persistDataKeys} = this.props;
if (typeof name !== 'string') {
return;
}
store.changeValue(name, value, changePristine);
store.changeValue(
name,
value,
changePristine,
undefined,
undefined,
changeReason || {
type: 'input'
}
);
if (!changePristine || typeof value !== 'undefined') {
(formLazyChange === false ? this.emitChange : this.lazyEmitChange)(
submit
Expand Down Expand Up @@ -1131,9 +1146,21 @@ export default class Form extends React.Component<FormProps, object> {
}
}

handleBulkChange(values: Object, submit: boolean) {
handleBulkChange(
values: Object,
submit: boolean,
changeReason?: DataChangeReason
) {
const {onChange, store, formLazyChange} = this.props;
store.setValues(values);
store.setValues(
values,
undefined,
undefined,
undefined,
changeReason || {
type: 'input'
}
);
// store.updateData(values);

// store.items.forEach(formItem => {
Expand Down
4 changes: 3 additions & 1 deletion packages/amis-core/src/renderers/Item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
BaseApiObject,
BaseSchemaWithoutType,
ClassName,
DataChangeReason,
Schema
} from '../types';
import {HocStoreFactory} from '../WithStore';
Expand Down Expand Up @@ -530,7 +531,8 @@ export interface FormItemProps extends RendererProps {
) => void;
onBulkChange?: (
values: {[propName: string]: any},
submitOnChange?: boolean
submitOnChange?: boolean,
changeReason?: DataChangeReason
) => void;
addHook: (
fn: Function,
Expand Down
58 changes: 50 additions & 8 deletions packages/amis-core/src/renderers/wrapControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {observer} from 'mobx-react';
import hoistNonReactStatic from 'hoist-non-react-statics';
import {withRootStore} from '../WithRootStore';
import {FormBaseControl, FormItemWrap} from './Item';
import {Api} from '../types';
import {Api, DataChangeReason} from '../types';
import {TableStore} from '../store/table';
import pick from 'lodash/pick';
import {
Expand Down Expand Up @@ -75,14 +75,19 @@ export interface ControlOutterProps extends RendererProps {
value: any,
name: string,
submit?: boolean,
changePristine?: boolean
changePristine?: boolean,
changeReason?: DataChangeReason
) => void;
formItemDispatchEvent: (type: string, data: any) => void;
formItemRef?: (control: any) => void;
}

export interface ControlProps {
onBulkChange?: (values: Object) => void;
onBulkChange?: (
values: Object,
submitOnChange?: boolean,
changeReason?: DataChangeReason
) => void;
onChange?: (value: any, name: string, submit: boolean) => void;
store: IIRendererStore;
}
Expand Down Expand Up @@ -535,7 +540,9 @@ export function wrapControl<
formItem.removeSubFormItem(this.model);

this.model.clearValueOnHidden &&
this.model.form?.deleteValueByName(this.model.name);
this.model.form?.deleteValueByName(this.model.name, {
type: 'hide'
});

isAlive(rootStore) && rootStore.removeStore(this.model);
}
Expand Down Expand Up @@ -691,7 +698,10 @@ export function wrapControl<
);
}

this.model.changeTmpValue(value, 'input');
this.model.changeTmpValue(
value,
type === 'formula' ? 'formulaChanged' : 'input'
);

if (changeImmediately || conrolChangeImmediately || !formInited) {
this.emitChange(submitOnChange);
Expand Down Expand Up @@ -772,12 +782,44 @@ export function wrapControl<
return;
}

const changeReason: DataChangeReason = {
type: 'input'
};

if (model.changeMotivation === 'formulaChanged') {
changeReason.type = 'formula';
} else if (
model.changeMotivation === 'initialValue' ||
model.changeMotivation === 'formInited' ||
model.changeMotivation === 'defaultValue'
) {
changeReason.type = 'init';
}

if (model.extraName) {
const values = model.splitExtraValue(value);
onChange?.(values[0], model.name);
onChange?.(values[1], model.extraName, submitOnChange === true);
onChange?.(
values[0],
model.name,
undefined,
undefined,
changeReason
);
onChange?.(
values[1],
model.extraName,
submitOnChange === true,
undefined,
changeReason
);
} else {
onChange?.(value, model.name, submitOnChange === true);
onChange?.(
value,
model.name,
submitOnChange === true,
undefined,
changeReason
);
}
this.checkValidate();
}
Expand Down
Loading

0 comments on commit 0d22954

Please sign in to comment.