2

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.

MJ Tsai
  • 161
  • 1
  • 13
  • The problem is the my code current behavior no matter what date I selected, it will only return the date today. – MJ Tsai Mar 14 '19 at 03:29

1 Answers1

0

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.

Abdullah Aziz
  • 700
  • 5
  • 11
  • Thanks for the prompt reply, however, I am thinking the wanted behavior is the cell will get the date as I choose. Current code will only output the date today, so I am wondering if I did wrong somewhere? – MJ Tsai Mar 14 '19 at 03:03
  • Also according to their discussion the issue was resolved, I think I am having some problem with my code – MJ Tsai Mar 14 '19 at 03:36
  • Oh the issue for date always todays is because of `dateFormat={moment().format("L")}`, i removed this moment format, and it is working fine. check out here https://codesandbox.io/s/q3jny4lm9j . Hope it will work now. Thanks – Abdullah Aziz Mar 14 '19 at 05:18
  • Thank you so much you saved my day! A follow up question is that how I suppose to format to MM/DD/YYYY? Many thanks! – MJ Tsai Mar 14 '19 at 05:40
  • Great! I also fix that in the same sandbox. Actually you can give many format strings same as moment formatter. here is the list for further options: https://momentjs.com/docs/#/displaying/format/ – Abdullah Aziz Mar 14 '19 at 05:59
  • For reference I am using React 16.4.2 / React Data Grid 6.1.0 / Semantic UI Calendar 0.14.4 / Semantic UI 0.84.0 on my build, if use older version dependencies there will be issue passing the date picked to RDG – MJ Tsai Apr 15 '19 at 17:47