-
Notifications
You must be signed in to change notification settings - Fork 20
/
Group.js
174 lines (143 loc) · 4.19 KB
/
Group.js
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import {
useCallback,
useContext,
useEffect,
useRef,
useState
} from 'preact/hooks';
import Tooltip from './entries/Tooltip';
import classnames from 'classnames';
import {
query as domQuery
} from 'min-dom';
import {
isFunction
} from 'min-dash';
import {
useErrors,
useLayoutState
} from '../hooks';
import { PropertiesPanelContext } from '../context';
import { useStickyIntersectionObserver } from '../hooks';
import { ArrowIcon } from './icons';
/**
* @param {import('../PropertiesPanel').GroupDefinition} props
*/
export default function Group(props) {
const {
element,
entries = [],
id,
label,
shouldOpen = false,
} = props;
const groupRef = useRef(null);
const [ open, setOpen ] = useLayoutState(
[ 'groups', id, 'open' ],
shouldOpen
);
const onShow = useCallback(() => setOpen(true), [ setOpen ]);
const toggleOpen = () => setOpen(!open);
const [ edited, setEdited ] = useState(false);
const [ sticky, setSticky ] = useState(false);
// set edited state depending on all entries
useEffect(() => {
// TODO(@barmac): replace with CSS when `:has()` is supported in all major browsers, or rewrite as in https://github.com/camunda/camunda-modeler/issues/3815#issuecomment-1733038161
const scheduled = requestAnimationFrame(() => {
const hasOneEditedEntry = entries.find(entry => {
const {
id,
isEdited
} = entry;
const entryNode = domQuery(`[data-entry-id="${id}"]`);
if (!isFunction(isEdited) || !entryNode) {
return false;
}
const inputNode = domQuery('.bio-properties-panel-input', entryNode);
return isEdited(inputNode);
});
setEdited(hasOneEditedEntry);
});
return () => cancelAnimationFrame(scheduled);
}, [ entries, setEdited ]);
// set error state depending on all entries
const allErrors = useErrors();
const hasErrors = entries.some(entry => allErrors[entry.id]);
// set css class when group is sticky to top
useStickyIntersectionObserver(groupRef, 'div.bio-properties-panel-scroll-container', setSticky);
const propertiesPanelContext = {
...useContext(PropertiesPanelContext),
onShow
};
return <div class="bio-properties-panel-group" data-group-id={ 'group-' + id } ref={ groupRef }>
<div class={ classnames(
'bio-properties-panel-group-header',
edited ? '' : 'empty',
open ? 'open' : '',
(sticky && open) ? 'sticky' : ''
) } onClick={ toggleOpen }>
<div
title={ props.tooltip ? null : label }
data-title={ label }
class="bio-properties-panel-group-header-title"
>
<Tooltip value={ props.tooltip } forId={ 'group-' + id } element={ element } parent={ groupRef }>
{ label }
</Tooltip>
</div>
<div class="bio-properties-panel-group-header-buttons">
{
<DataMarker
edited={ edited }
hasErrors={ hasErrors }
/>
}
<button
type="button"
title="Toggle section"
class="bio-properties-panel-group-header-button bio-properties-panel-arrow"
>
<ArrowIcon class={ open ? 'bio-properties-panel-arrow-down' : 'bio-properties-panel-arrow-right' } />
</button>
</div>
</div>
<div class={ classnames(
'bio-properties-panel-group-entries',
open ? 'open' : ''
) }>
<PropertiesPanelContext.Provider value={ propertiesPanelContext }>
{
entries.map(entry => {
const {
component: Component,
id
} = entry;
return (
<Component
{ ...entry }
element={ element }
key={ id } />
);
})
}
</PropertiesPanelContext.Provider>
</div>
</div>;
}
function DataMarker(props) {
const {
edited,
hasErrors
} = props;
if (hasErrors) {
return (
<div title="Section contains an error" class="bio-properties-panel-dot bio-properties-panel-dot--error"></div>
);
}
if (edited) {
return (
<div title="Section contains data" class="bio-properties-panel-dot"></div>
);
}
return null;
}