client/src/components/NoteInput.js
author Alexandre Segura <mex.zktk@gmail.com>
Thu, 01 Jun 2017 16:15:08 +0200
changeset 15 4a8bbd314a46
parent 12 48ddaa42b810
child 16 e67cd18cc594
permissions -rw-r--r--
Store note JSON data.

import React, {Component} from 'react';
import { Form, FormGroup, Button } from 'react-bootstrap';
import { Plain } from 'slate';

import PropTypes from 'prop-types';
import SlateEditor from './SlateEditor';

class NoteInput extends Component {

  state = {
    buttonDisabled: false
  }

  onChange = (state) => {
    const text = Plain.serialize(state);
    this.setState({ buttonDisabled: text.length === 0 });
  }

  onAddNoteClick = () => {
    const plain = this.refs.editor.asPlain();
    const raw = this.refs.editor.asRaw();

    this.props.addNote(this.props.session, {
      plain: plain,
      raw: raw
    });

    this.refs.editor.clear();
    setTimeout(() => this.refs.editor.focus(), 250);
  }

  componentDidMount() {
    const text = this.refs.editor.asPlain();
    this.setState({ buttonDisabled: text.length === 0 });
  }

  render() {
    return (
      <Form>
        <FormGroup>
          <div className="editor-wrapper">
            <SlateEditor ref="editor" onChange={this.onChange} />
          </div>
        </FormGroup>
        <Button disabled={this.state.buttonDisabled} bsStyle="primary" type="button" onClick={this.onAddNoteClick}>Add Note</Button>
      </Form>
    );
  }
}

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

export default NoteInput;