Skip to content

Commit 92c383c

Browse files
authored
Merge f43fc2f into 3bcd96a
2 parents 3bcd96a + f43fc2f commit 92c383c

File tree

5 files changed

+93
-35
lines changed

5 files changed

+93
-35
lines changed

Diff for: src/TreeSelect.tsx

+46-34
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
1-
import * as React from 'react';
2-
import { BaseSelect } from 'rc-select';
3-
import type { IconType } from 'rc-tree/lib/interface';
4-
import type { ExpandAction } from 'rc-tree/lib/Tree';
51
import type {
6-
BaseSelectRef,
7-
BaseSelectPropsWithoutPrivate,
82
BaseSelectProps,
3+
BaseSelectPropsWithoutPrivate,
4+
BaseSelectRef,
95
SelectProps,
106
} from 'rc-select';
11-
import { conductCheck } from 'rc-tree/lib/utils/conductUtil';
7+
import { BaseSelect } from 'rc-select';
128
import useId from 'rc-select/lib/hooks/useId';
9+
import type { IconType } from 'rc-tree/lib/interface';
10+
import type { ExpandAction } from 'rc-tree/lib/Tree';
11+
import { conductCheck } from 'rc-tree/lib/utils/conductUtil';
1312
import useMergedState from 'rc-util/lib/hooks/useMergedState';
13+
import warning from 'rc-util/lib/warning';
14+
import * as React from 'react';
15+
import useCache from './hooks/useCache';
16+
import useCheckedKeys from './hooks/useCheckedKeys';
17+
import useDataEntities from './hooks/useDataEntities';
18+
import useFilterTreeData from './hooks/useFilterTreeData';
19+
import useRefFunc from './hooks/useRefFunc';
20+
import useTreeData from './hooks/useTreeData';
21+
import LegacyContext from './LegacyContext';
1422
import OptionList from './OptionList';
1523
import TreeNode from './TreeNode';
16-
import { formatStrategyValues, SHOW_ALL, SHOW_PARENT, SHOW_CHILD } from './utils/strategyUtil';
17-
import type { CheckedStrategy } from './utils/strategyUtil';
18-
import TreeSelectContext from './TreeSelectContext';
1924
import type { TreeSelectContextProps } from './TreeSelectContext';
20-
import LegacyContext from './LegacyContext';
21-
import useTreeData from './hooks/useTreeData';
22-
import { toArray, fillFieldNames, isNil } from './utils/valueUtil';
23-
import useCache from './hooks/useCache';
24-
import useRefFunc from './hooks/useRefFunc';
25-
import useDataEntities from './hooks/useDataEntities';
25+
import TreeSelectContext from './TreeSelectContext';
2626
import { fillAdditionalInfo, fillLegacyProps } from './utils/legacyUtil';
27-
import useCheckedKeys from './hooks/useCheckedKeys';
28-
import useFilterTreeData from './hooks/useFilterTreeData';
27+
import type { CheckedStrategy } from './utils/strategyUtil';
28+
import { formatStrategyValues, SHOW_ALL, SHOW_CHILD, SHOW_PARENT } from './utils/strategyUtil';
29+
import { fillFieldNames, isNil, toArray } from './utils/valueUtil';
2930
import warningProps from './utils/warningPropsUtil';
30-
import warning from 'rc-util/lib/warning';
3131

3232
export type OnInternalSelect = (value: RawValueType, info: { selected: boolean }) => void;
3333

@@ -105,7 +105,7 @@ export interface LegacyDataNode extends DefaultOptionType {
105105
}
106106
export interface TreeSelectProps<
107107
ValueType = any,
108-
OptionType extends BaseOptionType = DefaultOptionType
108+
OptionType extends BaseOptionType = DefaultOptionType,
109109
> extends Omit<BaseSelectPropsWithoutPrivate, 'mode'> {
110110
prefixCls?: string;
111111
id?: string;
@@ -194,7 +194,7 @@ const TreeSelect = React.forwardRef<BaseSelectRef, TreeSelectProps>((props, ref)
194194
treeNodeFilterProp = 'value',
195195

196196
// Selector
197-
showCheckedStrategy = SHOW_CHILD,
197+
showCheckedStrategy,
198198
treeNodeLabelProp,
199199

200200
// Mode
@@ -246,6 +246,15 @@ const TreeSelect = React.forwardRef<BaseSelectRef, TreeSelectProps>((props, ref)
246246

247247
const [internalValue, setInternalValue] = useMergedState(defaultValue, { value });
248248

249+
// `multiple` && `!treeCheckable` should be show all
250+
const mergedShowCheckedStrategy = React.useMemo(() => {
251+
if (!treeCheckable) {
252+
return SHOW_ALL;
253+
}
254+
255+
return showCheckedStrategy || SHOW_CHILD;
256+
}, [showCheckedStrategy, treeCheckable]);
257+
249258
// ========================== Warning ===========================
250259
if (process.env.NODE_ENV !== 'production') {
251260
warningProps(props);
@@ -357,7 +366,9 @@ const TreeSelect = React.forwardRef<BaseSelectRef, TreeSelectProps>((props, ref)
357366
rawDisabled = entity.node.disabled;
358367
} else if (rawLabel === undefined) {
359368
// We try to find in current `labelInValue` value
360-
const labelInValueItem = toLabeledValues(internalValue).find(labeledItem => labeledItem.value === rawValue);
369+
const labelInValueItem = toLabeledValues(internalValue).find(
370+
labeledItem => labeledItem.value === rawValue,
371+
);
361372
rawLabel = labelInValueItem.label;
362373
}
363374

@@ -373,10 +384,10 @@ const TreeSelect = React.forwardRef<BaseSelectRef, TreeSelectProps>((props, ref)
373384
);
374385

375386
// =========================== Values ===========================
376-
const rawMixedLabeledValues = React.useMemo(() => toLabeledValues(internalValue), [
377-
toLabeledValues,
378-
internalValue,
379-
]);
387+
const rawMixedLabeledValues = React.useMemo(
388+
() => toLabeledValues(internalValue),
389+
[toLabeledValues, internalValue],
390+
);
380391

381392
// Split value into full check and half check
382393
const [rawLabeledValues, rawHalfLabeledValues] = React.useMemo(() => {
@@ -395,9 +406,10 @@ const TreeSelect = React.forwardRef<BaseSelectRef, TreeSelectProps>((props, ref)
395406
}, [rawMixedLabeledValues]);
396407

397408
// const [mergedValues] = useCache(rawLabeledValues);
398-
const rawValues = React.useMemo(() => rawLabeledValues.map(item => item.value), [
399-
rawLabeledValues,
400-
]);
409+
const rawValues = React.useMemo(
410+
() => rawLabeledValues.map(item => item.value),
411+
[rawLabeledValues],
412+
);
401413

402414
// Convert value to key. Will fill missed keys for conduct check.
403415
const [rawCheckedValues, rawHalfCheckedValues] = useCheckedKeys(
@@ -412,7 +424,7 @@ const TreeSelect = React.forwardRef<BaseSelectRef, TreeSelectProps>((props, ref)
412424
// Collect keys which need to show
413425
const displayKeys = formatStrategyValues(
414426
rawCheckedValues,
415-
showCheckedStrategy,
427+
mergedShowCheckedStrategy,
416428
keyEntities,
417429
mergedFieldNames,
418430
);
@@ -447,7 +459,7 @@ const TreeSelect = React.forwardRef<BaseSelectRef, TreeSelectProps>((props, ref)
447459
rawCheckedValues,
448460
rawLabeledValues,
449461
convert2LabelValues,
450-
showCheckedStrategy,
462+
mergedShowCheckedStrategy,
451463
keyEntities,
452464
]);
453465

@@ -474,7 +486,7 @@ const TreeSelect = React.forwardRef<BaseSelectRef, TreeSelectProps>((props, ref)
474486
if (treeConduction) {
475487
const formattedKeyList = formatStrategyValues(
476488
newRawValues,
477-
showCheckedStrategy,
489+
mergedShowCheckedStrategy,
478490
keyEntities,
479491
mergedFieldNames,
480492
);
@@ -743,9 +755,9 @@ if (process.env.NODE_ENV !== 'production') {
743755
TreeSelect.displayName = 'TreeSelect';
744756
}
745757

746-
const GenericTreeSelect = (TreeSelect as unknown) as (<
758+
const GenericTreeSelect = TreeSelect as unknown as (<
747759
ValueType = any,
748-
OptionType extends BaseOptionType | DefaultOptionType = DefaultOptionType
760+
OptionType extends BaseOptionType | DefaultOptionType = DefaultOptionType,
749761
>(
750762
props: React.PropsWithChildren<TreeSelectProps<ValueType, OptionType>> & {
751763
ref?: React.Ref<BaseSelectRef>;

Diff for: tests/Select.multiple.spec.js

+32-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/* eslint-disable no-undef */
2-
import React from 'react';
2+
import { render } from '@testing-library/react';
33
import { mount } from 'enzyme';
44
import KeyCode from 'rc-util/lib/KeyCode';
5+
import React from 'react';
56
import TreeSelect, { TreeNode } from '../src';
67
import focusTest from './shared/focusTest';
78

@@ -279,4 +280,34 @@ describe('TreeSelect.multiple', () => {
279280
expect(onChange).toHaveBeenCalledWith([], expect.anything(), expect.anything());
280281
expect(onDeselect).toHaveBeenCalledWith('not-exist', undefined);
281282
});
283+
284+
it('should not omit value', () => {
285+
const { container } = render(
286+
<TreeSelect
287+
value={['child1', 'child2', 'parent']}
288+
multiple
289+
treeData={[
290+
{
291+
label: 'parent',
292+
value: 'parent',
293+
children: [
294+
{
295+
label: 'child1',
296+
value: 'child1',
297+
},
298+
{
299+
label: 'child2',
300+
value: 'child2',
301+
},
302+
],
303+
},
304+
]}
305+
/>,
306+
);
307+
308+
const values = Array.from(
309+
container.querySelectorAll('.rc-tree-select-selection-item-content'),
310+
).map(ele => ele.textContent);
311+
expect(values).toEqual(['child1', 'child2', 'parent']);
312+
});
282313
});

Diff for: tests/__snapshots__/Select.checkable.spec.tsx.snap

+4
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ exports[`TreeSelect.checkable uncheck remove by selector not treeCheckStrictly 1
105105
aria-controls="rc_select_TEST_OR_SSR_list"
106106
aria-expanded="true"
107107
aria-haspopup="listbox"
108+
aria-label="Search"
108109
aria-owns="rc_select_TEST_OR_SSR_list"
109110
autocomplete="off"
110111
class="rc-tree-select-selection-search-input"
@@ -297,6 +298,7 @@ exports[`TreeSelect.checkable uncheck remove by selector not treeCheckStrictly 2
297298
aria-controls="rc_select_TEST_OR_SSR_list"
298299
aria-expanded="true"
299300
aria-haspopup="listbox"
301+
aria-label="Search"
300302
aria-owns="rc_select_TEST_OR_SSR_list"
301303
autocomplete="off"
302304
class="rc-tree-select-selection-search-input"
@@ -573,6 +575,7 @@ exports[`TreeSelect.checkable uncheck remove by tree check 1`] = `
573575
aria-controls="rc_select_TEST_OR_SSR_list"
574576
aria-expanded="true"
575577
aria-haspopup="listbox"
578+
aria-label="Search"
576579
aria-owns="rc_select_TEST_OR_SSR_list"
577580
autocomplete="off"
578581
class="rc-tree-select-selection-search-input"
@@ -765,6 +768,7 @@ exports[`TreeSelect.checkable uncheck remove by tree check 2`] = `
765768
aria-controls="rc_select_TEST_OR_SSR_list"
766769
aria-expanded="true"
767770
aria-haspopup="listbox"
771+
aria-label="Search"
768772
aria-owns="rc_select_TEST_OR_SSR_list"
769773
autocomplete="off"
770774
class="rc-tree-select-selection-search-input"

Diff for: tests/__snapshots__/Select.multiple.spec.js.snap

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ exports[`TreeSelect.multiple can hide search box by showSearch = false 1`] = `
2323
aria-controls="rc_select_TEST_OR_SSR_list"
2424
aria-expanded="false"
2525
aria-haspopup="listbox"
26+
aria-label="Search"
2627
aria-owns="rc_select_TEST_OR_SSR_list"
2728
autocomplete="off"
2829
class="rc-tree-select-selection-search-input"

Diff for: tests/__snapshots__/Select.spec.tsx.snap

+10
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ exports[`TreeSelect.basic render renders TreeNode correctly 1`] = `
1515
aria-controls="rc_select_TEST_OR_SSR_list"
1616
aria-expanded="true"
1717
aria-haspopup="listbox"
18+
aria-label="Search"
1819
aria-owns="rc_select_TEST_OR_SSR_list"
1920
autocomplete="off"
2021
class="rc-tree-select-selection-search-input"
@@ -208,6 +209,7 @@ exports[`TreeSelect.basic render renders TreeNode correctly with falsy child 1`]
208209
aria-controls="rc_select_TEST_OR_SSR_list"
209210
aria-expanded="true"
210211
aria-haspopup="listbox"
212+
aria-label="Search"
211213
aria-owns="rc_select_TEST_OR_SSR_list"
212214
autocomplete="off"
213215
class="rc-tree-select-selection-search-input"
@@ -410,6 +412,7 @@ exports[`TreeSelect.basic render renders correctly 1`] = `
410412
aria-controls="rc_select_TEST_OR_SSR_list"
411413
aria-expanded="false"
412414
aria-haspopup="listbox"
415+
aria-label="Search"
413416
aria-owns="rc_select_TEST_OR_SSR_list"
414417
autocomplete="off"
415418
class="awesome-selection-search-input"
@@ -452,6 +455,7 @@ exports[`TreeSelect.basic render renders disabled correctly 1`] = `
452455
aria-controls="rc_select_TEST_OR_SSR_list"
453456
aria-expanded="false"
454457
aria-haspopup="listbox"
458+
aria-label="Search"
455459
aria-owns="rc_select_TEST_OR_SSR_list"
456460
autocomplete="off"
457461
class="rc-tree-select-selection-search-input"
@@ -495,6 +499,7 @@ exports[`TreeSelect.basic render renders tree correctly 1`] = `
495499
aria-controls="rc_select_TEST_OR_SSR_list"
496500
aria-expanded="false"
497501
aria-haspopup="listbox"
502+
aria-label="Search"
498503
aria-owns="rc_select_TEST_OR_SSR_list"
499504
autocomplete="off"
500505
class="rc-tree-select-selection-search-input"
@@ -538,6 +543,7 @@ exports[`TreeSelect.basic render renders treeDataSimpleMode correctly 1`] = `
538543
aria-controls="rc_select_TEST_OR_SSR_list"
539544
aria-expanded="true"
540545
aria-haspopup="listbox"
546+
aria-label="Search"
541547
aria-owns="rc_select_TEST_OR_SSR_list"
542548
autocomplete="off"
543549
class="rc-tree-select-selection-search-input"
@@ -677,6 +683,7 @@ exports[`TreeSelect.basic search nodes check tree changed by filter 1`] = `
677683
aria-controls="rc_select_TEST_OR_SSR_list"
678684
aria-expanded="true"
679685
aria-haspopup="listbox"
686+
aria-label="Search"
680687
aria-owns="rc_select_TEST_OR_SSR_list"
681688
autocomplete="off"
682689
class="rc-tree-select-selection-search-input"
@@ -784,6 +791,7 @@ exports[`TreeSelect.basic search nodes check tree changed by filter 2`] = `
784791
aria-controls="rc_select_TEST_OR_SSR_list"
785792
aria-expanded="true"
786793
aria-haspopup="listbox"
794+
aria-label="Search"
787795
aria-owns="rc_select_TEST_OR_SSR_list"
788796
autocomplete="off"
789797
class="rc-tree-select-selection-search-input"
@@ -917,6 +925,7 @@ exports[`TreeSelect.basic search nodes filter node but not remove then 1`] = `
917925
aria-controls="rc_select_TEST_OR_SSR_list"
918926
aria-expanded="true"
919927
aria-haspopup="listbox"
928+
aria-label="Search"
920929
aria-owns="rc_select_TEST_OR_SSR_list"
921930
autocomplete="off"
922931
class="rc-tree-select-selection-search-input"
@@ -1049,6 +1058,7 @@ exports[`TreeSelect.basic search nodes renders search input 1`] = `
10491058
aria-controls="rc_select_TEST_OR_SSR_list"
10501059
aria-expanded="true"
10511060
aria-haspopup="listbox"
1061+
aria-label="Search"
10521062
aria-owns="rc_select_TEST_OR_SSR_list"
10531063
autocomplete="off"
10541064
class="rc-tree-select-selection-search-input"

0 commit comments

Comments
 (0)