diff --git a/CHANGELOG.md b/CHANGELOG.md index ec85a16072e..65314a89091 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ - Updated `tokenKeyword` to match the definition of keyword field type ([#5251](https://github.com/elastic/eui/pull/5251)) - Added `element`, `buttonElement`, and `arrowProps` props to further customize `EuiAccordion` ([#5258](https://github.com/elastic/eui/pull/5258)) +- Fixed missing `id` for `EuiCombobox` by generating one if `prepend` or `append` exists ([#5229](https://github.com/elastic/eui/pull/5229)) **Breaking changes** diff --git a/src-docs/src/views/combo_box/combo_box_example.js b/src-docs/src/views/combo_box/combo_box_example.js index bfafa4b18c1..44fc6237d7e 100644 --- a/src-docs/src/views/combo_box/combo_box_example.js +++ b/src-docs/src/views/combo_box/combo_box_example.js @@ -75,8 +75,10 @@ const groupsSnippet = ``; import SingleSelection from './single_selection'; +import SingleSelectionPrepend from './single_selection_prepend'; const singleSelectionSource = require('!!raw-loader!./single_selection'); const singleSelectionHtml = renderToHtml(SingleSelection); +const singleSelectionPrependSource = require('!!raw-loader!./single_selection_prepend'); const singleSelectionSnippet = ``; +const singleSelectionPrependSnippet = ``; import SingleSelectionCustomOptions from './single_selection_custom_options'; const singleSelectionCustomOptionsSource = require('!!raw-loader!./single_selection_custom_options'); @@ -402,19 +411,39 @@ export const ComboBoxExample = { {'singleSelection={{ asPlainText: true }}'}

+ + ), + props: { EuiComboBox, EuiComboBoxOptionOption }, + snippet: singleSelectionSnippet, + demo: , + }, + { + title: 'Single selection with prepended label', + source: [ + { + type: GuideSectionTypes.JS, + code: singleSelectionPrependSource, + }, + { + type: GuideSectionTypes.HTML, + code: singleSelectionHtml, + }, + ], + text: ( +

- Note: append and{' '} - prepend props only work if + append and prepend props only + work if singleSelection prop is not set to{' '} - false to avoid multilines that makes combobox + false to avoid multi-lines that makes combobox height greater than that of append and{' '} prepend.

), props: { EuiComboBox, EuiComboBoxOptionOption }, - snippet: singleSelectionSnippet, - demo: , + snippet: singleSelectionPrependSnippet, + demo: , }, { title: 'Single selection with custom options', diff --git a/src-docs/src/views/combo_box/single_selection.js b/src-docs/src/views/combo_box/single_selection.js index 9293e94e3f6..e8212a6bb76 100644 --- a/src-docs/src/views/combo_box/single_selection.js +++ b/src-docs/src/views/combo_box/single_selection.js @@ -51,9 +51,9 @@ export default () => { canDisabled={false} canReadOnly={false} canLoading={false} - canPrepend={false} canIsDisabled canAppend + canPrepend > { + const [selectedOptions, setSelected] = useState([options[2]]); + + const onChange = (selectedOptions) => { + // We should only get back either 0 or 1 options. + setSelected(selectedOptions); + }; + + return ( + + ); +}; diff --git a/src/components/combo_box/__snapshots__/combo_box.test.tsx.snap b/src/components/combo_box/__snapshots__/combo_box.test.tsx.snap index 87ebdc89ef7..a31cea0afc6 100644 --- a/src/components/combo_box/__snapshots__/combo_box.test.tsx.snap +++ b/src/components/combo_box/__snapshots__/combo_box.test.tsx.snap @@ -27,6 +27,7 @@ exports[`EuiComboBox is rendered 1`] = ` `; +exports[`props custom ID is rendered 1`] = ` +
+ +
+`; + exports[`props delimiter is rendered 1`] = `
@@ -716,6 +770,7 @@ exports[`props singleSelection prepend and append is rendered 1`] = ` compressed={false} fullWidth={false} hasSelectedOptions={false} + id="generated-id__eui-combobox-id" inputRef={[Function]} isListOpen={true} noIcon={false} @@ -845,6 +900,7 @@ exports[`props singleSelection selects existing option when opened 1`] = ` compressed={false} fullWidth={false} hasSelectedOptions={true} + id="generated-id__eui-combobox-id" inputRef={[Function]} isListOpen={true} noIcon={false} diff --git a/src/components/combo_box/combo_box.test.tsx b/src/components/combo_box/combo_box.test.tsx index cafdf0faaa1..5e1be4e4815 100644 --- a/src/components/combo_box/combo_box.test.tsx +++ b/src/components/combo_box/combo_box.test.tsx @@ -92,6 +92,18 @@ describe('props', () => { expect(component).toMatchSnapshot(); }); + test('custom ID is rendered', () => { + const component = shallow( + + ); + + expect(component).toMatchSnapshot(); + }); + describe('isClearable=false disallows user from clearing input', () => { test('when no options are selected', () => { const component = shallow( diff --git a/src/components/combo_box/combo_box.tsx b/src/components/combo_box/combo_box.tsx index fb460d58e9a..e493eedc20c 100644 --- a/src/components/combo_box/combo_box.tsx +++ b/src/components/combo_box/combo_box.tsx @@ -936,6 +936,9 @@ export class EuiComboBox extends Component< matchingOptions, } = this.state; + // Make sure we have a valid ID if users don't pass one as a prop + const inputId = id ?? this.rootId('_eui-combobox-id'); + // Visually indicate the combobox is in an invalid state if it has lost focus but there is text entered in the input. // When custom options are disabled and the user leaves the combo box after entering text that does not match any // options, this tells the user that they've entered invalid input. @@ -946,6 +949,8 @@ export class EuiComboBox extends Component< const classes = classNames('euiComboBox', className, { 'euiComboBox--compressed': compressed, 'euiComboBox--fullWidth': fullWidth, + 'euiComboBox--prepended': prepend, + 'euiComboBox--appended': append, 'euiComboBox-isDisabled': isDisabled, 'euiComboBox-isInvalid': markAsInvalid, 'euiComboBox-isOpen': isListOpen, @@ -1030,7 +1035,7 @@ export class EuiComboBox extends Component< } fullWidth={fullWidth} hasSelectedOptions={selectedOptions.length > 0} - id={id} + id={inputId} inputRef={this.searchInputRefCallback} isDisabled={isDisabled} isListOpen={isListOpen} diff --git a/src/components/combo_box/combo_box_input/combo_box_input.tsx b/src/components/combo_box/combo_box_input/combo_box_input.tsx index 08fb4c57e3d..8200f0adb3d 100644 --- a/src/components/combo_box/combo_box_input/combo_box_input.tsx +++ b/src/components/combo_box/combo_box_input/combo_box_input.tsx @@ -264,6 +264,7 @@ export class EuiComboBoxInput extends Component<