Skip to content

Commit

Permalink
[5.x] Fix suggested options in Field Conditions builder (#10650)
Browse files Browse the repository at this point in the history
Co-authored-by: Jason Varga <[email protected]>
  • Loading branch information
duncanmcclean and jasonvarga authored Aug 16, 2024
1 parent 799c864 commit 78892d2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
13 changes: 11 additions & 2 deletions resources/js/components/fieldtypes/HasInputOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
};
}

Expand Down
12 changes: 11 additions & 1 deletion resources/js/tests/NormalizeInputOptions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'}
Expand All @@ -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'}
]);
});

0 comments on commit 78892d2

Please sign in to comment.