Skip to content

Commit

Permalink
fix: Select label React Node warning some case (#946) (#961)
Browse files Browse the repository at this point in the history
* fix: Select label React Node warning some case

* test: test driven

* fix: warning of label when update

* chore: clean up

* chore: back of raw

---------

Co-authored-by: 洋 <hetongyang@bytedance.com>
Co-authored-by: 二货机器人 <smith3816@gmail.com>
(cherry picked from commit a5d4b75)

# Conflicts:
#	src/Select.tsx

Co-authored-by: 洋 <94534613+BoyYangzai@users.noreply.github.com>
  • Loading branch information
Wxh16144 and BoyYangzai committed Jul 21, 2023
1 parent 0235c7f commit 305b3dc
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 12 deletions.
7 changes: 6 additions & 1 deletion src/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,12 @@ const Select = React.forwardRef(
// Warning if label not same as provided
if (process.env.NODE_ENV !== 'production' && !optionLabelProp) {
const optionLabel = option?.[mergedFieldNames.label];
if (optionLabel !== undefined && optionLabel !== rawLabel) {
if (
optionLabel !== undefined &&
!React.isValidElement(optionLabel) &&
!React.isValidElement(rawLabel) &&
optionLabel !== rawLabel
) {
warning(false, '`label` of `value` is not same as `label` in Select options.');
}
}
Expand Down
60 changes: 49 additions & 11 deletions tests/Select.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1417,17 +1417,55 @@ describe('Select.Basic', () => {
expect(onKeyUp).toHaveBeenCalled();
});

it('warning if label not same as option', () => {
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
mount(
<Select value={{ value: '2', label: 'One' }} labelInValue>
<Option value="2">Two</Option>
</Select>,
);
expect(errorSpy).toHaveBeenCalledWith(
'Warning: `label` of `value` is not same as `label` in Select options.',
);
errorSpy.mockRestore();
describe('warning if label not same as option', () => {
it('should work', () => {
resetWarned();

const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
mount(
<Select value={{ value: '2', label: 'One' }} labelInValue>
<Option value="2">Two</Option>
</Select>,
);
expect(errorSpy).toHaveBeenCalledWith(
'Warning: `label` of `value` is not same as `label` in Select options.',
);
errorSpy.mockRestore();
});

it('not warning for react node', () => {
resetWarned();
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});

const Demo = () => {
const [, setVal] = React.useState(0);

return (
<Select
onChange={setVal}
defaultValue={0}
options={[
{
value: 0,
label: <div />,
},
{
value: 1,
label: <div />,
},
]}
/>
);
};

const wrapper = mount(<Demo />);

toggleOpen(wrapper);
selectItem(wrapper, 1);

expect(errorSpy).not.toHaveBeenCalled();
errorSpy.mockRestore();
});
});

describe('warning if use `props` to read data', () => {
Expand Down

0 comments on commit 305b3dc

Please sign in to comment.