fix(KeyValue Editor): Pressing enter on an input should update the value (#7580)

* fix onKeydown using the previous value

* use keybindingHandler for improved keyboard support
This commit is contained in:
James Gatz
2024-06-25 16:42:36 +02:00
committed by GitHub
parent c92707b292
commit b9883fdd14

View File

@@ -3,6 +3,7 @@ import React, { FC, Fragment, useCallback, useRef, useState } from 'react';
import { FocusScope } from 'react-aria';
import { Button, Dialog, DialogTrigger, DropIndicator, GridList, GridListItem, Menu, MenuItem, MenuTrigger, Popover, ToggleButton, Toolbar, useDragAndDrop } from 'react-aria-components';
import { useListData } from 'react-stately';
import { createKeybindingsHandler } from 'tinykeys';
import { describeByteSize, generateId } from '../../../common/misc';
import { useNunjucksEnabled } from '../../context/nunjucks/nunjucks-enabled-context';
@@ -54,6 +55,20 @@ const EditableOneLineEditorModal = ({
}
}, [buttonRef]);
const onKeydown = useCallback((e: KeyboardEvent, value: string) => {
const handler = createKeybindingsHandler({
'Enter': e => {
e.preventDefault();
setIsOpen(false);
onChange(value);
setValue(value);
},
});
handler(e);
},
[onChange]);
useResizeObserver({
ref: buttonRef,
onResize: onResize,
@@ -116,13 +131,7 @@ const EditableOneLineEditorModal = ({
readOnly={readOnly}
getAutocompleteConstants={getAutocompleteConstants}
onChange={setValue}
onKeyDown={e => {
if (e.key === 'Enter') {
e.preventDefault();
onChange(value);
setIsOpen(false);
}
}}
onKeyDown={onKeydown}
/>
</div>
</FocusScope>