简体   繁体   English

Material-UI如何在const中获取SelectField的值

[英]Material-UI how to get the value of SelectField inside const

Actually, my Drawer has a few fields including SelectField but I stuck on getting the value and the onChange part of the Field.实际上,我的 Drawer 有几个字段,包括 SelectField,但我坚持获取该字段的值和 onChange 部分。 and here's my code:这是我的代码:

 const DrawerCargoAddItem = (props) => {
  let { navDrawerOpen } = props;

  return (
    <Drawer docked={true} width={500} open={navDrawerOpen}>
         <div style={styles.logo}>
             Fleetcat Web
         </div>
         <div>
            <PageBase title={<Link to="dashboard"><img src="./images/fleetcat.png"/></Link>} style={page}>
                <SelectField
                   floatingLabelText="Category"
                   fullWidth={true}
                   value="">
                 <MenuItem key={0} primaryText="Food and Beverages"/>
                 <MenuItem key={1} primaryText="Medium"/>
                 <MenuItem key={2} primaryText="Large"/>
                 </SelectField>

             <Paper style={papers} zDepth={5}  >
                 <Link to="dashboard">
                     <FlatButton label="Tap to add Items" style={flatbutton}
                       onClick={() => { alert('foo');
                       console.log("Success");}}/> 
                 </Link>
             </Paper>
           </PageBase>
        </div>
    </Drawer>
  );
};

DrawerCargoAddItem.propTypes = {
  navDrawerOpen: PropTypes.bool,
  menus: PropTypes.array,
  username: PropTypes.string,
};

export default DrawerCargoAddItem;

You create a simple function and what you need is a React Component:你创建了一个简单的函数,你需要的是一个 React 组件:

import React from 'react';

export default class DrawerCargoAddItem extends React.Component {

state = {
    value: 0
};

handleChange = (event, index, value) => this.setState({value});

render() {
    let {navDrawerOpen} = this.props;
    const {value} = this.state

    return (
        <Drawer docked={true} width={500} open={navDrawerOpen}>
            <div style={styles.logo}>
                Fleetcat Web
            </div>
            <div>
                <PageBase
                    title={< Link to = "dashboard" > <img src="./images/fleetcat.png"/> < /Link>}
                    style={page}>
                    <SelectField
                        floatingLabelText="Category"
                        fullWidth={true}
                        value={value}
                        onChange={this.handleChange}>
                        <MenuItem value={0} key={0} primaryText="Food and Beverages"/>
                        <MenuItem value={1} key={1} primaryText="Medium"/>
                        <MenuItem value={2} key={2} primaryText="Large"/>
                    </SelectField>
                    <Paper style={papers} zDepth={5}>
                        <Link to="dashboard">
                            <FlatButton
                                label="Tap to add Items"
                                style={flatbutton}
                                onClick={() => {
                                alert('foo');
                                console.log("Success");
                            }}/>
                        </Link>
                    </Paper>
                </PageBase>
            </div>
        </Drawer>
    );
}
}

Basically you now have access to the full React lifecycle.基本上你现在可以访问完整的 React 生命周期。 The current value is now saved in the component's state.当前值现在保存在组件的状态中。

Here it is这里是

<SelectField  floatingLabelText="Category"
     fullWidth={true}
     value=""
     onChange={this.handleChanges}>
     <MenuItem key={0} value="Food and Beverages" primaryText="Food"/>
     <MenuItem key={1} value="Medium" primaryText="Medium"/>
     <MenuItem key={2} value="Large" primaryText="Large"/>
</SelectField>

here is handle change event function which should be written:这是应该编写的处理更改事件函数:

handleChanges = (e, index, value) => {
    alert(value);
}

For me worked this syntax : onBlur={event => { handleChange(event) } }对我来说,这个语法是: onBlur={event => { handleChange(event) } }

<Autocomplete
      id="size-small-standard"
      size="small"
      options={lOrigens}
      defaultValue={{ title: props.value }} 
      getOptionLabel={option => option.title}
      onBlur={e => { handleChange(e, props, { setChanging: setCurrentChangingItem1, i}) }}
      renderInput={params => (
            <TextField
                  {...params}
                  variant="standard"
                  label="Origens1"
                  placeholder="Origens2"
                  fullWidth
            />
      )}
/>

And to access the value: event.target.value并访问值: event.target.value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM