Skip to content

Commit 0df35af

Browse files
committed
Add expand button
1 parent fc7e580 commit 0df35af

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed

packages/compass-crud/src/components/json-editor.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,14 @@ const JSONEditor: React.FunctionComponent<JSONEditorProps> = ({
257257
onDeletionFinished,
258258
]);
259259

260+
const toggleExpandCollapse = useCallback(() => {
261+
if (doc.expanded) {
262+
doc.collapse();
263+
} else {
264+
doc.expand();
265+
}
266+
}, [doc]);
267+
260268
// Trying to change CodeMirror editor state when an update "effect" is in
261269
// progress results in an error which is why we timeout the code mirror update
262270
// itself.
@@ -296,6 +304,8 @@ const JSONEditor: React.FunctionComponent<JSONEditorProps> = ({
296304
className={cx(editorStyles, darkMode && editorDarkModeStyles)}
297305
actionsClassName={actionsGroupStyles}
298306
completer={completer}
307+
onExpand={editing ? undefined : toggleExpandCollapse}
308+
expanded={expanded}
299309
/>
300310
<DocumentList.DocumentEditActionsFooter
301311
doc={doc}

packages/compass-editor/src/action-button.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ const actionButtonStyle = css({
1515
pointerEvents: 'all',
1616
});
1717

18+
const actionCompactButtonStyle = css({
19+
'& > div:has(svg)': {
20+
paddingLeft: 3,
21+
paddingRight: 3,
22+
},
23+
});
24+
1825
const actionButtonContentStyle = css({
1926
position: 'relative',
2027
});
@@ -53,7 +60,8 @@ export const ActionButton: React.FunctionComponent<{
5360
...args: Parameters<React.MouseEventHandler<HTMLButtonElement>>
5461
) => boolean | void;
5562
'data-testid'?: string;
56-
}> = ({ label, icon, onClick, ...props }) => {
63+
compact?: boolean;
64+
}> = ({ label, icon, onClick, compact, ...props }) => {
5765
const [clickResult, setClickResult] = useState<'success' | 'error'>(
5866
'success'
5967
);
@@ -90,7 +98,7 @@ export const ActionButton: React.FunctionComponent<{
9098
aria-label={label}
9199
title={label}
92100
onClick={onButtonClick}
93-
className={actionButtonStyle}
101+
className={cx(actionButtonStyle, { [actionCompactButtonStyle]: compact })}
94102
data-testid={props['data-testid'] ?? `editor-action-${label}`}
95103
>
96104
<div className={actionButtonContentStyle}>

packages/compass-editor/src/actions-container.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ type ActionsContainerProps = {
1010
customActions?: Action[];
1111
className?: string;
1212
editorRef: RefObject<EditorRef>;
13+
onExpand?: () => void;
14+
expanded?: boolean;
1315
};
1416

1517
const actionsContainerStyle = css({
@@ -22,12 +24,19 @@ const actionsContainerStyle = css({
2224
pointerEvents: 'none',
2325
});
2426

27+
const actionsGroupItemSeparator = css({
28+
flex: '1 0 auto',
29+
pointerEvents: 'none',
30+
});
31+
2532
export const ActionsContainer = ({
2633
copyable,
2734
formattable,
2835
customActions,
2936
className,
3037
editorRef,
38+
onExpand,
39+
expanded,
3140
}: ActionsContainerProps) => {
3241
return (
3342
<div
@@ -37,6 +46,15 @@ export const ActionsContainer = ({
3746
className
3847
)}
3948
>
49+
{onExpand && (
50+
<ActionButton
51+
label={expanded ? 'Collapse all' : 'Expand all'}
52+
icon={expanded ? 'CaretDown' : 'CaretRight'}
53+
onClick={onExpand}
54+
compact
55+
/>
56+
)}
57+
<span className={actionsGroupItemSeparator}></span>
4058
{copyable && (
4159
<ActionButton
4260
label="Copy"

packages/compass-editor/src/editor.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1399,6 +1399,8 @@ type MultilineEditorProps = EditorProps & {
13991399
formattable?: boolean;
14001400
editorClassName?: string;
14011401
actionsClassName?: string;
1402+
onExpand?: () => void;
1403+
expanded?: boolean;
14021404
};
14031405

14041406
const MultilineEditor = React.forwardRef<EditorRef, MultilineEditorProps>(
@@ -1411,8 +1413,10 @@ const MultilineEditor = React.forwardRef<EditorRef, MultilineEditorProps>(
14111413
editorClassName,
14121414
actionsClassName,
14131415
darkMode: _darkMode,
1416+
onExpand,
1417+
expanded,
14141418
...props
1415-
},
1419+
}: MultilineEditorProps,
14161420
ref
14171421
) {
14181422
const darkMode = useDarkMode(_darkMode);
@@ -1492,6 +1496,8 @@ const MultilineEditor = React.forwardRef<EditorRef, MultilineEditorProps>(
14921496
></BaseEditor>
14931497
{hasActions && (
14941498
<ActionsContainer
1499+
onExpand={onExpand}
1500+
expanded={expanded}
14951501
copyable={copyable}
14961502
formattable={formattable}
14971503
editorRef={editorRef}

0 commit comments

Comments
 (0)