I am using MaterializeCss and I am wondering how I get it's select box to work with reactjs
I have this
<div className="input-field col m6 lg6">
<select name="selectedUnitType" value={this.state.selectedUnitType} onChange={ (event) => this.handleChange(event) } >
{
this.props.unitTypes.map((unitTypes, i) => {
return <option key={'unitTypes-' + i} value={unitTypes.id}>{unitTypes.name}</option>;
})
}
</select>
</div>
I then have these 2 functions
validate() {
console.log(this.state);
}
handleChange(event) {
this.setState({ [event.target.name]: event.target.value });
}
handleChange should change the state and validate would be executed on my "add" button and just logs right now the state so I can see if it changed.
However materializecss does some changing of the dom and in the end the html output looks like this
<div class="select-wrapper initialized">
<span class="caret">▼</span><input type="text" class="select-dropdown" readonly="true" data-activates="select-options-de36c20b-d50b-3b4b-2349-6d9f35e507f0" value="Meat">
<ul id="select-options-de36c20b-d50b-3b4b-2349-6d9f35e507f0" class="dropdown-content select-dropdown ">
<li class=""><span>Meat</span></li>
<li class=""><span>Fruit</span></li>
</ul>
<select class="initialized">
<option value="839751d4-7ed8-4aee-b439-12c7bfb47f43">Meat</option>
<option value="6b31ef3e-ad80-40fa-899a-9523dd2260b4">Fruit</option>
</select>
</div>
so I am not really sure how to do as now it's been changed to an unordered list
Materialize will change your select input to UL and options to li.
Give a id to your div that encloses the select input and use it to trigger the text input value change event (jquery).
<div class="input-field col m6 lg6" id="selectUnitTypeDiv">
<div class="select-wrapper initialized">
<span class="caret">▼</span><input type="text" class="select-dropdown" readonly="true" data-activates="select-options-de36c20b-d50b-3b4b-2349-6d9f35e507f0" value="Meat">
<ul id="select-options-de36c20b-d50b-3b4b-2349-6d9f35e507f0" class="dropdown-content select-dropdown ">
<li class=""><span>Meat</span></li>
<li class=""><span>Fruit</span></li>
</ul>
</div>
</div>
Write the onchange event in componentDidMount of the element:
componentDidMount(){
$('#selectUnitTypeDiv .select-wrapper input[type=text]').change(function() {
this.setState({ /*whatever the variable is*/ });
});
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.