-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #119 from splunk/ADDON-34278-single-select-component
ADDON-34278: Add singleselect component
- Loading branch information
Showing
1 changed file
with
49 additions
and
0 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
splunk_add_on_ucc_framework/ucc_ui_lib/src/main/webapp/components/SingleInputControl.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |