Skip to content

Commit

Permalink
Merge 581a9ce into 0235c7f
Browse files Browse the repository at this point in the history
  • Loading branch information
Wxh16144 authored Jul 21, 2023
2 parents 0235c7f + 581a9ce commit eaf475d
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 eaf475d

Please sign in to comment.