From d7409498d4fd867b5617d0944de1439fd01b5574 Mon Sep 17 00:00:00 2001 From: meerkat Date: Tue, 27 Dec 2022 10:46:00 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=A7=A3=E5=86=B3tree=E5=BC=82=E6=AD=A5?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E4=B8=8D=E6=B8=B2=E6=9F=93=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98=20(#5980)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/amis-ui/src/components/Tree.tsx | 12 +++++++----- .../Form/__snapshots__/chainedSelect.test.tsx.snap | 2 +- .../__tests__/renderers/Form/chainedSelect.test.tsx | 9 ++++----- 3 files changed, 12 insertions(+), 11 deletions(-) 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`] = `
{ expect(getByText('C 3')).toBeInTheDocument(); fireEvent.click(getByText('C 3')); - await wait(100); - expect(fetcher).toBeCalledTimes(5); + await waitFor(() => { + expect(fetcher).toBeCalledTimes(5); + }); fireEvent.click(getByText('请选择')); expect(getByText('未找到任何结果')).toBeInTheDocument(); - await waitFor(() => { - expect(container).toMatchSnapshot(); - }); + expect(container).toMatchSnapshot(); });