<i id='3ykej'><tr id='3ykej'><dt id='3ykej'><q id='3ykej'><span id='3ykej'><b id='3ykej'><form id='3ykej'><ins id='3ykej'></ins><ul id='3ykej'></ul><sub id='3ykej'></sub></form><legend id='3ykej'></legend><bdo id='3ykej'><pre id='3ykej'><center id='3ykej'></center></pre></bdo></b><th id='3ykej'></th></span></q></dt></tr></i><div id='3ykej'><tfoot id='3ykej'></tfoot><dl id='3ykej'><fieldset id='3ykej'></fieldset></dl></div>

<small id='3ykej'></small><noframes id='3ykej'>

  • <legend id='3ykej'><style id='3ykej'><dir id='3ykej'><q id='3ykej'></q></dir></style></legend>
    <tfoot id='3ykej'></tfoot>
      <bdo id='3ykej'></bdo><ul id='3ykej'></ul>

      1. 如何在 Material UI (React JS) 中根据需要制作“选择"组件

        How to make a #39;Select#39; component as required in Material UI (React JS)(如何在 Material UI (React JS) 中根据需要制作“选择组件)
        <i id='uho7U'><tr id='uho7U'><dt id='uho7U'><q id='uho7U'><span id='uho7U'><b id='uho7U'><form id='uho7U'><ins id='uho7U'></ins><ul id='uho7U'></ul><sub id='uho7U'></sub></form><legend id='uho7U'></legend><bdo id='uho7U'><pre id='uho7U'><center id='uho7U'></center></pre></bdo></b><th id='uho7U'></th></span></q></dt></tr></i><div id='uho7U'><tfoot id='uho7U'></tfoot><dl id='uho7U'><fieldset id='uho7U'></fieldset></dl></div>

            <bdo id='uho7U'></bdo><ul id='uho7U'></ul>
            • <small id='uho7U'></small><noframes id='uho7U'>

              <tfoot id='uho7U'></tfoot>

              <legend id='uho7U'><style id='uho7U'><dir id='uho7U'><q id='uho7U'></q></dir></style></legend>
                <tbody id='uho7U'></tbody>
                  本文介绍了如何在 Material UI (React JS) 中根据需要制作“选择"组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  除非有选择的选项,否则我想用红色显示错误.有没有办法做到这一点.

                  I want to display like an error with red color unless there is a selected option. Is there any way to do it.

                  推荐答案

                  要使用 Material UI 设置必填的 Select 字段,您可以这样做:

                  For setting a required Select field with Material UI, you can do:

                  class SimpleSelect extends React.PureComponent {
                    state = {
                      selected: null,
                      hasError: false
                    }
                  
                    handleChange(value) {
                      this.setState({ selected: value });
                    }
                  
                    handleClick() {
                      this.setState(state => ({ hasError: !state.selected }));
                    }
                  
                    render() {
                      const { classes } = this.props;
                      const { selected, hasError } = this.state;
                  
                      return (
                        <form className={classes.root} autoComplete="off">
                          <FormControl className={classes.formControl} error={hasError}>
                            <InputLabel htmlFor="name">
                              Name
                            </InputLabel>
                            <Select
                              name="name"
                              value={selected}
                              onChange={event => this.handleChange(event.target.value)}
                              input={<Input id="name" />}
                            >
                              <MenuItem value="hai">Hai</MenuItem>
                              <MenuItem value="olivier">Olivier</MenuItem>
                              <MenuItem value="kevin">Kevin</MenuItem>
                            </Select>
                            {hasError && <FormHelperText>This is required!</FormHelperText>}
                          </FormControl>
                          <button type="button" onClick={() => this.handleClick()}>
                            Submit
                          </button>
                        </form>
                      );
                    }
                  }
                  

                  CodeSandBox 上的工作演示

                  这篇关于如何在 Material UI (React JS) 中根据需要制作“选择"组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Pause youtube video, youtube api(暂停 youtube 视频,youtube api)
                  Youtube iframe api not triggering onYouTubeIframeAPIReady(Youtube iframe api 未触发 onYouTubeIframeAPIReady)
                  How can I stop a video with Javascript in Youtube?(如何在 Youtube 中停止使用 Javascript 的视频?)
                  How to call Greasemonkey#39;s GM_ functions from code that must run in the target page scope?(如何从必须在目标页面范围内运行的代码中调用 Greasemonkey 的 GM_ 函数?)
                  How do you mute an embedded Youtube player?(如何使嵌入式 Youtube 播放器静音?)
                  How to get number of video views with YouTube API?(如何使用 YouTube API 获取视频观看次数?)
                    <tbody id='B3RSg'></tbody>

                1. <tfoot id='B3RSg'></tfoot>

                    <small id='B3RSg'></small><noframes id='B3RSg'>

                        <i id='B3RSg'><tr id='B3RSg'><dt id='B3RSg'><q id='B3RSg'><span id='B3RSg'><b id='B3RSg'><form id='B3RSg'><ins id='B3RSg'></ins><ul id='B3RSg'></ul><sub id='B3RSg'></sub></form><legend id='B3RSg'></legend><bdo id='B3RSg'><pre id='B3RSg'><center id='B3RSg'></center></pre></bdo></b><th id='B3RSg'></th></span></q></dt></tr></i><div id='B3RSg'><tfoot id='B3RSg'></tfoot><dl id='B3RSg'><fieldset id='B3RSg'></fieldset></dl></div>
                          <bdo id='B3RSg'></bdo><ul id='B3RSg'></ul>
                          • <legend id='B3RSg'><style id='B3RSg'><dir id='B3RSg'><q id='B3RSg'></q></dir></style></legend>