Giter Site home page Giter Site logo

Comments (4)

michelengelen avatar michelengelen commented on July 17, 2024

Hey @master117 ... what you want to achieve is basically a cell that is in edit state infinitely, right?
You can achieve that by just controlling the edit state on load:

import React from 'react';
import { DataGrid, GridRowsProp, GridColDef, GridRowModes } from '@mui/x-data-grid';
import { randomCreatedDate, randomTraderName, randomUpdatedDate } from '@mui/x-data-grid-generator';

const columns: GridColDef[] = [
  { field: 'name', headerName: 'Name', width: 180, editable: true },
  {
    field: 'age',
    headerName: 'Age',
    type: 'number',
    editable: true,
    align: 'left',
    headerAlign: 'left',
  },
  {
    field: 'dateCreated',
    headerName: 'Date Created',
    type: 'date',
    width: 180,
    editable: true,
  },
  {
    field: 'lastLogin',
    headerName: 'Last Login',
    type: 'dateTime',
    width: 220,
    editable: true,
  },
];

const rows: GridRowsProp = [
  {
    id: 1,
    name: randomTraderName(),
    age: 25,
    dateCreated: randomCreatedDate(),
    lastLogin: randomUpdatedDate(),
  },
  {
    id: 2,
    name: randomTraderName(),
    age: 36,
    dateCreated: randomCreatedDate(),
    lastLogin: randomUpdatedDate(),
  },
  {
    id: 3,
    name: randomTraderName(),
    age: 19,
    dateCreated: randomCreatedDate(),
    lastLogin: randomUpdatedDate(),
  },
  {
    id: 4,
    name: randomTraderName(),
    age: 28,
    dateCreated: randomCreatedDate(),
    lastLogin: randomUpdatedDate(),
  },
  {
    id: 5,
    name: randomTraderName(),
    age: 23,
    dateCreated: randomCreatedDate(),
    lastLogin: randomUpdatedDate(),
  },
];

const rowModesModel = rows.reduce(
  (acc, row) => ({
    ...acc,
    [row.id]: {
      mode: GridRowModes.Edit,
    },
  }),
  {},
);

export default function App() {
  return (
    <div style={{ height: 300, width: '100%' }}>
      <DataGrid rows={rows} columns={columns} editMode={'row'} rowModesModel={rowModesModel} />
    </div>
  );
}

from mui-x.

master117 avatar master117 commented on July 17, 2024

No, this cell will not update my state. I need my state to be updated on every input or when leaving the field.

(I also prefer the design of a textfield in the table over a cell in edit mode.)

from mui-x.

KenanYusuf avatar KenanYusuf commented on July 17, 2024

How do I prevent navigating off? Changing all cells of that column to edit mode is visually unappealing but maybe this can be countered with css?

You could prevent keyboard events from propagating by adding an onKeyDown event and calling the stopPropagation method on the event.

<TextField
  error={!IsNumeric(params.row.age)}
  value={params.row.age}
  onKeyDown={(event) => {
     event.stopPropagation();
  }}
  onChange={(event) => {
    const temp = [...rows];
    const index = temp.findIndex((x) => x.id === params.row.id);
    temp[index] = { ...temp[index], age: event.target.value };
    setRows(temp);
  }}
/>

Is this even a good idea?

One consideration is that by hijacking the keyboard events within the data grid you are disabling the keyboard navigation functionality that is built in for accessibility.

As you suggested in the previous question, it is probably best to use the edit functionality that is built into the data grid and customizing the look of the fields with CSS.

from mui-x.

github-actions avatar github-actions commented on July 17, 2024

The issue has been inactive for 7 days and has been automatically closed.

from mui-x.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.