Skip to content
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
5 changes: 4 additions & 1 deletion src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ export function getValuePropValue(child) {
if (child.key) {
return child.key;
}
throw new Error(`no key or value for ${child}`);
if (child.type && child.type.isSelectOptGroup && props.label) {
return props.label;
}
throw new Error(`no key or value or label(only for OptGroup) for ${child}`);
}

export function getPropValue(child, prop) {
Expand Down
18 changes: 18 additions & 0 deletions tests/Select.multiple.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,22 @@ describe('Select.multiple', () => {
expect(handleFocus).toBeCalled();
});
});

it('OptGroup without key', () => {
expect(() => {
mount(
<Select
multiple
defaultValue={['1']}
>
<OptGroup label="group1">
<Option value="1">One</Option>
</OptGroup>
<OptGroup label="group2">
<Option value="2">Two</Option>
</OptGroup>
</Select>,
);
}).not.toThrow();
});
});
26 changes: 25 additions & 1 deletion tests/util.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable no-undef */
import { includesSeparators, splitBySeparators } from '../src/util';
import { includesSeparators, splitBySeparators, getValuePropValue } from '../src/util';

describe('includesSeparators', () => {
const separators = [' ', ','];
Expand Down Expand Up @@ -38,3 +38,27 @@ describe('splitBySeparators', () => {
expect(splitBySeparators(string, separators)).toEqual([]);
});
});

describe('getValuePropValue', () => {
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});

afterEach(() => {
errorSpy.mockReset();
});

afterAll(() => {
errorSpy.mockRestore();
});

it('return label when type is isSelectOptGroup', () => {
expect(getValuePropValue({
props: {
label: 'Manager',
},
type: {
isSelectOptGroup: true,
},
})).toBe('Manager');
expect(errorSpy).not.toHaveBeenCalled();
});
});