Skip to content

Commit

Permalink
Merge pull request #119 from splunk/ADDON-34278-single-select-component
Browse files Browse the repository at this point in the history
ADDON-34278: Add singleselect component
  • Loading branch information
mchavda-splunk authored Mar 8, 2021
2 parents 83a08d1 + 1263f82 commit 609ac1d
Showing 1 changed file with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import React from 'react';
import PropTypes from 'prop-types';
import Select from '@splunk/react-ui/Select';

function SingleInputControl(props) {
const { id, field, disabled = false, value, controlOptions, ...restProps } = props;
const { autoCompleteFields } = controlOptions;

function handleChange(e, { value }) {
restProps.handleChange(id, value);
}

function generateOptions() {
const data = [];
autoCompleteFields.forEach((item) => {
if (item.value && item.label) {
data.push(<Select.Option label={item.label} value={item.value} key={item.value} />);
}
if (item.children && item.label) {
data.push(<Select.Heading key={item.label}>{item.label}</Select.Heading>);
item.children.forEach((child) => {
data.push(
<Select.Option label={child.label} value={child.value} key={child.value} />
);
});
}
});
return data;
}

return (
<Select value={value} name={field} disabled={disabled} onChange={handleChange}>
{generateOptions()}
</Select>
);
}

SingleInputControl.propTypes = {
id: PropTypes.number.isRequired,
disabled: PropTypes.bool,
value: PropTypes.string,
handleChange: PropTypes.func.isRequired,
field: PropTypes.string,
controlOptions: PropTypes.shape({
autoCompleteFields: PropTypes.array.isRequired,
}),
};

export default SingleInputControl;

0 comments on commit 609ac1d

Please sign in to comment.