Как я могу сохранить значение для даты из выбора даты? - PullRequest
0 голосов
/ 01 февраля 2019

Я хочу сохранить значение для даты из этого кода, я использую vscode, реагировать и пакет материалов.

import React from 'react';
import PropTypes from 'prop-types';
import { withStyles } from '@material-ui/core/styles';
import TextField from '@material-ui/core/TextField';

const styles = theme => ({
  container: {
    display: 'flex',
    flexWrap: 'wrap',
  },
  textField: {
    marginLeft: theme.spacing.unit,
    marginRight: theme.spacing.unit,
    width: 200,
  },
});

function DatePickers(props) {
  const { classes } = props;
  //console.log(props)

  return (
    <form className={classes.container} noValidate>
      <TextField
        id="date"
        label="Birthday"
        type="date"
        defaultValue="2017-05-24"
        className={classes.textField}
        InputLabelProps={{
          shrink: true,
        }}
      />
    </form>
  );
}

DatePickers.propTypes = {
  classes: PropTypes.object.isRequired,
};

export default withStyles(styles)(DatePickers);

1 Ответ

0 голосов
/ 07 марта 2019

Чтобы сохранить выбранную дату, ваш код должен выглядеть примерно так:

import React, { Component } from "react";
import PropTypes from "prop-types";
import { withStyles } from "@material-ui/core/styles";
import TextField from "@material-ui/core/TextField";

const styles = theme => ({
  container: {
    display: "flex",
    flexWrap: "wrap"
  },
  textField: {
    marginLeft: theme.spacing.unit,
    marginRight: theme.spacing.unit,
    width: 200
  }
});

class DatePickers extends Component {
  state = {
    date: "2017-05-24"
  };

  handleChange = event => {
    this.setState({ date: event.target.value });
  };

  render() {
    const { classes } = this.props;
    console.log(this.state);
    return (
      <form className={classes.container} noValidate>
        <TextField
          id="date"
          label="Birthday"
          type="date"
          value={this.state.date}
          onChange={this.handleChange}
          className={classes.textField}
          InputLabelProps={{
            shrink: true
          }}
        />
      </form>
    );
  }
}

DatePickers.propTypes = {
  classes: PropTypes.object.isRequired
};

export default withStyles(styles)(DatePickers);

...