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

fix: select option never use key in jsx declare usage #1889

Merged
merged 2 commits into from
Nov 1, 2023
Merged
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
2 changes: 2 additions & 0 deletions packages/semi-foundation/select/foundation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -939,6 +939,8 @@ export default class SelectFoundation extends BaseFoundation<SelectAdapter> {
delete newOption._show;
delete newOption._selected;
delete newOption._scrollIndex;
delete newOption._keyInJsx;

if ('_keyInOptionList' in newOption) {
newOption.key = newOption._keyInOptionList;
delete newOption._keyInOptionList;
Expand Down
18 changes: 18 additions & 0 deletions packages/semi-ui/select/_story/select.stories.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3367,3 +3367,21 @@ class VirtualizeAllowCreate extends React.Component {

// virtualize allowCreate + renderCreateItem, optionList render not as expected
export const Fix1856 = () => (<VirtualizeAllowCreate />);


export const TestOptionKey = () => {
return <><Select style={{ width: 300 }}>
<Select.Option label='3' value='2' key='abc'></Select.Option>
<Select.Option label='2' value='3' key='efg'></Select.Option>
<Select.Option label='4' value='5'></Select.Option>
<Select.Option label='5' value='4'></Select.Option>
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

before:
image

after:
image

</Select>
<br/><br/>
<Select style={{ width: 300 }} optionList={[
{ label: '1', value: '2', key: 'kkk' },
{ label: '2', value: '3', key: 'jjj' },
{ label: '3', value: '2' },
]}>
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

still same
image

</Select>
</>
}
18 changes: 9 additions & 9 deletions packages/semi-ui/select/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,14 @@ export type SelectProps = {
showRestTagsPopover?: boolean;
restTagsPopoverProps?: PopoverProps
} & Pick<
TooltipProps,
| 'spacing'
| 'getPopupContainer'
| 'motion'
| 'autoAdjustOverflow'
| 'mouseLeaveDelay'
| 'mouseEnterDelay'
| 'stopPropagation'
TooltipProps,
| 'spacing'
| 'getPopupContainer'
| 'motion'
| 'autoAdjustOverflow'
| 'mouseLeaveDelay'
| 'mouseEnterDelay'
| 'stopPropagation'
> & React.RefAttributes<any>;

export interface SelectState {
Expand Down Expand Up @@ -775,7 +775,7 @@ class Select extends BaseComponent<SelectProps, SelectState> {
focused={isFocused}
onMouseEnter={() => this.onOptionHover(optionIndex)}
style={optionStyle}
key={option.key || option.label as string + option.value as string + optionIndex}
key={option._keyInOptionList || option._keyInJsx || option.label as string + option.value as string + optionIndex}
renderOptionItem={renderOptionItem}
inputValue={inputValue}
semiOptionId={`${this.selectID}-option-${optionIndex}`}
Expand Down
8 changes: 7 additions & 1 deletion packages/semi-ui/select/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,20 @@ const generateOption = (child: React.ReactElement, parent: any, index: number):
}
const option = {
value: childProps.value,
// Drop-down menu rendering priority label value, children, value in turn downgrade
// Dropdown menu rendering priority label value, children, value in turn downgrade
label: childProps.label || childProps.children || childProps.value,
_show: true,
_selected: false,
_scrollIndex: index,
...childProps,
_parentGroup: parent,
};

// Props are collected from ReactNode, after React.Children.toArray
// no need to determine whether the key exists in child
// Even if the user does not explicitly declare it, React will always generate a key.
option._keyInJsx = child.key;

return option;
};

Expand Down