client/src/components/NoteInput.js
author ymh <ymh.work@gmail.com>
Mon, 22 May 2017 17:59:19 +0200
changeset 2 b52921a63e77
parent 1 431977d7c9a6
child 5 5c91bfa8fcde
permissions -rw-r--r--
add scss + bootstrap

import React, {Component} from 'react';

import ReactDOM from 'react-dom'
import { Form, FormControl, FormGroup, Button } from 'react-bootstrap';

import PropTypes from 'prop-types';

class NoteInput extends Component {
  constructor(props) {
    super(props);

    this.state = {value: ''};

    this.onAddNoteClick = this.onAddNoteClick.bind(this);
    this.handleChange = this.handleChange.bind(this);
  }

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

  onAddNoteClick(event) {
    this.props.addNote(this.state.value);

    this.noteInput.value = "";

    this.noteInput.focus();
  }

  componentDidMount() {
    this.noteInput.focus();
  }

  render() {
    return (
      <Form>
        <FormGroup>
          <FormControl
              componentClass="textarea"
              placeholder="Enter note"
              onChange={this.handleChange}
              ref={(input) => { this.noteInput = ReactDOM.findDOMNode(input); }}
          />
        </FormGroup>
        <Button type="button" onClick={this.onAddNoteClick}>Add Note</Button>
      </Form>
    );
  }
}

NoteInput.propTypes = {
  addNote: PropTypes.func.isRequired
};

export default NoteInput;