-
Notifications
You must be signed in to change notification settings - Fork 2
feat/COMPASS-9799 Add support to change field type #160
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
Open
mabaasit
wants to merge
12
commits into
main
Choose a base branch
from
feature/COMPASS-9799-editable-field-type
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+744
−82
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
c0b18f7
feat: add field type select
mabaasit e93bb71
position correctly
mabaasit 4d78df9
add tests
mabaasit 0a8906e
storybook
mabaasit 673333b
dismiss on click
mabaasit 74b46ce
add lg ticket
mabaasit ba27e7c
return multiple types and fix closing of popover
mabaasit 81e921a
fix test
mabaasit 8e7263e
make editable on double click
mabaasit b14eca1
clean up and testing
mabaasit 4b836d6
tests
mabaasit b436c7d
validate this compass
mabaasit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| import styled from '@emotion/styled'; | ||
| import { fontWeights } from '@leafygreen-ui/tokens'; | ||
| import { useCallback, useEffect, useRef, useState } from 'react'; | ||
|
|
||
| import { ellipsisTruncation } from '@/styles/styles'; | ||
| import { FieldDepth } from '@/components/field/field-depth'; | ||
| import { FieldType } from '@/components/field/field-type'; | ||
| import { FieldId, NodeField } from '@/types'; | ||
| import { useEditableDiagramInteractions } from '@/hooks/use-editable-diagram-interactions'; | ||
|
|
||
| import { FieldNameContent } from './field-name-content'; | ||
|
|
||
| const FieldContentWrapper = styled.div` | ||
| display: contents; | ||
| `; | ||
|
|
||
| const FieldName = styled.div` | ||
| display: flex; | ||
| flex-grow: 1; | ||
| align-items: center; | ||
| font-weight: ${fontWeights.medium}; | ||
| ${ellipsisTruncation}; | ||
| `; | ||
|
|
||
| interface FieldContentProps extends NodeField { | ||
| id: FieldId; | ||
| isEditable: boolean; | ||
| isDisabled: boolean; | ||
| nodeId: string; | ||
| } | ||
|
|
||
| export const FieldContent = ({ isEditable, isDisabled, depth = 0, name, type, id, nodeId }: FieldContentProps) => { | ||
| const [isEditing, setIsEditing] = useState(false); | ||
| const fieldContentRef = useRef<HTMLDivElement>(null); | ||
|
|
||
| const { onChangeFieldName, onChangeFieldType, fieldTypes } = useEditableDiagramInteractions(); | ||
| const handleNameChange = useCallback( | ||
| (newName: string) => onChangeFieldName?.(nodeId, Array.isArray(id) ? id : [id], newName), | ||
| [onChangeFieldName, id, nodeId], | ||
| ); | ||
| const handleTypeChange = useCallback( | ||
| (newType: string[]) => onChangeFieldType?.(nodeId, Array.isArray(id) ? id : [id], newType), | ||
| [onChangeFieldType, id, nodeId], | ||
| ); | ||
|
|
||
| const handleDoubleClick = useCallback(() => { | ||
| setIsEditing(true); | ||
| }, []); | ||
|
|
||
| useEffect(() => { | ||
| // When clicking outside of the field content while editing, stop editing. | ||
| const container = fieldContentRef.current; | ||
| const listener = (event: Event) => { | ||
| if (event.composedPath().includes(container!)) { | ||
| return; | ||
| } | ||
| setIsEditing(false); | ||
| }; | ||
|
|
||
| if (container && isEditable) { | ||
| document.addEventListener('click', listener); | ||
| } else { | ||
| document.removeEventListener('click', listener); | ||
| } | ||
| return () => { | ||
| document.removeEventListener('click', listener); | ||
| }; | ||
| }, [isEditable]); | ||
|
|
||
| useEffect(() => { | ||
| if (!isEditable) { | ||
| setIsEditing(false); | ||
| } | ||
| }, [isEditable]); | ||
|
|
||
| const isNameEditable = isEditing && isEditable && !!onChangeFieldName; | ||
| const isTypeEditable = isEditing && isEditable && !!onChangeFieldType && (fieldTypes ?? []).length > 0; | ||
|
|
||
| return ( | ||
| <FieldContentWrapper | ||
| data-testid={`field-content-${name}`} | ||
| onDoubleClick={isEditable ? handleDoubleClick : undefined} | ||
| ref={fieldContentRef} | ||
| > | ||
| <FieldName> | ||
| <FieldDepth depth={depth} /> | ||
| <FieldNameContent | ||
| name={name} | ||
| isEditing={isNameEditable} | ||
| onChange={handleNameChange} | ||
| onCancelEditing={() => setIsEditing(false)} | ||
| /> | ||
| </FieldName> | ||
| <FieldType | ||
| type={type} | ||
| nodeId={nodeId} | ||
| id={id} | ||
| isEditing={isTypeEditable} | ||
| isDisabled={isDisabled} | ||
| onChange={handleTypeChange} | ||
| /> | ||
| </FieldContentWrapper> | ||
| ); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| import { useTheme } from '@emotion/react'; | ||
| import styled from '@emotion/styled'; | ||
| import { useDarkMode } from '@leafygreen-ui/leafygreen-provider'; | ||
| import { spacing, color } from '@leafygreen-ui/tokens'; | ||
| import { Select, Option } from '@leafygreen-ui/select'; | ||
| import Icon from '@leafygreen-ui/icon'; | ||
| import { useEffect, useRef, useState } from 'react'; | ||
|
|
||
| import { ellipsisTruncation } from '@/styles/styles'; | ||
| import { FieldTypeContent } from '@/components/field/field-type-content'; | ||
| import { FieldId } from '@/types'; | ||
| import { useEditableDiagramInteractions } from '@/hooks/use-editable-diagram-interactions'; | ||
|
|
||
| const FieldTypeWrapper = styled.div<{ color: string }>` | ||
| color: ${props => props.color}; | ||
| font-weight: normal; | ||
| padding-left:${spacing[100]}px; | ||
| padding-right ${spacing[50]}px; | ||
| flex: 0 0 ${spacing[200] * 10}px; | ||
| display: flex; | ||
| justify-content: flex-end; | ||
| align-items: center; | ||
| `; | ||
|
|
||
| const FieldContentWrapper = styled.div` | ||
| max-width: ${spacing[200] * 10}px; | ||
| ${ellipsisTruncation} | ||
| `; | ||
|
|
||
| const CaretIconWrapper = styled.div` | ||
| display: flex; | ||
| `; | ||
|
|
||
| const StyledSelect = styled(Select)` | ||
| visibility: hidden; | ||
| height: 0; | ||
| width: 0; | ||
| & > button { | ||
| height: 0; | ||
| width: 0; | ||
| border: none; | ||
| box-shadow: none; | ||
| } | ||
| `; | ||
|
|
||
| export function FieldType({ | ||
| id, | ||
| type, | ||
| nodeId, | ||
| isEditing, | ||
| isDisabled, | ||
| onChange, | ||
| }: { | ||
| id: FieldId; | ||
| nodeId: string; | ||
| type: string | string[] | undefined; | ||
| isEditing: boolean; | ||
| isDisabled: boolean; | ||
| onChange: (newType: string[]) => void; | ||
| }) { | ||
| const internalTheme = useTheme(); | ||
| const { theme } = useDarkMode(); | ||
| const { fieldTypes } = useEditableDiagramInteractions(); | ||
| const [isSelectOpen, setIsSelectOpen] = useState(false); | ||
| const fieldTypeRef = useRef<HTMLDivElement>(null); | ||
|
|
||
| useEffect(() => { | ||
| if (!isEditing) { | ||
| setIsSelectOpen(false); | ||
| } | ||
| }, [isEditing]); | ||
|
|
||
| const getSecondaryTextColor = () => { | ||
| if (isDisabled) { | ||
| return internalTheme.node.disabledColor; | ||
| } | ||
| return color[theme].text.secondary.default; | ||
| }; | ||
|
|
||
| return ( | ||
| <FieldTypeWrapper | ||
| ref={fieldTypeRef} | ||
| {...(isEditing | ||
| ? { | ||
| onClick: () => setIsSelectOpen(!isSelectOpen), | ||
| } | ||
| : undefined)} | ||
| color={getSecondaryTextColor()} | ||
| > | ||
| {/** | ||
| * Rendering hidden select first so that whenever popover shows it, its relative | ||
| * to the field type position. LG Select does not provide a way to set the | ||
| * position of the popover using refs. | ||
| */} | ||
| {isEditing && ( | ||
| <StyledSelect | ||
| aria-label="Select field type" | ||
| size="xsmall" | ||
| renderMode="portal" | ||
| open={isSelectOpen} | ||
| onChange={val => { | ||
| if (val) { | ||
| // Currently its a single select, so we are returning it as an array. | ||
| // That way once we have multi-select support, we don't need to change | ||
| // the API and it should work seemlessly for clients. | ||
| // Trigger onChange only if the value is different | ||
| if (type !== val) { | ||
| onChange([val]); | ||
| } | ||
| setIsSelectOpen(false); | ||
| } | ||
| }} | ||
| // As its not multi-select, we can just use the first value. Once LG-5657 | ||
| // is implemented, we can use ComboBox component for multi-select support | ||
| value={Array.isArray(type) ? type[0] : type || ''} | ||
| allowDeselect={false} | ||
| dropdownWidthBasis="option" | ||
| tabIndex={0} | ||
| > | ||
| {fieldTypes!.map(fieldType => ( | ||
| <Option key={fieldType} value={fieldType}> | ||
| {fieldType} | ||
| </Option> | ||
| ))} | ||
| </StyledSelect> | ||
| )} | ||
| <FieldContentWrapper> | ||
| <FieldTypeContent type={type} nodeId={nodeId} id={id} isAddFieldToObjectDisabled={isEditing} /> | ||
| </FieldContentWrapper> | ||
| {isEditing && ( | ||
| <CaretIconWrapper title="Select field type" aria-label="Select field type"> | ||
| <Icon glyph="CaretDown" /> | ||
| </CaretIconWrapper> | ||
| )} | ||
| </FieldTypeWrapper> | ||
| ); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
ah, thanks for adding the titles as well!