使用材料ui和reactjs

时间:2017-11-19 00:20:57

标签: javascript reactjs material-ui

刚开始学习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>
    );
  }

};

2 个答案:

答案 0 :(得分:2)

您可以使用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});
        }

答案 1 :(得分:1)

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

材质UI TextField 组件支持inputRef prop,它将添加到本机input元素中。

所以这就是你需要添加的东西 -

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



摘要:您可以通过ref方法通过 TextField 组件的inputRef道具来获取输入值。


希望有所帮助