|
| 1 | +import { useTheme } from '@emotion/react'; |
| 2 | +import styled from '@emotion/styled'; |
| 3 | +import { useDarkMode } from '@leafygreen-ui/leafygreen-provider'; |
| 4 | +import { spacing, color } from '@leafygreen-ui/tokens'; |
| 5 | +import { Select, Option } from '@leafygreen-ui/select'; |
| 6 | +import Icon from '@leafygreen-ui/icon'; |
| 7 | +import { useEffect, useRef, useState } from 'react'; |
| 8 | + |
| 9 | +import { ellipsisTruncation } from '@/styles/styles'; |
| 10 | +import { FieldTypeContent } from '@/components/field/field-type-content'; |
| 11 | +import { FieldId } from '@/types'; |
| 12 | +import { useEditableDiagramInteractions } from '@/hooks/use-editable-diagram-interactions'; |
| 13 | + |
| 14 | +const FieldTypeWrapper = styled.div<{ color: string }>` |
| 15 | + color: ${props => props.color}; |
| 16 | + font-weight: normal; |
| 17 | + padding-left:${spacing[100]}px; |
| 18 | + padding-right ${spacing[50]}px; |
| 19 | + flex: 0 0 ${spacing[200] * 10}px; |
| 20 | + display: flex; |
| 21 | + justify-content: flex-end; |
| 22 | + align-items: center; |
| 23 | +`; |
| 24 | + |
| 25 | +const FieldContentWrapper = styled.div` |
| 26 | + max-width: ${spacing[200] * 10}px; |
| 27 | + ${ellipsisTruncation} |
| 28 | +`; |
| 29 | + |
| 30 | +const CaretIconWrapper = styled.div` |
| 31 | + display: flex; |
| 32 | +`; |
| 33 | + |
| 34 | +const StyledSelect = styled(Select)` |
| 35 | + visibility: hidden; |
| 36 | + height: 0; |
| 37 | + width: 0; |
| 38 | + & > button { |
| 39 | + height: 0; |
| 40 | + width: 0; |
| 41 | + border: none; |
| 42 | + box-shadow: none; |
| 43 | + } |
| 44 | +`; |
| 45 | + |
| 46 | +export function FieldType({ |
| 47 | + id, |
| 48 | + type, |
| 49 | + nodeId, |
| 50 | + isEditing, |
| 51 | + isDisabled, |
| 52 | + onChange, |
| 53 | +}: { |
| 54 | + id: FieldId; |
| 55 | + nodeId: string; |
| 56 | + type: string | string[] | undefined; |
| 57 | + isEditing: boolean; |
| 58 | + isDisabled: boolean; |
| 59 | + onChange: (newType: string[]) => void; |
| 60 | +}) { |
| 61 | + const internalTheme = useTheme(); |
| 62 | + const { theme } = useDarkMode(); |
| 63 | + const { fieldTypes } = useEditableDiagramInteractions(); |
| 64 | + const [isSelectOpen, setIsSelectOpen] = useState(false); |
| 65 | + const fieldTypeRef = useRef<HTMLDivElement>(null); |
| 66 | + |
| 67 | + useEffect(() => { |
| 68 | + if (!isEditing) { |
| 69 | + setIsSelectOpen(false); |
| 70 | + } |
| 71 | + }, [isEditing]); |
| 72 | + |
| 73 | + const getSecondaryTextColor = () => { |
| 74 | + if (isDisabled) { |
| 75 | + return internalTheme.node.disabledColor; |
| 76 | + } |
| 77 | + return color[theme].text.secondary.default; |
| 78 | + }; |
| 79 | + |
| 80 | + return ( |
| 81 | + <FieldTypeWrapper |
| 82 | + ref={fieldTypeRef} |
| 83 | + {...(isEditing |
| 84 | + ? { |
| 85 | + onClick: () => setIsSelectOpen(!isSelectOpen), |
| 86 | + } |
| 87 | + : undefined)} |
| 88 | + color={getSecondaryTextColor()} |
| 89 | + > |
| 90 | + {/** |
| 91 | + * Rendering hidden select first so that whenever popover shows it, its relative |
| 92 | + * to the field type position. LG Select does not provide a way to set the |
| 93 | + * position of the popover using refs. |
| 94 | + */} |
| 95 | + {isEditing && ( |
| 96 | + <StyledSelect |
| 97 | + aria-label="Select field type" |
| 98 | + size="xsmall" |
| 99 | + renderMode="portal" |
| 100 | + open={isSelectOpen} |
| 101 | + onChange={val => { |
| 102 | + if (val) { |
| 103 | + // Currently its a single select, so we are returning it as an array. |
| 104 | + // That way once we have multi-select support, we don't need to change |
| 105 | + // the API and it should work seemlessly for clients. |
| 106 | + // Trigger onChange only if the value is different |
| 107 | + if (type !== val) { |
| 108 | + onChange([val]); |
| 109 | + } |
| 110 | + setIsSelectOpen(false); |
| 111 | + } |
| 112 | + }} |
| 113 | + // As its not multi-select, we can just use the first value. Once LG-5657 |
| 114 | + // is implemented, we can use ComboBox component for multi-select support |
| 115 | + value={Array.isArray(type) ? type[0] : type || ''} |
| 116 | + allowDeselect={false} |
| 117 | + dropdownWidthBasis="option" |
| 118 | + tabIndex={0} |
| 119 | + > |
| 120 | + {fieldTypes!.map(fieldType => ( |
| 121 | + <Option key={fieldType} value={fieldType}> |
| 122 | + {fieldType} |
| 123 | + </Option> |
| 124 | + ))} |
| 125 | + </StyledSelect> |
| 126 | + )} |
| 127 | + <FieldContentWrapper> |
| 128 | + <FieldTypeContent type={type} nodeId={nodeId} id={id} isAddFieldToObjectDisabled={isEditing} /> |
| 129 | + </FieldContentWrapper> |
| 130 | + {isEditing && ( |
| 131 | + <CaretIconWrapper title="Select field type" aria-label="Select field type"> |
| 132 | + <Icon glyph="CaretDown" /> |
| 133 | + </CaretIconWrapper> |
| 134 | + )} |
| 135 | + </FieldTypeWrapper> |
| 136 | + ); |
| 137 | +} |
0 commit comments