-
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
14 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 cfcae3b
Merge branch 'main' of https://github.com/mongodb-js/diagramming into…
mabaasit ffd1419
yarn lock
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
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,124 @@ | ||
| import { useTheme } from '@emotion/react'; | ||
| import styled from '@emotion/styled'; | ||
| import { useDarkMode } from '@leafygreen-ui/leafygreen-provider'; | ||
| import { color, spacing as LGSpacing } from '@leafygreen-ui/tokens'; | ||
| import { Select, Option } from '@leafygreen-ui/select'; | ||
| import Icon from '@leafygreen-ui/icon'; | ||
| import { useMemo, 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: ${props => props.color}; | ||
| font-weight: normal; | ||
| padding-left:${LGSpacing[100]}px; | ||
| padding-right ${LGSpacing[50]}px; | ||
| flex: 0 0 ${LGSpacing[200] * 10}px; | ||
| display: flex; | ||
| justify-content: flex-end; | ||
| align-items: center; | ||
| `; | ||
|
|
||
| const FieldContentWrapper = styled.div` | ||
| max-width: ${LGSpacing[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, | ||
| isDisabled, | ||
| isEditable, | ||
| }: { | ||
| id: FieldId; | ||
| nodeId: string; | ||
| type: string | string[] | undefined; | ||
| isDisabled: boolean; | ||
| isEditable: boolean; | ||
| }) { | ||
| const internalTheme = useTheme(); | ||
| const { theme } = useDarkMode(); | ||
| const { onChangeFieldType, fieldTypes } = useEditableDiagramInteractions(); | ||
| const [isSelectOpen, setIsSelectOpen] = useState(false); | ||
|
|
||
| const getSecondaryTextColor = () => { | ||
| if (isDisabled) { | ||
| return internalTheme.node.disabledColor; | ||
| } | ||
| return color[theme].text.secondary.default; | ||
| }; | ||
|
|
||
| const isFieldTypeEditable = useMemo(() => { | ||
| return isEditable && !isDisabled && !!onChangeFieldType && (fieldTypes ?? []).length > 0; | ||
| }, [onChangeFieldType, isDisabled, isEditable, fieldTypes]); | ||
|
|
||
| return ( | ||
| <FieldTypeWrapper | ||
| {...(isFieldTypeEditable | ||
| ? { | ||
| 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. | ||
| */} | ||
| {isFieldTypeEditable && ( | ||
| <StyledSelect | ||
| aria-label="Select field type" | ||
| size="xsmall" | ||
| renderMode="portal" | ||
| open={isSelectOpen} | ||
| setOpen={setIsSelectOpen} | ||
| onChange={val => { | ||
| if (val) { | ||
| onChangeFieldType?.(nodeId, Array.isArray(id) ? id : [id], val); | ||
| } | ||
| }} | ||
| // As its not multi-select, we can just use the first value | ||
| value={Array.isArray(type) ? type[0] : type || ''} | ||
| allowDeselect={false} | ||
| dropdownWidthBasis="option" | ||
| justify="middle" | ||
| > | ||
| {fieldTypes!.map(fieldType => ( | ||
| <Option key={fieldType} value={fieldType}> | ||
| {fieldType} | ||
| </Option> | ||
| ))} | ||
| </StyledSelect> | ||
| )} | ||
| <FieldContentWrapper> | ||
| <FieldTypeContent type={type} nodeId={nodeId} id={id} /> | ||
| </FieldContentWrapper> | ||
| {isFieldTypeEditable && ( | ||
| <CaretIconWrapper title="Select field type" aria-label="Select field type"> | ||
| <Icon glyph="CaretDown" /> | ||
| </CaretIconWrapper> | ||
| )} | ||
| </FieldTypeWrapper> | ||
| ); | ||
| } |
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
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!