Skip to content

Commit

Permalink
chore: 优化编辑器中修改表单项 name 效果,可在预览区及时看到效果 (baidu#9557)
Browse files Browse the repository at this point in the history
  • Loading branch information
2betop committed Jan 30, 2024
1 parent df1e09e commit 8e002ec
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 33 deletions.
73 changes: 40 additions & 33 deletions packages/amis-core/src/renderers/wrapControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -241,39 +241,7 @@ export function wrapControl<
model.changeTmpValue(propValue, 'controlled');
model.setIsControlled(true);
} else {
const isExp = isExpression(value);

if (isExp) {
model.changeTmpValue(
FormulaExec['formula'](value, data), // 对组件默认值进行运算
'formulaChanged'
);
} else {
let initialValue = model.extraName
? [
getVariable(data, model.name, form?.canAccessSuperData),
getVariable(
data,
model.extraName,
form?.canAccessSuperData
)
]
: getVariable(data, model.name, form?.canAccessSuperData);

if (
model.extraName &&
initialValue.every((item: any) => item === undefined)
) {
initialValue = undefined;
}

model.changeTmpValue(
initialValue ?? replaceExpression(value),
typeof initialValue !== 'undefined'
? 'initialValue'
: 'defaultValue'
);
}
this.setInitialValue(value);
}

if (
Expand Down Expand Up @@ -350,6 +318,7 @@ export function wrapControl<

changedEffect(
[
'name',
'id',
'validations',
'validationErrors',
Expand Down Expand Up @@ -383,6 +352,10 @@ export function wrapControl<
isValueSchemaExp: isExpression(props.$schema.value),
inputGroupControl: props?.inputGroupControl
} as any);

if (changes.hasOwnProperty('name')) {
this.setInitialValue(this.props.$schema.value);
}
}
);

Expand Down Expand Up @@ -493,6 +466,40 @@ export function wrapControl<
this.disposeModel();
}

setInitialValue(value: any) {
const model = this.model!;
const {formStore: form, data} = this.props;
const isExp = isExpression(value);

if (isExp) {
model.changeTmpValue(
FormulaExec['formula'](value, data), // 对组件默认值进行运算
'formulaChanged'
);
} else {
let initialValue = model.extraName
? [
getVariable(data, model.name, form?.canAccessSuperData),
getVariable(data, model.extraName, form?.canAccessSuperData)
]
: getVariable(data, model.name, form?.canAccessSuperData);

if (
model.extraName &&
initialValue.every((item: any) => item === undefined)
) {
initialValue = undefined;
}

model.changeTmpValue(
initialValue ?? replaceExpression(value),
typeof initialValue !== 'undefined'
? 'initialValue'
: 'defaultValue'
);
}
}

disposeModel() {
const {formStore: form, formItem, rootStore} = this.props;

Expand Down
3 changes: 3 additions & 0 deletions packages/amis-core/src/store/formItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ export const FormItemStore = StoreNode.named('FormItemStore')
let loadAutoUpdateCancel: Function | null = null;

function config({
name,
extraName,
required,
unique,
Expand All @@ -345,6 +346,7 @@ export const FormItemStore = StoreNode.named('FormItemStore')
inputGroupControl,
pagination
}: {
name?: string;
extraName?: string;
required?: boolean;
unique?: boolean;
Expand Down Expand Up @@ -383,6 +385,7 @@ export const FormItemStore = StoreNode.named('FormItemStore')
rules = str2rules(rules);
}

typeof name !== 'undefined' && (self.name = name);
typeof extraName !== 'undefined' && (self.extraName = extraName);
typeof type !== 'undefined' && (self.type = type);
typeof id !== 'undefined' && (self.itemId = id);
Expand Down

0 comments on commit 8e002ec

Please sign in to comment.