-
Notifications
You must be signed in to change notification settings - 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
base: main
Are you sure you want to change the base?
Changes from all commits
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,77 @@ | ||
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]); | ||
|
||
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 |
||
textInputRef.current?.focus(); | ||
textInputRef.current?.select(); | ||
}); | ||
} | ||
}, [isEditing]); | ||
|
||
return isEditing ? ( | ||
<InlineInput | ||
type="text" | ||
ref={textInputRef} | ||
value={value} | ||
onChange={e => { | ||
setValue(e.target.value); | ||
}} | ||
onBlur={handleSubmit} | ||
onKeyDown={e => { | ||
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. Can we move this and the |
||
if (e.key === 'Enter') handleSubmit(); | ||
if (e.key === 'Escape') setIsEditing(false); | ||
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. To me it's more expected behaviour to reset the field name to it's original value on |
||
}} | ||
title="Edit field name" | ||
/> | ||
) : ( | ||
<InnerFieldName | ||
onDoubleClick={ | ||
onChange && isEditable | ||
Anemy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
? () => { | ||
setIsEditing(true); | ||
} | ||
: 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-versa