fix: 解决tree异步数据不渲染的问题 (#5980)

This commit is contained in:
meerkat 2022-12-27 10:46:00 +08:00 committed by GitHub
parent 9d18904eef
commit d7409498d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 11 deletions

View File

@ -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 {

View File

@ -317,7 +317,7 @@ exports[`Renderer:chained-select 1`] = `
</span>
<div
class="cxd-PopOver cxd-Select-popover cxd-PopOver--leftBottomLeftTop"
style="display: block; width: auto; left: 0px; top: 0px; position: relative;"
style="display: block; width: 0px; left: 0px; top: 0px; position: relative;"
theme="cxd"
>
<div

View File

@ -113,12 +113,11 @@ test('Renderer:chained-select', async () => {
expect(getByText('C 3')).toBeInTheDocument();
fireEvent.click(getByText('C 3'));
await wait(100);
await waitFor(() => {
expect(fetcher).toBeCalledTimes(5);
});
fireEvent.click(getByText('请选择'));
expect(getByText('未找到任何结果')).toBeInTheDocument();
await waitFor(() => {
expect(container).toMatchSnapshot();
});
});