Skip to content

Commit

Permalink
[DataGrid] Fix "stop editing" integration with IME e.g. Japanese (#5257)
Browse files Browse the repository at this point in the history
Co-authored-by: Matheus Wichman <matheushw@outlook.com>
  • Loading branch information
Gumichocopengin8 and m4theushw committed Jul 5, 2022
1 parent d6adde4 commit 22567ed
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,38 @@ describe('<DataGridPro /> - Cell Editing', () => {
deleteValue: true,
});
});

it(`should ignore keydown event until the IME is confirmed with a letter`, () => {
render(<TestCase />);
const listener = spy();
apiRef.current.subscribeEvent('cellEditStop', listener);
const cell = getCell(0, 1);
fireEvent.doubleClick(cell);
const input = cell.querySelector('input')!;
fireEvent.change(input, { target: { value: 'あ' } });
fireEvent.keyDown(cell, { key: 'Enter', keyCode: 229 });
expect(listener.callCount).to.equal(0);
fireEvent.keyDown(cell, { key: 'Enter', keyCode: 13 });
expect(listener.callCount).to.equal(1);
expect(input.value).to.equal('あ');
expect(listener.lastCall.args[0].reason).to.equal('enterKeyDown');
});

it(`should ignore keydown event until the IME is confirmed with multiple letters`, () => {
render(<TestCase />);
const listener = spy();
apiRef.current.subscribeEvent('cellEditStop', listener);
const cell = getCell(0, 1);
fireEvent.doubleClick(cell);
const input = cell.querySelector('input')!;
fireEvent.change(input, { target: { value: 'ありがとう' } });
fireEvent.keyDown(cell, { key: 'Enter', keyCode: 229 });
expect(listener.callCount).to.equal(0);
fireEvent.keyDown(cell, { key: 'Enter', keyCode: 13 });
expect(listener.callCount).to.equal(1);
expect(input.value).to.equal('ありがとう');
expect(listener.lastCall.args[0].reason).to.equal('enterKeyDown');
});
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,38 @@ describe('<DataGridPro /> - Row Editing', () => {
deleteValue: true,
});
});

it(`should ignore keydown event until the IME is confirmed with a letter`, () => {
render(<TestCase />);
const listener = spy();
apiRef.current.subscribeEvent('rowEditStop', listener);
const cell = getCell(0, 1);
fireEvent.doubleClick(cell);
const input = cell.querySelector('input')!;
fireEvent.change(input, { target: { value: 'あ' } });
fireEvent.keyDown(input, { key: 'Enter', keyCode: 229 });
expect(listener.callCount).to.equal(0);
fireEvent.keyDown(input, { key: 'Enter', keyCode: 13 });
expect(listener.callCount).to.equal(1);
expect(input.value).to.equal('あ');
expect(listener.lastCall.args[0].reason).to.equal('enterKeyDown');
});

it(`should ignore keydown event until the IME is confirmed with multiple letters`, () => {
render(<TestCase />);
const listener = spy();
apiRef.current.subscribeEvent('rowEditStop', listener);
const cell = getCell(0, 1);
fireEvent.doubleClick(cell);
const input = cell.querySelector('input')!;
fireEvent.change(input, { target: { value: 'ありがとう' } });
fireEvent.keyDown(input, { key: 'Enter', keyCode: 229 });
expect(listener.callCount).to.equal(0);
fireEvent.keyDown(input, { key: 'Enter', keyCode: 13 });
expect(listener.callCount).to.equal(1);
expect(input.value).to.equal('ありがとう');
expect(listener.lastCall.args[0].reason).to.equal('enterKeyDown');
});
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -447,4 +447,26 @@ describe('<DataGridPro /> - Row Editing', () => {
clock.runToLast();
expect(getRow(1)).not.to.have.class('MuiDataGrid-row--editing');
});

it(`should ignore keydown event until the IME is confirmed`, () => {
const valueSetter = spy(({ row }) => row);
render(
<TestCase
editMode="row"
rows={[{ id: 0, text: 'こんにちは' }]}
columns={[{ field: 'text', editable: true, valueSetter }]}
/>,
);

const cell = getCell(0, 0);
fireEvent.doubleClick(cell);
const input = cell.querySelector('input')!;
expect(input!.value).to.equal('こんにちは');
fireEvent.change(input, { target: { value: 'あ' } });
fireEvent.keyDown(input, { key: 'Enter', keyCode: 229 });
expect(valueSetter.callCount).to.equal(0);
fireEvent.keyDown(input, { key: 'Enter', keyCode: 13 });
expect(valueSetter.callCount).to.equal(1);
expect(input!.value).to.equal('あ');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ export const useGridCellEditing = (
const handleCellKeyDown = React.useCallback<GridEventListener<'cellKeyDown'>>(
(params, event) => {
if (params.cellMode === GridCellModes.Edit) {
// Wait until IME is settled for Asian languages like Japanese and Chinese
// TODO: `event.which` is depricated but this is a temporary workaround
if (event.which === 229) {
return;
}

let reason: GridCellEditStopReasons | undefined;

if (event.key === 'Escape') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,12 @@ export const useCellEditing = (

const handleCellKeyDown = React.useCallback<GridEventListener<'cellKeyDown'>>(
async (params, event) => {
// Wait until IME is settled for Asian languages like Japanese and Chinese
// TODO: `event.which` is depricated but this is a temporary workaround
if (event.which === 229) {
return;
}

const { id, field, cellMode, isEditable } = params;
if (!isEditable) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@ export const useGridRowEditing = (
const handleCellKeyDown = React.useCallback<GridEventListener<'cellKeyDown'>>(
(params, event) => {
if (params.cellMode === GridRowModes.Edit) {
// Wait until IME is settled for Asian languages like Japanese and Chinese
// TODO: `event.which` is depricated but this is a temporary workaround
if (event.which === 229) {
return;
}

let reason: GridRowEditStopReasons | undefined;

if (event.key === 'Escape') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,12 @@ export const useGridRowEditing = (

const handleCellKeyDown = React.useCallback<GridEventListener<'cellKeyDown'>>(
async (params, event) => {
// Wait until IME is settled for Asian languages like Japanese and Chinese
// TODO: `event.which` is depricated but this is a temporary workaround
if (event.which === 229) {
return;
}

const { cellMode, isEditable } = params;
if (!isEditable) {
return;
Expand Down

0 comments on commit 22567ed

Please sign in to comment.