【问题标题】:Returning textfield value with material ui and reactjs使用材料 ui 和 reactjs 返回文本字段值
【发布时间】:2017-11-19 00:20:57
【问题描述】:

刚开始学习 React,当我点击提交按钮时,我不确定如何从我的文本字段中取回值。我在这里关注示例:https://material-ui-next.com/demos/dialogs/,但它们从未涵盖如何获取文本字段的值。我已经尝试了很多方法,但一直不确定价值。这是我当前的代码:

import React from 'react';
import ReactDOM from 'react-dom';
import Button from 'material-ui/Button';
import TextField from 'material-ui/TextField';
import { withStyles } from 'material-ui/styles';
import PropTypes from 'prop-types';
import Dialog, {
  DialogActions,
  DialogContent,
  DialogContentText,
  DialogTitle,
} from 'material-ui/Dialog';
import InsertLinkIcon from 'material-ui-icons/Link';
import ReactTooltip from 'react-tooltip'
import Icon from 'material-ui/Icon';
import IconButton from 'material-ui/IconButton';


const button = {
  fontSize: '60px',
  paddingRight: '20px',
  paddingLeft: '20px',
}

const inlineStyle = {
  display: 'inline-block',
}

export default class addTorrentPopup extends React.Component {

  state = {
    open: false,
  };

  handleClickOpen = () => {
    this.setState({ open: true });
  };

  handleRequestClose = () => {
    this.setState({ open: false });
  };

  handleSubmit = () => {
      this.setState({ open: false });
      let magnetLinkSubmit = this.state.textValue;
      console.log("Sending magnet link: ", magnetLinkSubmit);
      ws.send(magnetLinkSubmit);
  }

  render() {
    const { classes, onRequestClose, handleRequestClose, handleSubmit } = this.props;
    return (
      <div style={inlineStyle}>
        <IconButton onClick={this.handleClickOpen} color="primary" data-tip="Add Magnet Link" style={button}  centerRipple aria-label="Add Magnet Link" >
        <ReactTooltip place="top" type="light" effect="float" />
        <InsertLinkIcon />
      </IconButton>
        <Dialog open={this.state.open} onRequestClose={this.handleRequestClose}>
          <DialogTitle>Add Magnet Link</DialogTitle>
          <DialogContent>
            <DialogContentText>
              Add a Magnet Link here and hit submit to add torrent...
            </DialogContentText>
            <TextField
              autoFocus
              margin="dense"
              id="name"
              label="Magnet Link"
              type="text"
              placeholder="Enter Magnet Link Here"
              fullWidth
            />
          </DialogContent>
          <DialogActions>
            <Button onClick={this.handleRequestClose} color="primary">
              Cancel
            </Button>
            <Button onClick={this.handleSubmit} color="primary">
              Submit
            </Button>
          </DialogActions>
        </Dialog>
      </div>
    );
  }

};

【问题讨论】:

    标签: javascript reactjs material-ui


    【解决方案1】:

    您可以使用 TextField onChange 方法将其值存储在 addTorrentPopup 组件中。

             <TextField
              onChange={this.setTextValue}
              autoFocus
              margin="dense"
              id="name"
              label="Magnet Link"
              type="text"
              placeholder="Enter Magnet Link Here"
              fullWidth
            />
    
            ...
    
            // Implementation of setTextValue method
            setTextValue = (event) => {
              this.setState({textValue: event.target.value});
            }
    

    【讨论】:

      【解决方案2】:

      React 支持一个特殊属性 ref,您可以将其附加到输入(或任何其他元素)。

      ref 属性带有一个回调函数,该回调会在表单提交后立即执行。以下是它的工作原理--

      <form>
         <input 
            type="text" 
            value"this input element will be passed" 
            ref={$el=>{
              //You got the input value here
              let inputData = $el.value;
            }} 
      />
      

      Material UI TextField 组件支持inputRef 属性,该属性将被添加到本机输入元素中。

      所以这是你需要添加的--

      <TextField
          autoFocus
          margin="dense"
          id="name"
          label="Magnet Link"
          type="text"
          placeholder="Enter Magnet Link Here"
          fullWidth
          inputRef={$el=>{
              //you got the input value here
              const inputValue = $el.value
          }}
      />
      



      总结:您可以通过 TextField 组件的 inputRef 属性传递 ref 方法来获得 Input 的值。


      希望对你有帮助

      【讨论】:

        猜你喜欢
        • 2021-03-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-23
        • 2021-04-06
        • 1970-01-01
        • 2018-02-15
        • 1970-01-01
        相关资源
        最近更新 更多