Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Supports onPopupScroll props #507

Merged
merged 10 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ online example: https://tree-select-react-component.vercel.app/
|dropdownMatchSelectWidth | whether dropdown's with is same with select. Default set `min-width` same as input | bool | true |
|dropdownClassName | additional className applied to dropdown | String | - |
|dropdownStyle | additional style applied to dropdown | Object | {} |
|dropdownPopupAlign | specify alignment for dropdown (alignConfig of [dom-align](https://github.com/yiminghe/dom-align)) | Object | - |
|onDropdownVisibleChange | control dropdown visible | function | `() => { return true; }` |
|notFoundContent | specify content to show when no result matches. | String | 'Not Found' |
|showSearch | whether show search input in single mode | bool | true |
Expand All @@ -75,6 +74,7 @@ online example: https://tree-select-react-component.vercel.app/
|onSelect | called when select treeNode | function(value, node, extra) | - |
|onSearch | called when input changed | function | - |
|onTreeExpand | called when tree node expand | function(expandedKeys) | - |
|onPopupScroll | called when popup scroll | function(event) | - |
|showCheckedStrategy | `TreeSelect.SHOW_ALL`: show all checked treeNodes (Include parent treeNode). `TreeSelect.SHOW_PARENT`: show checked treeNodes (Just show parent treeNode). Default just show child. | enum{TreeSelect.SHOW_ALL, TreeSelect.SHOW_PARENT, TreeSelect.SHOW_CHILD } | TreeSelect.SHOW_CHILD |
|treeIcon | show tree icon | bool | false |
|treeLine | show tree line | bool | false |
Expand Down
22 changes: 18 additions & 4 deletions examples/basic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,9 @@ class Demo extends React.Component {
});
}}
onSelect={this.onSelect}
onPopupScroll={evt => {
console.log('[ onPopupScroll evt ] ===>', evt);
}}
/>

<h2>single select (just select children)</h2>
Expand Down Expand Up @@ -256,10 +259,10 @@ class Demo extends React.Component {
choiceTransitionName="rc-tree-select-selection__choice-zoom"
style={{ width: 300 }}
// dropdownStyle={{ height: 200, overflow: 'auto' }}
dropdownPopupAlign={{
overflow: { adjustY: 0, adjustX: 0 },
offset: [0, 2],
}}
// dropdownPopupAlign={{
// overflow: { adjustY: 0, adjustX: 0 },
// offset: [0, 2],
// }}
onDropdownVisibleChange={this.onDropdownVisibleChange}
placeholder={<i>请下拉选择</i>}
treeLine
Expand Down Expand Up @@ -389,6 +392,17 @@ class Demo extends React.Component {
treeData={gData}
treeTitleRender={node => node.label + 'ok'}
/>

<h2 style={{ marginTop: 140 }}>onPopupScroll</h2>
<TreeSelect
open
style={{ width: 300 }}
treeData={gData}
treeDefaultExpandAll
onPopupScroll={evt => {
console.log('[ onPopupScroll evt ] ===>', evt);
}}
/>
</div>
);
}
Expand Down
8 changes: 4 additions & 4 deletions examples/filter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ class Demo extends React.Component {
transitionName="rc-tree-select-dropdown-slide-up"
choiceTransitionName="rc-tree-select-selection__choice-zoom"
// dropdownStyle={{ height: 200, overflow: 'auto' }}
dropdownPopupAlign={{
overflow: { adjustY: 0, adjustX: 0 },
offset: [0, 2],
}}
// dropdownPopupAlign={{
// overflow: { adjustY: 0, adjustX: 0 },
// offset: [0, 2],
// }}
placeholder={<i>请下拉选择</i>}
treeLine
maxTagTextLength={10}
Expand Down
5 changes: 5 additions & 0 deletions src/OptionList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
dropdownMatchSelectWidth,
treeExpandAction,
treeTitleRender,
onPopupScroll,
} = React.useContext(TreeSelectContext);

const {
Expand All @@ -70,6 +71,7 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,

const memoTreeData = useMemo(
() => treeData,
// eslint-disable-next-line react-hooks/exhaustive-deps
[open, treeData],
(prev, next) => next[0] && prev[1] !== next[1],
);
Expand Down Expand Up @@ -97,6 +99,7 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
treeRef.current?.scrollTo({ key: checkedKeys[0] });
setActiveKey(checkedKeys[0]);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [open]);

// ========================== Search ==========================
Expand All @@ -123,6 +126,7 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
if (searchValue) {
setSearchExpandedKeys(getAllKeys(treeData, fieldNames));
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [searchValue]);

const onInternalExpand = (keys: Key[]) => {
Expand Down Expand Up @@ -252,6 +256,7 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
onLoad={onTreeLoad}
filterTreeNode={filterTreeNode}
expandAction={treeExpandAction}
onScroll={onPopupScroll}
/>
</div>
);
Expand Down
4 changes: 4 additions & 0 deletions src/TreeSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ const TreeSelect = React.forwardRef<BaseSelectRef, TreeSelectProps>((props, ref)
treeMotion,
treeTitleRender,

onPopupScroll,
...restProps
} = props;

Expand Down Expand Up @@ -465,6 +466,7 @@ const TreeSelect = React.forwardRef<BaseSelectRef, TreeSelectProps>((props, ref)
...item,
label: item.label ?? item.value,
}));
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [
mergedFieldNames,
mergedMultiple,
Expand Down Expand Up @@ -681,6 +683,7 @@ const TreeSelect = React.forwardRef<BaseSelectRef, TreeSelectProps>((props, ref)
onSelect: onOptionSelect,
treeExpandAction,
treeTitleRender,
onPopupScroll,
}),
[
virtual,
Expand All @@ -693,6 +696,7 @@ const TreeSelect = React.forwardRef<BaseSelectRef, TreeSelectProps>((props, ref)
onOptionSelect,
treeExpandAction,
treeTitleRender,
onPopupScroll,
],
);

Expand Down
1 change: 1 addition & 0 deletions src/TreeSelectContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface TreeSelectContextProps {
onSelect: OnInternalSelect;
treeExpandAction?: ExpandAction;
treeTitleRender?: (node: any) => React.ReactNode;
onPopupScroll?: React.UIEventHandler<HTMLDivElement>;
}

const TreeSelectContext = React.createContext<TreeSelectContextProps>(null as any);
Expand Down
15 changes: 15 additions & 0 deletions tests/Select.props.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,21 @@ describe('TreeSelect.props', () => {
expect(handleSearch).toHaveBeenCalledWith('Search changed');
});

it('onPopupScroll', () => {
const handleScroll = jest.fn();
const wrapper = mount(
<TreeSelect open onPopupScroll={handleScroll}>
<SelectNode value="Value 0" title="Title 0" key="key 0">
<SelectNode value="Value 0-0" title="Title 0-0" key="key 0-0" />
<SelectNode value="Value 0-1" title="Title 0-1" key="key 0-1" />
<SelectNode value="Value 0-2" title="Title 0-2" key="key 0-2" />
</SelectNode>
</TreeSelect>,
);

expect(wrapper.find('List').props().onScroll).toBeTruthy();
wanpan11 marked this conversation as resolved.
Show resolved Hide resolved
});

it('showArrow', () => {
const wrapper = mount(createOpenSelect({ suffixIcon: null }));
expect(wrapper.find('.rc-tree-select-arrow').length).toBeFalsy();
Expand Down
Loading