-
Notifications
You must be signed in to change notification settings - Fork 194
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
nathanlao
wants to merge
2
commits into
react-component:master
Choose a base branch
from
nathanlao:option-onchange-in-tree-select
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+577
−469
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; nodes?: 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. 🛠️ Refactor suggestion 确保类型一致性 为保持代码一致性,建议将 - option: extra.nodes,
+ nodes: extra.nodes, Also applies to: 464-465 |
||
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.nodes, | ||
} 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, nodes: 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, nodes: node as DefaultOptionType }, | ||
source || 'option', | ||
); | ||
} | ||
|
||
// Trigger select event | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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