diff --git a/resources/js/components/fieldtypes/HasInputOptions.js b/resources/js/components/fieldtypes/HasInputOptions.js index e2e6e3b176..e829d93644 100644 --- a/resources/js/components/fieldtypes/HasInputOptions.js +++ b/resources/js/components/fieldtypes/HasInputOptions.js @@ -12,9 +12,18 @@ export default { return _.map(options, (option) => { if (typeof option === 'object') { + let valueKey = 'value'; + let labelKey = 'label'; + + // Support both {key: '', value: ''} and {value: '', label: ''} formats. + if (option.hasOwnProperty('key')) { + valueKey = 'key'; + labelKey = 'value'; + } + return { - 'value': option.value, - 'label': __(option.label) || option.value + 'value': option[valueKey], + 'label': __(option[labelKey]) || option[valueKey] }; } diff --git a/resources/js/tests/NormalizeInputOptions.test.js b/resources/js/tests/NormalizeInputOptions.test.js index 525c35c99c..4a96af5993 100644 --- a/resources/js/tests/NormalizeInputOptions.test.js +++ b/resources/js/tests/NormalizeInputOptions.test.js @@ -46,7 +46,7 @@ it('normalizes input options with object', () => { ]); }); -it('normalizes input options with array of objects', () => { +it('normalizes input options with array of objects with value label keys', () => { expect(normalizeInputOptions([ {value: 'one', label: 'One'}, {value: 'two', label: 'Two'} @@ -55,3 +55,13 @@ it('normalizes input options with array of objects', () => { {value: 'two', label: 'Two'} ]); }); + +it('normalizes input options with array of objects with key value keys', () => { + expect(normalizeInputOptions([ + {key: 'one', value: 'One'}, + {key: 'two', value: 'Two'} + ])).toEqual([ + {value: 'one', label: 'Uno'}, + {value: 'two', label: 'Two'} + ]); +});