简体   繁体   English

使用材料ui和reactjs返回文本字段值

[英]Returning textfield value with material ui and reactjs

Just started learning React and I'm not sure how to get the value back from my textfield when I hit the submit button. 刚开始学习React,我不确定当我点击提交按钮时如何从文本字段中获取值。 I'm following the examples here: https://material-ui-next.com/demos/dialogs/ but they never cover how to get the value of the textfield. 我正在按照这里的示例进行操作: https//material-ui-next.com/demos/dialogs/但他们从不介绍如何获取文本字段的值。 I've tried a bunch of ways, but keep getting undefined for the value. 我已经尝试了很多方法,但不断为这个值定义。 Here is my current code: 这是我目前的代码:

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>
    );
  }

};

You can use TextField onChange method to store its value in addTorrentPopup component. 您可以使用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});
        }

React supports a special attribute ref that you can attach to input(or any other element). React支持一个特殊的属性ref ,您可以将其附加到输入(或任何其他元素)。

The ref attribute takes a callback function, and the callback will be executed immediately after the form is submitted . ref属性采用回调函数,回调将在提交表单后立即执行。 Here's how it works -- 这是它的工作原理 -

<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 component supports inputRef prop which will be added to the native input element. Material UI TextField组件支持将添加到本机输入元素的inputRef prop。

So this is what you need to add-- 所以这就是你需要添加的东西 -

<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
    }}
/>



summary : You can have the value of the Input by passing a ref method through inputRef prop of TextField component. 摘要 :您可以通过将ref方法传递给TextField组件的inputRef prop来inputRef Input的值。


hope it helps 希望能帮助到你

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

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