Skip to content

Commit

Permalink
fix(dropdownSelect): discovery-151 undefined options (#149)
Browse files Browse the repository at this point in the history
* options, undefined check
* activateOptions, length check
  • Loading branch information
cdcabrera committed Sep 20, 2022
1 parent 5ecf626 commit 77052a5
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ Array [
]
`;

exports[`Select Component should allow alternate array and object options: undefined options 1`] = `Array []`;

exports[`Select Component should allow alternate direction and position options: direction up 1`] = `
Object {
"className": "quipucords-select-pf ",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ describe('Select Component', () => {
props.selectedOptions = ['world', 'ipsum'];

expect(formatOptions(props).options).toMatchSnapshot('key value object');

props.options = undefined;
props.selectedOptions = [];

expect(formatOptions(props).options).toMatchSnapshot('undefined options');
});

it('should allow plain objects as values, and be able to select options based on values within the object', async () => {
Expand Down
17 changes: 8 additions & 9 deletions src/components/dropdownSelect/dropdownSelect.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const SelectPosition = DropdownPosition;
// FixMe: attributes filtered on PF select component. allow data- attributes
/**
* Format options into a consumable array of objects format.
* Note: It is understood that for line 60'ish around "updatedOptions" we dump all values regardless
* Note: It is understood that for line 83'ish around "updatedOptions" we dump all values regardless
* of whether they are plain objects, or not, into updatedOptions. This has been done for speed only,
* one less check to perform.
*
Expand All @@ -79,14 +79,13 @@ const SelectPosition = DropdownPosition;
const formatOptions = ({ selectField = { current: null }, options, selectedOptions, variant, ...props } = {}) => {
const { current: domElement = {} } = selectField;
const dataAttributes = Object.entries(props).filter(([key]) => /^data-/i.test(key));
const updatedOptions = _isPlainObject(options)
? Object.entries(options).map(([key, value]) => ({ ...value, title: key, value }))
: _cloneDeep(options);

const updatedOptions =
(_isPlainObject(options) && Object.entries(options).map(([key, value]) => ({ ...value, title: key, value }))) ||
(options && _cloneDeep(options)) ||
[];
const isSelectedOptionsStringNumber = typeof selectedOptions === 'string' || typeof selectedOptions === 'number';
const activateOptions =
(selectedOptions && typeof selectedOptions === 'string') || typeof selectedOptions === 'number'
? [selectedOptions]
: selectedOptions;
(Array.isArray(selectedOptions) && selectedOptions) || (isSelectedOptionsStringNumber && [selectedOptions]) || [];

updatedOptions.forEach((option, index) => {
let convertedOption = option;
Expand All @@ -106,7 +105,7 @@ const formatOptions = ({ selectField = { current: null }, options, selectedOptio
convertedOption.textContent = convertedOption.textContent || convertedOption.title;
convertedOption.label = convertedOption.label || convertedOption.title;

if (activateOptions) {
if (activateOptions.length) {
let isSelected;

if (_isPlainObject(convertedOption.value)) {
Expand Down

0 comments on commit 77052a5

Please sign in to comment.