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 1 commit
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; option?: DefaultOptionType },
Copy link
Member

Choose a reason for hiding this comment

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

option 感觉不太好,如果是多选这个就不对了。或许 nodes 会好点。

Copy link
Author

Choose a reason for hiding this comment

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

感谢意见!刚刚对应修改了函数变量名称,麻烦看看是否合理。

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.option,
} 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, option: 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, option: 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
3 changes: 3 additions & 0 deletions tests/Select.props.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ describe('TreeSelect.props', () => {
}),
],
checked: true,
option: expect.anything(),
Copy link
Member

Choose a reason for hiding this comment

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

需要填写 treeNode 的额外数据,然后对比这个值是否和 treeNode 一致。不能 anything。

Copy link
Author

Choose a reason for hiding this comment

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

感谢意见!刚刚对应添加了额外数据进行对比,麻烦看看是否合理。

preValue: [],
triggerNode: expect.anything(),
triggerValue: 'Value 0',
Expand All @@ -342,6 +343,7 @@ describe('TreeSelect.props', () => {
}),
],
checked: true,
option: expect.anything(),
preValue: [],
triggerNode: expect.anything(),
triggerValue: 'Value 0',
Expand All @@ -360,6 +362,7 @@ describe('TreeSelect.props', () => {
}),
],
checked: true,
option: expect.anything(),
preValue: [],
triggerNode: expect.anything(),
triggerValue: 'Value 0',
Expand Down
Loading