Skip to content

Commit

Permalink
feat(EditableTypography): allow autoselect of text when going into ed…
Browse files Browse the repository at this point in the history
…it mode

this affects both EditableText and EditableHeading as well
  • Loading branch information
YossiSaadi authored Dec 30, 2024
1 parent 67ef600 commit 7b59e7b
Showing 1 changed file with 13 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export interface EditableTypographyImplementationProps {
ariaLabel?: string;
/** Controls the mode of the component (i.e. view/edit mode) */
isEditMode?: boolean;
/** If true, automatically select all text when entering edit mode */
autoSelectTextOnEditMode?: boolean;
/** Will be called when the mode of the component changes */
onEditModeChange?: (isEditMode: boolean) => void;
/** Override Tooltip props when needed */
Expand Down Expand Up @@ -61,6 +63,7 @@ const EditableTypography: VibeComponent<EditableTypographyProps, HTMLElement> =
typographyClassName,
component: TypographyComponent,
isEditMode,
autoSelectTextOnEditMode,
onEditModeChange,
tooltipProps,
type,
Expand Down Expand Up @@ -145,16 +148,20 @@ const EditableTypography: VibeComponent<EditableTypographyProps, HTMLElement> =
const toggleKeyboardEditMode = useKeyboardButtonPressedFunc(toggleEditMode);

function focus() {
if (inputRef.current) {
inputRef.current?.focus();
}
inputRef.current?.focus?.();
}

function selectAllInputText() {
inputRef.current?.select?.();
}

useEffect(() => {
if (isEditing) {
focus();
if (!isEditing) return;
focus();
if (autoSelectTextOnEditMode) {
selectAllInputText();
}
}, [isEditing]);
}, [autoSelectTextOnEditMode, isEditing]);

useEffect(() => {
if (!typographyRef.current) {
Expand Down

0 comments on commit 7b59e7b

Please sign in to comment.