Background
The ComboBox control allows users to select an item from a dropdown list. To perform a custom action when a user clicks the control, attach a native JavaScript click event listener to its hostElement.
Steps to Complete
- Import the ComboBox component and styles.
- Define the click handler and register the listener.
- Create the ComboBox.
Getting Started
Import the ComboBox component and styles
import { ComboBox } from '@mescius/wijmo.react.input';
import '@mescius/wijmo.styles/wijmo.css';
- The component import provides the React ComboBox wrapper, and the stylesheet supplies its default appearance.
Define the click handler and register the listener
function handleClick() {
console.log('ComboBox clicked');
}
function initializeComboBox(comboBox) {
comboBox.hostElement.addEventListener(
'click',
handleClick,
true
);
}
- The
initializeComboBoxfunction receives the initialized ComboBox instance. ItshostElementproperty provides access to the DOM element that hosts the control. -
Calling
addEventListenerattaches the click handler to that element. The third argument,true, registers the listener for the capture phase, allowing it to receive clicks as they travel toward elements inside the control. - Replace the console message with the action your application needs to perform.
Create the ComboBox
export default function App() {
return (
<div className="App">
<ComboBox
itemsSource={['US', 'Germany', 'UK', 'Japan']}
initialized={initializeComboBox}
/>
</div>
);
}
- The
itemsSourceproperty supplies the dropdown items. Theinitializedcallback connects the control to the function that registers its click listener.
The complete App.jsx file is shown below:
import { ComboBox } from '@mescius/wijmo.react.input';
import '@mescius/wijmo.styles/wijmo.css';
function handleClick() {
console.log('ComboBox clicked');
}
function initializeComboBox(comboBox) {
comboBox.hostElement.addEventListener(
'click',
handleClick,
true
);
}
export default function App() {
return (
<div className="App">
<ComboBox
itemsSource={['US', 'Germany', 'UK', 'Japan']}
initialized={initializeComboBox}
/>
</div>
);
}
- This listener handles clicks within the ComboBox’s host element, including its input and dropdown button. It does not indicate that the selected item changed. Use
selectedIndexChangedwhen your action should respond to a selection change.
With this setup, the ComboBox responds to clicks through a native JavaScript event listener attached to its host element, allowing you to perform custom actions when users interact with the control.
Happy coding!
Andrew Peterson
Technical Engagement Engineer