2
votes

I was trying to implement a custom editor for a row in my react data grid which I wish to let user input from a calendar instead of typing. I came up something which keeps returning invalid dates.

index.js

import React, { Component } from "react";
import ReactDOM from "react-dom";
import "semantic-ui-css/semantic.min.css";
import {
  Icon,
  Menu,
  Checkbox,
  Grid,
  Dropdown,
  Rail,
  Segment,
  MenuHeader,
  Responsive
} from "semantic-ui-react";
import ReactDataGrid from "react-data-grid";
import autoBind from "react-autobind";
import { Editors, Formatters, Data, Filters } from "react-data-grid-addons";
import { connect } from "react-redux";
import moment from "moment";
import DateEditor from "./DateEditor";

import "./styles.css";

const columns = [
  { key: "id", name: "ID" },
  { key: "title", name: "Title" },
  { key: "date", name: "Date", editor: DateEditor }
];

const rows = [
  { id: 0, title: "Task 1", issueType: "Bug", labelColour: "#1D1D1F" },
  { id: 1, title: "Task 2", issueType: "Story", labelColour: "#1D1D1F" },
  { id: 2, title: "Task 3", issueType: "Epic", labelColour: "1D1D1F" }
];

class Example extends React.Component {
  state = { rows };

  onGridRowsUpdated = ({ fromRow, toRow, updated }) => {
    this.setState(state => {
      const rows = state.rows.slice();
      for (let i = fromRow; i <= toRow; i++) {
        rows[i] = { ...rows[i], ...updated };
      }
      return { rows };
    });
  };
  render() {
    return (
      <div>
        <ReactDataGrid
          columns={columns}
          rowGetter={i => this.state.rows[i]}
          rowsCount={3}
          onGridRowsUpdated={this.onGridRowsUpdated}
          enableCellSelect={true}
        />
      </div>
    );
  }
}

const rootElement = document.getElementById("root");
ReactDOM.render(<Example />, rootElement);

DateEditor.js


import { DateInput } from "semantic-ui-calendar-react";
import React, { Component } from "react";
import ReactDOM from "react-dom";
import { Form } from "semantic-ui-react";
import autoBind from "react-autobind";
import moment, { isMoment } from "moment";

export default class DateEditor extends React.Component {
  constructor(props) {
    super(props);
    this.state = { dateEditor: "" };
    autoBind(this);
    //moment(props.value).format('L')
  }
  getValue() {
    //return { date: moment(this.state.dateEditor).format("L") };
    return { date: this.state.dateEditor };
  }

  getInputNode() {
    return ReactDOM.findDOMNode(this);
  }

  handleChange = (event, { name, value }) => {
    this.setState({ [name]: value }, () => this.props.onCommit());
    //if (this.state.hasOwnProperty(name)) {
    //  this.setState({ [name]: value }, () => this.props.onCommit()); //);
    //}
  };

  render() {
    return (
      <Form>
        <DateInput
          name="dateEditor"
          dateFormat={moment().format("L")}
          placeholder="Date"
          value={this.state.dateEditor}
          //{moment(this.state.date).format('L')}
          iconPosition="left"
          onChange={this.handleChange}
        />
      </Form>
    );
  }
}

If anyone has ideas I also have my piece of code experiment here React-Data-Grid Custom Editor at CodeSandBox Some reference: Semantic UI Calendar and React-Data-Grid Custom Editor Many thanks to people may have some ideas where I did wrong, really appreciate.

1
The problem is the my code current behavior no matter what date I selected, it will only return the date today. - SVMJ

1 Answers

0
votes

Actually, it is working, if you double click on the date input, or start typing there, it will show you the calendar to select the date. The issue is with react-data-grid and it seems like the default behavior of react-data-grid. You can see the examples here. The issue is already been created on the reach-data-grid github, Hopefully they will fix this issue.

See here, you can see easily that DateEditor component is working well. I just showed the component down there outside the react-data-grid component.

For now, I think it would be good to avoid react-data-grid and instead use table from semantic-ui-react.

Hope it will help.