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: support option in onChange handler #589

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
24 changes: 19 additions & 5 deletions src/TreeSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import type {
DefaultValueType,
FieldNames,
LegacyDataNode,
DefaultOptionType,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

建议重命名参数以提高代码清晰度

根据之前的评审意见,建议将 option 重命名为 nodes,因为在多选模式下这个命名更准确。这样可以让接口更清晰地表达其用途。

- extra: ChangeEventExtra & { option?: DefaultOptionType }
+ extra: ChangeEventExtra & { nodes?: DefaultOptionType }

Also applies to: 49-53

} from './interface';

export interface TreeSelectProps<ValueType = any, OptionType extends DataNode = DataNode>
Expand All @@ -45,7 +46,11 @@ export interface TreeSelectProps<ValueType = any, OptionType extends DataNode =
// >>> Value
value?: ValueType;
defaultValue?: ValueType;
onChange?: (value: ValueType, labelList: React.ReactNode[], extra: ChangeEventExtra) => void;
onChange?: (
value: ValueType,
labelList: React.ReactNode[],
extra: ChangeEventExtra & { option?: DefaultOptionType },
) => void;

// >>> Search
searchValue?: string;
Expand Down Expand Up @@ -410,7 +415,7 @@ const TreeSelect = React.forwardRef<BaseSelectRef, TreeSelectProps>((props, ref)
const triggerChange = useRefFunc(
(
newRawValues: SafeKey[],
extra: { triggerValue?: SafeKey; selected?: boolean },
extra: { triggerValue?: SafeKey; selected?: boolean; nodes?: DefaultOptionType },
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

确保类型一致性

为保持代码一致性,建议将 triggerChange 函数中的 option 也更新为 nodes

- option: extra.nodes,
+ nodes: extra.nodes,

Also applies to: 464-465

source: SelectSource,
) => {
const labeledValues = convert2LabelValues(newRawValues);
Expand Down Expand Up @@ -456,7 +461,8 @@ const TreeSelect = React.forwardRef<BaseSelectRef, TreeSelectProps>((props, ref)
// [Legacy] Always return as array contains label & value
preValue: rawLabeledValues,
triggerValue,
} as ChangeEventExtra;
option: extra.nodes,
} as ChangeEventExtra & { option?: DefaultOptionType };

// [Legacy] Fill legacy data if user query.
// This is expansive that we only fill when user query
Expand Down Expand Up @@ -505,7 +511,11 @@ const TreeSelect = React.forwardRef<BaseSelectRef, TreeSelectProps>((props, ref)
// Never be falsy but keep it safe
if (!mergedMultiple) {
// Single mode always set value
triggerChange([selectedValue], { selected: true, triggerValue: selectedValue }, 'option');
triggerChange(
[selectedValue],
{ selected: true, triggerValue: selectedValue, nodes: node as DefaultOptionType },
'option',
);
} else {
let newRawValues = selected
? [...rawValues, selectedValue]
Expand Down Expand Up @@ -535,7 +545,11 @@ const TreeSelect = React.forwardRef<BaseSelectRef, TreeSelectProps>((props, ref)
...checkedKeys.map(key => keyEntities[key as SafeKey].node[mergedFieldNames.value]),
];
}
triggerChange(newRawValues, { selected, triggerValue: selectedValue }, source || 'option');
triggerChange(
newRawValues,
{ selected, triggerValue: selectedValue, nodes: node as DefaultOptionType },
source || 'option',
);
}

// Trigger select event
Expand Down
13 changes: 13 additions & 0 deletions src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@ import type { SafeKey, Key, DataNode as TreeDataNode } from 'rc-tree/lib/interfa

export type { SafeKey, Key };

export interface BaseOptionType {
disabled?: boolean;
className?: string;
title?: string;
[name: string]: any;
}

export interface DefaultOptionType extends BaseOptionType {
label?: React.ReactNode;
value?: string | number | null;
children?: Omit<DefaultOptionType, 'children'>[];
}

export interface DataNode extends Record<string, any>, Omit<TreeDataNode, 'key' | 'children'> {
key?: Key;
value?: SafeKey;
Expand Down
21 changes: 21 additions & 0 deletions tests/Select.props.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,24 @@ describe('TreeSelect.props', () => {
});

describe('showCheckedStrategy', () => {
const treeData = {
key: 'key 0',
title: 'Title 0',
value: 'Value 0',
children: [
{
key: 'key 0-0',
title: 'Title 0-0',
value: 'Value 0-0',
},
{
key: 'key 0-1',
title: 'Title 0-1',
value: 'Value 0-1',
},
],
};

const testList = [
{
strategy: SHOW_ALL,
Expand All @@ -324,6 +342,7 @@ describe('TreeSelect.props', () => {
}),
],
checked: true,
option: expect.objectContaining(treeData),
preValue: [],
triggerNode: expect.anything(),
triggerValue: 'Value 0',
Expand All @@ -342,6 +361,7 @@ describe('TreeSelect.props', () => {
}),
],
checked: true,
option: expect.objectContaining(treeData),
preValue: [],
triggerNode: expect.anything(),
triggerValue: 'Value 0',
Expand All @@ -360,6 +380,7 @@ describe('TreeSelect.props', () => {
}),
],
checked: true,
option: expect.objectContaining(treeData),
preValue: [],
triggerNode: expect.anything(),
triggerValue: 'Value 0',
Expand Down
Loading