-
Couldn't load subscription status.
- Fork 1
feat/COMPASS-9798 inline field name editing #136
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
Changes from all commits
9cf47c6
d3de8e6
a866b3e
388d31a
92445a5
fb550a3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| import { styled } from 'storybook/internal/theming'; | ||
| import { useCallback, useEffect, useRef, useState } from 'react'; | ||
|
|
||
| import { ellipsisTruncation } from '@/styles/styles'; | ||
| import { DEFAULT_FIELD_HEIGHT } from '@/utilities/constants'; | ||
|
|
||
| const InnerFieldName = styled.div` | ||
| width: 100%; | ||
| min-height: ${DEFAULT_FIELD_HEIGHT}px; | ||
| ${ellipsisTruncation} | ||
| `; | ||
|
|
||
| const InlineInput = styled.input` | ||
| border: none; | ||
| background: none; | ||
| height: ${DEFAULT_FIELD_HEIGHT}px; | ||
| color: inherit; | ||
| font-size: inherit; | ||
| font-family: inherit; | ||
| font-style: inherit; | ||
| `; | ||
|
|
||
| interface FieldNameProps { | ||
| name: string; | ||
| isEditable?: boolean; | ||
| onChange?: (newName: string) => void; | ||
| onBlur?: () => void; | ||
| } | ||
|
|
||
| export const FieldNameContent = ({ name, isEditable, onChange }: FieldNameProps) => { | ||
| const [isEditing, setIsEditing] = useState(false); | ||
| const [value, setValue] = useState(name); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would name ever change externally? Would we need a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might actually, we have a sidebar where they can edit too |
||
| const textInputRef = useRef<HTMLInputElement>(null); | ||
|
|
||
| const handleSubmit = useCallback(() => { | ||
| setIsEditing(false); | ||
| onChange?.(value); | ||
| }, [value, onChange]); | ||
|
|
||
| const handleKeyboardEvent = useCallback( | ||
| (e: React.KeyboardEvent<HTMLInputElement>) => { | ||
| if (e.key === 'Enter') handleSubmit(); | ||
| if (e.key === 'Escape') { | ||
| setValue(name); | ||
| setIsEditing(false); | ||
| } | ||
| }, | ||
| [handleSubmit, name], | ||
| ); | ||
|
|
||
| const handleNameDoubleClick = useCallback(() => { | ||
| setIsEditing(true); | ||
| }, []); | ||
|
|
||
| const handleChange = useCallback((e: React.ChangeEvent<HTMLInputElement>) => { | ||
| setValue(e.target.value); | ||
| }, []); | ||
|
|
||
| useEffect(() => { | ||
| if (isEditing) { | ||
| setTimeout(() => { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is to postpone the focus action until the input is rendered. I also wasn't sure if it was needed but apparently it is. From the docs:
|
||
| textInputRef.current?.focus(); | ||
| textInputRef.current?.select(); | ||
| }); | ||
| } | ||
| }, [isEditing]); | ||
|
|
||
| return isEditing ? ( | ||
| <InlineInput | ||
| type="text" | ||
| ref={textInputRef} | ||
| value={value} | ||
| onChange={handleChange} | ||
| onBlur={handleSubmit} | ||
| onKeyDown={handleKeyboardEvent} | ||
| title="Edit field name" | ||
| /> | ||
| ) : ( | ||
| <InnerFieldName onDoubleClick={onChange && isEditable ? handleNameDoubleClick : undefined}>{value}</InnerFieldName> | ||
| ); | ||
| }; | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be better to have two seperate components, one which is editable and the other (the current flow) which is not? At the moment we're passing in quite a few (optional) props which require a conditional check, especially in that
useEffect. I could see someone accidentally adding in a change which re-renders this whole component too, rather than just the edited component and vice-versaThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm on the edge about this one, the split components could accidentally start diverging more than intended (even in the same app we'll have editable & not editable fields - like
_id, so we expect them to be consistent)