admin管理员组文章数量:1401640
I want multiple material-ui sliders in one react ponent sharing a mon event handler. However, to make this work, I would need to identify the originating slider. From the API documentation I can't see how that is achieved. I've tried applying id
and name
attributes to the <Slider>
-ponent, yet I'm not seeing these in the synthesized event in the event handler.
handleChange = (event, value) => {
console.log(event); // 'Id' and 'name' attributes in 'target' are empty
this.setState({ value });
};
render() {
const { classes } = this.props;
const { value } = this.state;
return (
<div className={classes.root}>
<Typography id="label">Slider label</Typography>
<Slider
classes={{ container: classes.slider }}
value={value}
aria-labelledby="label"
onChange={this.handleChange}
/>
</div>
);
}
This is fetched from the official demo project:
Any help would be greatly appreciated!
I want multiple material-ui sliders in one react ponent sharing a mon event handler. However, to make this work, I would need to identify the originating slider. From the API documentation I can't see how that is achieved. I've tried applying id
and name
attributes to the <Slider>
-ponent, yet I'm not seeing these in the synthesized event in the event handler.
handleChange = (event, value) => {
console.log(event); // 'Id' and 'name' attributes in 'target' are empty
this.setState({ value });
};
render() {
const { classes } = this.props;
const { value } = this.state;
return (
<div className={classes.root}>
<Typography id="label">Slider label</Typography>
<Slider
classes={{ container: classes.slider }}
value={value}
aria-labelledby="label"
onChange={this.handleChange}
/>
</div>
);
}
This is fetched from the official demo project:
https://codesandbox.io/s/4j9l9xn1o4
Any help would be greatly appreciated!
Share Improve this question asked Dec 4, 2018 at 11:21 conciliatorconciliator 6,1386 gold badges43 silver badges71 bronze badges2 Answers
Reset to default 6You can format your state like so:
state = {
slider1: 50, //slider1 is the name of the first slider
slider2: 50, //slider2 is the name of the second slider
}
After that, you have 2 ways to set the state when the value of the slider is changed:
(Update: This method doesn't work! However I will leave it here for future reference) By using HTML attribute
id
, then access it by usingevent.target.id
. The wholehandleChange
method would look like this:handleChange = (e, value) => { this.setState({ [e.target.id]: value }); }
By passing then name of the slider straight to the
handleChange
method, and it would be like this:handleChange = name => (e, value) => { this.setState({ [name]: value }); }
Overall, your ponent should be:
class SimpleSlider extends Component {
state = {
slider1: 50,
slider2: 50
};
handleChange = name => (e, value) => {
this.setState({
[name]: value // --> Important bit here: This is how you set the value of sliders
});
};
render() {
const { classes } = this.props;
const { slider1, slider2 } = this.state;
return (
<div className={classes.root}>
<Typography id="label">Slider label</Typography>
<Slider
classes={{ container: classes.slider }}
value={slider1}
aria-labelledby="label"
onChange={this.handleChange("slider1")}
/>
<Slider
classes={{ container: classes.slider }}
value={slider2}
aria-labelledby="label"
onChange={this.handleChange("slider2")}
/>
</div>
);
}
}
See it in action: https://codesandbox.io/s/4qz8o01qp4
Edit: After running the code I found that the #1 doesn't work because the id attribute is not being passed down to the event target
You can pass a name
prop to the slider
ponent. This can be accessed using event.target.name
class MySlider extends Component {
handleChange => (e: React.ChangeEvent<HTMLInputElement>, values: number | number[]) => {
if (Array.isArray(values)) {
sliderChanged(e.target.name, values[0], values[1]);
}
});
};
render() {
const { classes } = this.props;
const { slider1, slider2 } = this.state;
return (
<div className={classes.root}>
<Typography id="label">Slider label</Typography>
<Slider
classes={{ container: classes.slider }}
value={slider1}
aria-labelledby="label"
onChange={this.handleChange("slider1")}
/>
<Slider
name={"pass name here"}
value={[
minValue != null ? minValue : 0,
maxValue != null ? maxValue : 1,
]}
onChange={this.handleChange}
/>
</div>
);
}
}
本文标签: javascriptHow do I identify a materialui slider in reactStack Overflow
版权声明:本文标题:javascript - How do I identify a material-ui slider in react? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744245318a2596982.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论