From 0f16085032a52eaf48935013934c8d83f5e89b03 Mon Sep 17 00:00:00 2001 From: afc163 Date: Thu, 30 Dec 2021 17:56:20 +0800 Subject: [PATCH] fix: dropdownMatchSelectWidth={false} not disable virtual --- src/Select.tsx | 41 ++++++++++++++++++++++------------------- tests/Select.test.tsx | 2 ++ 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/src/Select.tsx b/src/Select.tsx index 8eba3b04b..14861395e 100644 --- a/src/Select.tsx +++ b/src/Select.tsx @@ -161,6 +161,7 @@ const Select = React.forwardRef( // Select onSelect, onDeselect, + dropdownMatchSelectWidth, // Options filterOption, @@ -547,8 +548,9 @@ const Select = React.forwardRef( }; // ========================== Context =========================== - const selectContext = React.useMemo( - () => ({ + const selectContext = React.useMemo(() => { + const realVirtual = virtual !== false && dropdownMatchSelectWidth !== false; + return { ...parsedOptions, flattenOptions: displayOptions, onActiveValue, @@ -557,26 +559,26 @@ const Select = React.forwardRef( menuItemSelectedIcon, rawValues, fieldNames: mergedFieldNames, - virtual, - listHeight, - listItemHeight, - childrenAsData, - }), - [ - parsedOptions, - displayOptions, - onActiveValue, - mergedDefaultActiveFirstOption, - onInternalSelect, - menuItemSelectedIcon, - rawValues, - mergedFieldNames, - virtual, + virtual: realVirtual, listHeight, listItemHeight, childrenAsData, - ], - ); + }; + }, [ + parsedOptions, + displayOptions, + onActiveValue, + mergedDefaultActiveFirstOption, + onInternalSelect, + menuItemSelectedIcon, + rawValues, + mergedFieldNames, + virtual, + dropdownMatchSelectWidth, + listHeight, + listItemHeight, + childrenAsData, + ]); // ========================== Warning =========================== if (process.env.NODE_ENV !== 'production') { @@ -603,6 +605,7 @@ const Select = React.forwardRef( searchValue={mergedSearchValue} onSearch={onInternalSearch} onSearchSplit={onInternalSearchSplit} + dropdownMatchSelectWidth={dropdownMatchSelectWidth} // >>> OptionList OptionList={OptionList} emptyOptions={!displayOptions.length} diff --git a/tests/Select.test.tsx b/tests/Select.test.tsx index 411c553bd..4f3ef9399 100644 --- a/tests/Select.test.tsx +++ b/tests/Select.test.tsx @@ -4,6 +4,7 @@ import React from 'react'; import { act } from 'react-dom/test-utils'; import { resetWarned } from 'rc-util/lib/warning'; import { spyElementPrototype } from 'rc-util/lib/test/domHook'; +import VirtualList from 'rc-virtual-list'; import type { SelectProps } from '../src'; import Select, { OptGroup, Option, useBaseProps } from '../src'; import focusTest from './shared/focusTest'; @@ -1273,6 +1274,7 @@ describe('Select.Basic', () => { // dropdownMatchSelectWidth is false means close virtual scroll expect(wrapper.find('.rc-select-item')).toHaveLength(options.length); + expect((wrapper.find(VirtualList).props() as any).virtual).toBe(false); }); it('virtual false also no render virtual list', () => {