diff --git a/packages/amis-ui/src/components/Tree.tsx b/packages/amis-ui/src/components/Tree.tsx index 1ba555a09..17c8dfcaa 100644 --- a/packages/amis-ui/src/components/Tree.tsx +++ b/packages/amis-ui/src/components/Tree.tsx @@ -240,6 +240,7 @@ export class TreeSelector extends React.Component< }; this.syncUnFolded(props); + this.flattenOptions(props, true); } componentDidMount() { @@ -247,8 +248,6 @@ export class TreeSelector extends React.Component< // onRef只有渲染器的情况才会使用 this.props.onRef?.(this); - // 初始化 - this.flattenOptions(); enableNodePath && this.expandLazyLoadNodes(); } @@ -257,6 +256,7 @@ export class TreeSelector extends React.Component< if (prevProps.options !== props.options) { this.syncUnFolded(props); + this.flattenOptions(props); } if ( @@ -326,7 +326,6 @@ export class TreeSelector extends React.Component< } }); - this.flattenOptions(); initFoldedLevel && this.forceUpdate(); return unfolded; @@ -832,7 +831,10 @@ export class TreeSelector extends React.Component< /** * TODO: this.unfolded => reaction 更加合理 */ - flattenOptions(props?: TreeSelectorProps): void | Option[] { + flattenOptions( + props?: TreeSelectorProps, + initial?: boolean + ): void | Option[] { let flattenedOptions: Option[] = []; eachTree( @@ -855,7 +857,7 @@ export class TreeSelector extends React.Component< } } ); - if (!this.state.flattenedOptions.length && flattenedOptions.length) { + if (initial) { // 初始化 this.state = {...this.state, flattenedOptions}; } else { diff --git a/packages/amis/__tests__/renderers/Form/__snapshots__/chainedSelect.test.tsx.snap b/packages/amis/__tests__/renderers/Form/__snapshots__/chainedSelect.test.tsx.snap index 7c777f378..e7ae2814c 100644 --- a/packages/amis/__tests__/renderers/Form/__snapshots__/chainedSelect.test.tsx.snap +++ b/packages/amis/__tests__/renderers/Form/__snapshots__/chainedSelect.test.tsx.snap @@ -317,7 +317,7 @@ exports[`Renderer:chained-select 1`] = `