Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

};
like image 524
deranjer Avatar asked Mar 14 '26 14:03

deranjer


2 Answers

You can use TextField onChange method to store its value in addTorrentPopup component.

         <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});
        }
like image 198
AranS Avatar answered Mar 16 '26 03:03

AranS


React supports a special attribute ref that you can attach to input(or any other element).

The ref attribute takes a callback function, and the callback will be executed immediately after the form is submitted . 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.

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.


hope it helps

like image 41
Ruhul Amin Avatar answered Mar 16 '26 02:03

Ruhul Amin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!