Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/OptionList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import LegacyContext from './LegacyContext';
import TreeSelectContext from './TreeSelectContext';
import type { Key, SafeKey } from './interface';
import { getAllKeys, isCheckDisabled } from './utils/valueUtil';
import { useEvent } from 'rc-util';

const HIDDEN_STYLE = {
width: 0,
Expand Down Expand Up @@ -241,8 +242,10 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
onKeyUp: () => {},
}));

const syncLoadData = useEvent(loadData);

const loadDataFun = useMemo(
() => (searchValue ? null : (loadData as any)),
() => (searchValue ? null : (syncLoadData as any)),
[searchValue, treeExpandedKeys || expandedKeys],
([preSearchValue], [nextSearchValue, nextExcludeSearchExpandedKeys]) =>
preSearchValue !== nextSearchValue && !!(nextSearchValue || nextExcludeSearchExpandedKeys),
Expand Down
46 changes: 46 additions & 0 deletions tests/Select.loadData.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* eslint-disable no-undef, react/no-multi-comp, no-console */
import React from 'react';
import { render, fireEvent, act } from '@testing-library/react';

import TreeSelect from '../src';

describe('TreeSelect.loadData', () => {
it('keep sync', async () => {
const Demo = () => {
const [treeData, setTreeData] = React.useState([
{
title: '0',
value: 0,
isLeaf: false,
},
]);

const loadData = async () => {
const nextId = treeData.length;

setTreeData([
...treeData,
{
title: `${nextId}`,
value: nextId,
isLeaf: false,
},
]);
};

return <TreeSelect open treeData={treeData} loadData={loadData} />;
};

render(<Demo />);

for (let i = 0; i < 5; i += 1) {
fireEvent.click(document.querySelector('.rc-tree-select-tree-switcher_close'));
await act(async () => {
await Promise.resolve();
});
expect(
document.querySelectorAll('.rc-tree-select-tree-list .rc-tree-select-tree-treenode'),
).toHaveLength(2 + i);
}
});
});
Loading