Skip to content

Commit

Permalink
feat: Add select on focus to text field (#1072)
Browse files Browse the repository at this point in the history
Co-authored-by: JonnCh <[email protected]>
  • Loading branch information
JonnCharpentier and JonnCh authored Sep 23, 2024
1 parent 66af1b7 commit 6cc4009
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/components/SuperDrawer/components/SuperDrawerHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ interface SuperDrawerHeaderProps {
}

interface SuperDrawerHeaderStructuredProps {
title: string;
title: string | ReactNode;
left?: ReactNode;
right?: ReactNode;
hideControls?: boolean;
Expand All @@ -32,7 +32,7 @@ export function SuperDrawerHeader(props: SuperDrawerHeaderProps | SuperDrawerHea
{isStructuredProps(props) ? (
<div css={Css.df.jcsb.aic.gap2.fg1.$}>
<div css={Css.fg1.df.aic.gap2.$}>
<h1>{props.title}</h1>
{typeof props.title === "string" ? <h1>{props.title}</h1> : props.title}
{props.left}
</div>
{props.right && <div css={Css.fs0.$}>{props.right}</div>}
Expand Down
2 changes: 2 additions & 0 deletions src/inputs/TextField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export interface TextFieldProps<X> extends BeamTextFieldProps<X> {
endAdornment?: ReactNode;
startAdornment?: ReactNode;
hideErrorMessage?: boolean;
/** Allow focusing without selecting, i.e. to let the user keep typing after we've pre-filled text + called focus, like the Add New component. */
selectOnFocus?: boolean;
}

export function TextField<X extends Only<TextFieldXss, X>>(props: TextFieldProps<X>) {
Expand Down
5 changes: 4 additions & 1 deletion src/inputs/TextFieldBase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ export interface TextFieldBaseProps<X>
// Replaces empty input field and placeholder with node
// IE: Multiselect renders list of selected items in the input field
unfocusedPlaceholder?: ReactNode;
/** Allow focusing without selecting, i.e. to let the user keep typing after we've pre-filled text + called focus, like the Add New component. */
selectOnFocus?: boolean;
}

// Used by both TextField and TextArea
Expand Down Expand Up @@ -97,6 +99,7 @@ export function TextFieldBase<X extends Only<TextFieldXss, X>>(props: TextFieldB
alwaysShowHelperText = false,
fullWidth = fieldProps?.fullWidth ?? false,
unfocusedPlaceholder,
selectOnFocus = true,
} = props;

const typeScale = fieldProps?.typeScale ?? (inputProps.readOnly && labelStyle !== "hidden" ? "smMd" : "sm");
Expand Down Expand Up @@ -188,7 +191,7 @@ export function TextFieldBase<X extends Only<TextFieldXss, X>>(props: TextFieldB
}

const onFocusChained = chain((e: FocusEvent<HTMLInputElement> | FocusEvent<HTMLTextAreaElement>) => {
e.target.select();
if (selectOnFocus) e.target.select();
}, onFocus);

// Simulate clicking `ElementType` when using an unfocused placeholder
Expand Down

0 comments on commit 6cc4009

Please sign in to comment.