feat: support esc shortcut on input-edit (#6143)

This commit is contained in:
Fangdun Tsai
2024-03-16 21:13:37 +08:00
committed by GitHub
parent eec24db1a1
commit 636fa503b8
2 changed files with 24 additions and 4 deletions

View File

@@ -1,9 +1,12 @@
import clsx from 'clsx';
import type {
CSSProperties,
ForwardedRef,
HTMLAttributes,
KeyboardEvent,
PropsWithChildren,
} from 'react';
import {
type CSSProperties,
type ForwardedRef,
type HTMLAttributes,
type PropsWithChildren,
useCallback,
useEffect,
useImperativeHandle,
@@ -29,6 +32,10 @@ export interface InlineEditProps
* Whether the content is editable
*/
editable?: boolean;
/**
* Whether to exit when pressing `Escape`
*/
exitible?: boolean;
onInput?: (v: string) => void;
onChange?: (v: string) => void;
@@ -68,6 +75,7 @@ export interface InlineEditProps
export const InlineEdit = ({
value,
editable,
exitible,
className,
style,
trigger = 'doubleClick',
@@ -127,6 +135,16 @@ export const InlineEdit = ({
inputRef.current?.scrollTo(0, 0);
}, [submit]);
const onKeyDown = useCallback(
(e: KeyboardEvent<HTMLInputElement>) => {
e.stopPropagation();
if (!exitible) return;
if (e.key !== 'Escape') return;
inputRef.current?.blur();
},
[exitible]
);
const inputHandler = useCallback(
(v: string) => {
setEditingValue(v);
@@ -198,6 +216,7 @@ export const InlineEdit = ({
placeholder={placeholder}
onBlur={onBlur}
onEnter={onEnter}
onKeyDown={onKeyDown}
onChange={inputHandler}
style={inputWrapperInheritsStyles}
inputStyle={inputInheritsStyles}

View File

@@ -43,6 +43,7 @@ export const BlocksuiteHeaderTitle = (props: BlockSuiteHeaderTitleProps) => {
value={title}
onChange={onChange}
editable={!isPublic}
exitible={true}
placeholder="Untitled"
data-testid="title-edit-button"
handleRef={inputHandleRef}