Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DataGrid] Fix "stop editing" integration with IME e.g. Japanese #5257

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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