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 8 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
46 changes: 42 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,41 @@ 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);
}}
>
<TreeNode value="Value 0" title="Title 0">
<TreeNode value="Value 0-0" title="Title 0-0" />
<TreeNode value="Value 0-1" title="Title 0-1" />
<TreeNode value="Value 0-2" title="Title 0-2" />
</TreeNode>

<TreeNode value="Value 1" title="Title 1">
<TreeNode value="Value 1-0" title="Title 1-0" />
<TreeNode value="Value 1-1" title="Title 1-1" />
<TreeNode value="Value 1-2" title="Title 1-2" />
</TreeNode>

<TreeNode value="Value 2" title="Title 2">
<TreeNode value="Value 2-0" title="Title 2-0" />
<TreeNode value="Value 2-1" title="Title 2-1" />
<TreeNode value="Value 2-2" title="Title 2-2" />
</TreeNode>

<TreeNode value="Value 3" title="Title 3">
<TreeNode value="Value 3-0" title="Title 3-0" />
<TreeNode value="Value 3-1" title="Title 3-1" />
<TreeNode value="Value 3-2" title="Title 3-2" />
</TreeNode>
</TreeSelect>
</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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"rc-dialog": "^7.5.7",
"rc-field-form": "^1.0.0",
"rc-test": "^7.0.4",
"rc-virtual-list": "^1.1.0",
"rc-virtual-list": "^3.0.0",
"react": "^16.8.0",
"react-dom": "^16.8.0",
"typescript": "^5.0.0"
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
2 changes: 1 addition & 1 deletion tests/Select.multiple.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import TreeSelect, { TreeNode } from '../src';
import focusTest from './shared/focusTest';

describe('TreeSelect.multiple', () => {
focusTest('multiple');
focusTest(true);

const treeData = [
{ key: '0', value: '0', title: 'label0' },
Expand Down
29 changes: 29 additions & 0 deletions tests/Select.props.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import { mount } from 'enzyme';
import Tree, { TreeNode } from 'rc-tree';
import React from 'react';
import { render, fireEvent } from '@testing-library/react';

import TreeSelect, { SHOW_ALL, SHOW_CHILD, SHOW_PARENT, TreeNode as SelectNode } from '../src';

// Promisify timeout to let jest catch works
Expand Down Expand Up @@ -241,6 +243,33 @@ describe('TreeSelect.props', () => {
expect(handleSearch).toHaveBeenCalledWith('Search changed');
});

it('onPopupScroll', async () => {
const onPopupScroll = jest.fn(e => {
// Prevents React from resetting its properties:
e.persist();
});
render(
<TreeSelect
open
treeDefaultExpandAll
onPopupScroll={onPopupScroll}
treeData={new Array(10).fill(0).map((_, index) => ({
title: `Title ${index}`,
value: index,
}))}
/>,
);

fireEvent.scroll(document.querySelector('.rc-tree-select-tree-list-holder'), {
scrollY: 100,
});

expect(onPopupScroll).toHaveBeenCalled();
expect(onPopupScroll.mock.calls[0][0].target).toBe(
document.querySelector('.rc-tree-select-tree-list-holder'),
);
});

it('showArrow', () => {
const wrapper = mount(createOpenSelect({ suffixIcon: null }));
expect(wrapper.find('.rc-tree-select-arrow').length).toBeFalsy();
Expand Down
6 changes: 3 additions & 3 deletions tests/Select.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('TreeSelect.basic', () => {
jest.useRealTimers();
});

focusTest('single');
focusTest();

describe('render', () => {
const treeData = [
Expand Down Expand Up @@ -391,11 +391,11 @@ describe('TreeSelect.basic', () => {
wrapper.openSelect();
expect(wrapper.isOpen()).toBeFalsy();

const scrollTo = jest.fn();
/* const scrollTo = jest.fn();
zombieJ marked this conversation as resolved.
Show resolved Hide resolved
wrapper.find('List').instance().scrollTo = scrollTo;

wrapper.openSelect();
expect(scrollTo).toHaveBeenCalled();
expect(scrollTo).toHaveBeenCalled(); */
});
});

Expand Down
Loading
Loading