feat: support esc shortcut on input-edit (#6143)
This commit is contained in:
@@ -1,9 +1,12 @@
|
|||||||
import clsx from 'clsx';
|
import clsx from 'clsx';
|
||||||
|
import type {
|
||||||
|
CSSProperties,
|
||||||
|
ForwardedRef,
|
||||||
|
HTMLAttributes,
|
||||||
|
KeyboardEvent,
|
||||||
|
PropsWithChildren,
|
||||||
|
} from 'react';
|
||||||
import {
|
import {
|
||||||
type CSSProperties,
|
|
||||||
type ForwardedRef,
|
|
||||||
type HTMLAttributes,
|
|
||||||
type PropsWithChildren,
|
|
||||||
useCallback,
|
useCallback,
|
||||||
useEffect,
|
useEffect,
|
||||||
useImperativeHandle,
|
useImperativeHandle,
|
||||||
@@ -29,6 +32,10 @@ export interface InlineEditProps
|
|||||||
* Whether the content is editable
|
* Whether the content is editable
|
||||||
*/
|
*/
|
||||||
editable?: boolean;
|
editable?: boolean;
|
||||||
|
/**
|
||||||
|
* Whether to exit when pressing `Escape`
|
||||||
|
*/
|
||||||
|
exitible?: boolean;
|
||||||
|
|
||||||
onInput?: (v: string) => void;
|
onInput?: (v: string) => void;
|
||||||
onChange?: (v: string) => void;
|
onChange?: (v: string) => void;
|
||||||
@@ -68,6 +75,7 @@ export interface InlineEditProps
|
|||||||
export const InlineEdit = ({
|
export const InlineEdit = ({
|
||||||
value,
|
value,
|
||||||
editable,
|
editable,
|
||||||
|
exitible,
|
||||||
className,
|
className,
|
||||||
style,
|
style,
|
||||||
trigger = 'doubleClick',
|
trigger = 'doubleClick',
|
||||||
@@ -127,6 +135,16 @@ export const InlineEdit = ({
|
|||||||
inputRef.current?.scrollTo(0, 0);
|
inputRef.current?.scrollTo(0, 0);
|
||||||
}, [submit]);
|
}, [submit]);
|
||||||
|
|
||||||
|
const onKeyDown = useCallback(
|
||||||
|
(e: KeyboardEvent<HTMLInputElement>) => {
|
||||||
|
e.stopPropagation();
|
||||||
|
if (!exitible) return;
|
||||||
|
if (e.key !== 'Escape') return;
|
||||||
|
inputRef.current?.blur();
|
||||||
|
},
|
||||||
|
[exitible]
|
||||||
|
);
|
||||||
|
|
||||||
const inputHandler = useCallback(
|
const inputHandler = useCallback(
|
||||||
(v: string) => {
|
(v: string) => {
|
||||||
setEditingValue(v);
|
setEditingValue(v);
|
||||||
@@ -198,6 +216,7 @@ export const InlineEdit = ({
|
|||||||
placeholder={placeholder}
|
placeholder={placeholder}
|
||||||
onBlur={onBlur}
|
onBlur={onBlur}
|
||||||
onEnter={onEnter}
|
onEnter={onEnter}
|
||||||
|
onKeyDown={onKeyDown}
|
||||||
onChange={inputHandler}
|
onChange={inputHandler}
|
||||||
style={inputWrapperInheritsStyles}
|
style={inputWrapperInheritsStyles}
|
||||||
inputStyle={inputInheritsStyles}
|
inputStyle={inputInheritsStyles}
|
||||||
|
|||||||
@@ -43,6 +43,7 @@ export const BlocksuiteHeaderTitle = (props: BlockSuiteHeaderTitleProps) => {
|
|||||||
value={title}
|
value={title}
|
||||||
onChange={onChange}
|
onChange={onChange}
|
||||||
editable={!isPublic}
|
editable={!isPublic}
|
||||||
|
exitible={true}
|
||||||
placeholder="Untitled"
|
placeholder="Untitled"
|
||||||
data-testid="title-edit-button"
|
data-testid="title-edit-button"
|
||||||
handleRef={inputHandleRef}
|
handleRef={inputHandleRef}
|
||||||
|
|||||||
Reference in New Issue
Block a user