Skip to content

Commit

Permalink
feat: autoFill api 模式也支持 initAutoFill 配置 Close: baidu#9631 baidu#9716 b…
Browse files Browse the repository at this point in the history
  • Loading branch information
2betop committed Mar 4, 2024
1 parent cbe6297 commit c03f026
Show file tree
Hide file tree
Showing 6 changed files with 348 additions and 200 deletions.
20 changes: 16 additions & 4 deletions packages/amis-core/src/renderers/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -762,8 +762,14 @@ export default class Form extends React.Component<FormProps, object> {
const initedAt = store.initedAt;

store.setInited(true);
const hooks: Array<(data: any) => Promise<any>> = this.hooks['init'] || [];
await Promise.all(hooks.map(hook => hook(data)));
const hooks: Array<(data: any) => Promise<any>> = (
this.hooks['init'] || []
).sort((a: any, b: any) => a.__weight - b.__weight);

// 有步骤依赖
for (let hook of hooks) {
await hook(data);
}

if (!isAlive(store)) {
return;
Expand Down Expand Up @@ -976,9 +982,15 @@ export default class Form extends React.Component<FormProps, object> {
store.reset(onReset);
}

addHook(fn: () => any, type: 'validate' | 'init' | 'flush' = 'validate') {
addHook(
fn: () => any,
type: 'validate' | 'init' | 'flush' = 'validate',
weight = 0
) {
this.hooks[type] = this.hooks[type] || [];
this.hooks[type].push(type === 'flush' ? fn : promisify(fn));
const hook = type === 'flush' ? fn : promisify(fn);
(hook as any).__weight = weight;
this.hooks[type].push(hook);
return () => {
this.removeHook(fn, type);
fn = noop;
Expand Down

0 comments on commit c03f026

Please sign in to comment.