Allow returning to input on selectTree when pressing escape (#49106)
* Allow returning to input on selectTree when pressing escape * Call onEscape in single mode as well * Close the menu on escape * Extract handleEscapePress function
This commit is contained in:
parent
d66e1a2438
commit
96739fd6fd
|
@ -0,0 +1,4 @@
|
||||||
|
Significance: minor
|
||||||
|
Type: update
|
||||||
|
|
||||||
|
Allow returning to input on selectTree when pressing escape
|
|
@ -42,6 +42,7 @@ export const SelectTreeMenu = ( {
|
||||||
items,
|
items,
|
||||||
treeRef: ref,
|
treeRef: ref,
|
||||||
onClose = () => {},
|
onClose = () => {},
|
||||||
|
onEscape,
|
||||||
shouldShowCreateButton,
|
shouldShowCreateButton,
|
||||||
...props
|
...props
|
||||||
}: MenuProps ) => {
|
}: MenuProps ) => {
|
||||||
|
@ -135,6 +136,7 @@ export const SelectTreeMenu = ( {
|
||||||
shouldShowCreateButton={
|
shouldShowCreateButton={
|
||||||
shouldShowCreateButton
|
shouldShowCreateButton
|
||||||
}
|
}
|
||||||
|
onEscape={ onEscape }
|
||||||
style={ {
|
style={ {
|
||||||
width: boundingRect?.width,
|
width: boundingRect?.width,
|
||||||
} }
|
} }
|
||||||
|
|
|
@ -245,6 +245,10 @@ export const SelectTree = function SelectTree( {
|
||||||
isOpen={ isOpen }
|
isOpen={ isOpen }
|
||||||
items={ linkedTree }
|
items={ linkedTree }
|
||||||
shouldShowCreateButton={ shouldShowCreateButton }
|
shouldShowCreateButton={ shouldShowCreateButton }
|
||||||
|
onEscape={ () => {
|
||||||
|
focusOnInput();
|
||||||
|
setIsOpen( false );
|
||||||
|
} }
|
||||||
onClose={ () => {
|
onClose={ () => {
|
||||||
setIsOpen( false );
|
setIsOpen( false );
|
||||||
} }
|
} }
|
||||||
|
|
|
@ -33,6 +33,15 @@ export const TreeItem = forwardRef( function ForwardedTreeItem(
|
||||||
ref,
|
ref,
|
||||||
} );
|
} );
|
||||||
|
|
||||||
|
function handleEscapePress(
|
||||||
|
event: React.KeyboardEvent< HTMLInputElement >
|
||||||
|
) {
|
||||||
|
if ( event.key === 'Escape' && props.onEscape ) {
|
||||||
|
event.preventDefault();
|
||||||
|
props.onEscape();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<li
|
<li
|
||||||
{ ...treeItemProps }
|
{ ...treeItemProps }
|
||||||
|
@ -58,6 +67,7 @@ export const TreeItem = forwardRef( function ForwardedTreeItem(
|
||||||
}
|
}
|
||||||
checked={ selection.checkedStatus === 'checked' }
|
checked={ selection.checkedStatus === 'checked' }
|
||||||
onChange={ selection.onSelectChild }
|
onChange={ selection.onSelectChild }
|
||||||
|
onKeyDown={ handleEscapePress }
|
||||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||||
// @ts-ignore __nextHasNoMarginBottom is a valid prop
|
// @ts-ignore __nextHasNoMarginBottom is a valid prop
|
||||||
__nextHasNoMarginBottom={ true }
|
__nextHasNoMarginBottom={ true }
|
||||||
|
@ -70,6 +80,7 @@ export const TreeItem = forwardRef( function ForwardedTreeItem(
|
||||||
onChange={ ( event ) =>
|
onChange={ ( event ) =>
|
||||||
selection.onSelectChild( event.target.checked )
|
selection.onSelectChild( event.target.checked )
|
||||||
}
|
}
|
||||||
|
onKeyDown={ handleEscapePress }
|
||||||
/>
|
/>
|
||||||
) }
|
) }
|
||||||
|
|
||||||
|
|
|
@ -59,6 +59,7 @@ export const Tree = forwardRef( function ForwardedTree(
|
||||||
) as HTMLButtonElement
|
) as HTMLButtonElement
|
||||||
)?.focus();
|
)?.focus();
|
||||||
} }
|
} }
|
||||||
|
onEscape={ props.onEscape }
|
||||||
/>
|
/>
|
||||||
) ) }
|
) ) }
|
||||||
</ol>
|
</ol>
|
||||||
|
@ -94,6 +95,9 @@ export const Tree = forwardRef( function ForwardedTree(
|
||||||
)
|
)
|
||||||
?.focus();
|
?.focus();
|
||||||
}
|
}
|
||||||
|
} else if ( event.key === 'Escape' && props.onEscape ) {
|
||||||
|
event.preventDefault();
|
||||||
|
props.onEscape();
|
||||||
}
|
}
|
||||||
} }
|
} }
|
||||||
>
|
>
|
||||||
|
|
|
@ -76,6 +76,10 @@ type BaseTreeProps = {
|
||||||
* Called when the create button is clicked to help closing any related popover.
|
* Called when the create button is clicked to help closing any related popover.
|
||||||
*/
|
*/
|
||||||
onTreeBlur?(): void;
|
onTreeBlur?(): void;
|
||||||
|
/**
|
||||||
|
* Called when the escape key is pressed.
|
||||||
|
*/
|
||||||
|
onEscape?(): void;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type TreeProps = BaseTreeProps &
|
export type TreeProps = BaseTreeProps &
|
||||||
|
|
|
@ -21630,7 +21630,7 @@ packages:
|
||||||
engines: {node: '>=18'}
|
engines: {node: '>=18'}
|
||||||
hasBin: true
|
hasBin: true
|
||||||
peerDependencies:
|
peerDependencies:
|
||||||
react: ^17.0.2
|
react: 18.2.0
|
||||||
|
|
||||||
react-number-format@4.9.3:
|
react-number-format@4.9.3:
|
||||||
resolution: {integrity: sha512-am1A1xYAbENuKJ+zpM7V+B1oRTSeOHYltqVKExznIVFweBzhLmOBmyb1DfIKjHo90E0bo1p3nzVJ2NgS5xh+sQ==}
|
resolution: {integrity: sha512-am1A1xYAbENuKJ+zpM7V+B1oRTSeOHYltqVKExznIVFweBzhLmOBmyb1DfIKjHo90E0bo1p3nzVJ2NgS5xh+sQ==}
|
||||||
|
|
Loading…
Reference in New Issue