-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfield-content.tsx
More file actions
145 lines (128 loc) · 4.35 KB
/
field-content.tsx
File metadata and controls
145 lines (128 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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 { DiagramIconButton } from '@/components/buttons/diagram-icon-button';
import { FieldId, NodeField } from '@/types';
import { useEditableDiagramInteractions } from '@/hooks/use-editable-diagram-interactions';
import { ChevronDown } from '../icons/chevron-down';
import { ChevronUp } from '../icons/chevron-up';
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;
isExpandable?: boolean;
nodeId: string;
}
export const FieldContent = ({
isEditable,
isDisabled,
isExpandable,
depth = 0,
name,
type,
id,
nodeId,
expanded,
}: FieldContentProps) => {
const [isEditing, setIsEditing] = useState(false);
const fieldContentRef = useRef<HTMLDivElement>(null);
const { onChangeFieldName, onChangeFieldType, fieldTypes, onFieldExpandToggle } = useEditableDiagramInteractions();
const hasExpandFunctionality = !!onFieldExpandToggle;
const hasExpandButton = hasExpandFunctionality && isExpandable;
const placeholderCollapse = hasExpandFunctionality && !hasExpandButton;
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);
}, []);
const handleFieldExpandToggle = useCallback(
(event: React.MouseEvent<HTMLButtonElement>) => {
if (!onFieldExpandToggle) return;
// Don't click on the field element.
event.stopPropagation();
onFieldExpandToggle(event, nodeId, Array.isArray(id) ? id : [id], !expanded);
},
[onFieldExpandToggle, nodeId, id, expanded],
);
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}
placeholderCollapse={placeholderCollapse}
/>
{hasExpandButton && (
<DiagramIconButton
data-testid={`field-expand-toggle-${nodeId}-${typeof id === 'string' ? id : id.join('.')}`}
onClick={handleFieldExpandToggle}
aria-label={expanded ? 'Collapse Field' : 'Expand Field'}
title={expanded ? 'Collapse Field' : 'Expand Field'}
>
{expanded ? <ChevronDown /> : <ChevronUp />}
</DiagramIconButton>
)}
</FieldContentWrapper>
);
};