Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Adds onModeChange callback to EditableText #939

Merged
merged 1 commit into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/hungry-years-pump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@autoguru/overdrive': patch
---

EditableText exposes an onModeChange callback
31 changes: 22 additions & 9 deletions packages/overdrive/lib/components/EditableText/EditableText.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import * as inputStyles from '../private/InputBase/withEnhancedInput.css';

import * as styles from './EditableText.css';

type BoxProps = Pick<ComponentProps<typeof Box>, 'display' | 'onFocus' | 'onBlur'>;
type BoxProps = Pick<ComponentProps<typeof Box>, 'display' | 'onFocus' | 'onBlur' | 'onKeyDown'>;
type TextProps = Pick<
ComponentProps<typeof Text>,
'is' | 'colour' | 'size' | 'children' | 'noWrap'
Expand All @@ -22,14 +22,18 @@ type InputProps = Omit<
| 'height'
| 'onFocus'
| 'onBlur'
| 'onKeyDown'
| keyof TextProps
| keyof BoxProps
>;

export interface Props extends TextProps, InputProps, BoxProps {
className?: string;

onModeChange?: (mode: InputMode) => void;
}

type InputMode = 'TEXT' | 'INPUT';
export const EditableText = forwardRef<HTMLAnchorElement, Props>(
(
{
Expand All @@ -40,13 +44,20 @@ export const EditableText = forwardRef<HTMLAnchorElement, Props>(
value,
onFocus,
onBlur,
onKeyDown,
onModeChange,
...inputProps
},
ref,
) => {
const textRef = useRef<HTMLSpanElement>(null);
const [isEditing, setIsEditing] = useState(false);
const onRequestEdit = () => setIsEditing(true);
const [mode, setMode] = useState<InputMode>('TEXT');
const onRequestModeChange = (newMode: InputMode) => {
setMode(newMode);
if (typeof onModeChange === 'function') {
onModeChange(newMode);
}
};
const textStyles = useTextStyles({
is,
colour,
Expand All @@ -64,24 +75,26 @@ export const EditableText = forwardRef<HTMLAnchorElement, Props>(
display={display}
position="relative"
className={styles.root}
onClick={onRequestEdit}
onClick={() => onRequestModeChange('INPUT')}
onFocus={(e) => {
onRequestEdit();
if (typeof onFocus === 'function')
onFocus(e);
onRequestModeChange('INPUT');
}}
onBlur={(e) => {
setIsEditing(false);
if (typeof onBlur === 'function')
onBlur(e);
onRequestModeChange('TEXT');
}}
onKeyDown={(e) => {
if (typeof onKeyDown === 'function')
onKeyDown(e);
if (e.key === 'Enter' || e.key === 'Escape') {
setIsEditing(false);
onRequestModeChange('TEXT');
}
}}
>
{isEditing && (
{mode === 'INPUT' && (
<Box
is="input"
{...inputProps}
Expand All @@ -101,7 +114,7 @@ export const EditableText = forwardRef<HTMLAnchorElement, Props>(
colour={colour}
size={size}
className={clsx(textStyles, styles.text, {
[styles.textHidden]: isEditing,
[styles.textHidden]: mode === 'INPUT',
})}
>
{value}
Expand Down
Loading