-
Notifications
You must be signed in to change notification settings - Fork 195
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,7 @@ import type { | |
DefaultValueType, | ||
FieldNames, | ||
LegacyDataNode, | ||
DefaultOptionType, | ||
} from './interface'; | ||
|
||
export interface TreeSelectProps<ValueType = any, OptionType extends DataNode = DataNode> | ||
|
@@ -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; | ||
|
@@ -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 }, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 感谢意见!刚刚对应修改了函数变量名称,麻烦看看是否合理。 |
||
source: SelectSource, | ||
) => { | ||
const labeledValues = convert2LabelValues(newRawValues); | ||
|
@@ -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 | ||
|
@@ -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] | ||
|
@@ -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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -324,6 +324,7 @@ describe('TreeSelect.props', () => { | |
}), | ||
], | ||
checked: true, | ||
option: expect.anything(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 需要填写 treeNode 的额外数据,然后对比这个值是否和 treeNode 一致。不能 anything。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 感谢意见!刚刚对应添加了额外数据进行对比,麻烦看看是否合理。 |
||
preValue: [], | ||
triggerNode: expect.anything(), | ||
triggerValue: 'Value 0', | ||
|
@@ -342,6 +343,7 @@ describe('TreeSelect.props', () => { | |
}), | ||
], | ||
checked: true, | ||
option: expect.anything(), | ||
preValue: [], | ||
triggerNode: expect.anything(), | ||
triggerValue: 'Value 0', | ||
|
@@ -360,6 +362,7 @@ describe('TreeSelect.props', () => { | |
}), | ||
], | ||
checked: true, | ||
option: expect.anything(), | ||
preValue: [], | ||
triggerNode: expect.anything(), | ||
triggerValue: 'Value 0', | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
建议重命名参数以提高代码清晰度
根据之前的评审意见,建议将
option
重命名为nodes
,因为在多选模式下这个命名更准确。这样可以让接口更清晰地表达其用途。Also applies to: 49-53