diff --git a/src/util.js b/src/util.js index e1f98b20a..449938d1d 100644 --- a/src/util.js +++ b/src/util.js @@ -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) { diff --git a/tests/Select.multiple.spec.js b/tests/Select.multiple.spec.js index c665bf109..1641ab3a9 100644 --- a/tests/Select.multiple.spec.js +++ b/tests/Select.multiple.spec.js @@ -67,4 +67,22 @@ describe('Select.multiple', () => { expect(handleFocus).toBeCalled(); }); }); + + it('OptGroup without key', () => { + expect(() => { + mount( + , + ); + }).not.toThrow(); + }); }); diff --git a/tests/util.spec.js b/tests/util.spec.js index 1c432a36c..5518141d0 100644 --- a/tests/util.spec.js +++ b/tests/util.spec.js @@ -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 = [' ', ',']; @@ -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(); + }); +});