-
Notifications
You must be signed in to change notification settings - Fork 46
/
Form.tsx
159 lines (151 loc) · 4.84 KB
/
Form.tsx
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
/*******************************************************************************
* Copyright (c) 2019, 2023 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
import { makeStyles } from '@material-ui/core/styles';
import Tab from '@material-ui/core/Tab';
import Tabs from '@material-ui/core/Tabs';
import React, { useEffect, useState } from 'react';
import { Page } from '../pages/Page';
import { ToolbarAction } from '../toolbaraction/ToolbarAction';
import { FormProps, FormState } from './Form.types';
const useFormStyles = makeStyles((theme) => ({
form: {
display: 'flex',
flexDirection: 'column',
paddingLeft: theme.spacing(1),
paddingRight: theme.spacing(1),
},
tabsRoot: {
minHeight: theme.spacing(4),
borderBottomColor: theme.palette.divider,
borderBottomWidth: '1px',
borderBottomStyle: 'solid',
},
tabRoot: {
minHeight: theme.spacing(4),
textTransform: 'none',
},
tabLabel: {
display: 'flex',
flexDirection: 'row',
alignItems: 'center',
width: 'inherit',
},
tabLabelText: {
textOverflow: 'ellipsis',
whiteSpace: 'nowrap',
overflow: 'hidden',
},
toolbar: {
display: 'flex',
flexDirection: 'row',
justifyContent: 'flex-end',
alignItems: 'center',
},
toolbarAction: {
paddingRight: theme.spacing(1),
whiteSpace: 'nowrap',
},
pagesListAndToolbar: {
display: 'flex',
justifyContent: 'space-between',
paddingLeft: theme.spacing(1),
},
}));
const a11yProps = (id: string) => {
return {
id: `simple-tab-${id}`,
'aria-controls': `simple-tabpanel-${id}`,
};
};
export const Form = ({ editingContextId, form, widgetSubscriptions, setSelection, readOnly }: FormProps) => {
const classes = useFormStyles();
const { id, pages } = form;
const [state, setState] = useState<FormState>({ selectedPage: pages[0], pages });
useEffect(() => {
setState(() => {
const selectedPage = pages.find((page) => page.id === state.selectedPage?.id);
if (selectedPage) {
return { selectedPage, pages };
}
return { selectedPage: pages[0], pages };
});
}, [pages, state.selectedPage.id]);
const onChangeTab = (_: React.ChangeEvent<{}>, value: string) => {
const selectedPage = pages.find((page) => page.id === value);
setState((prevState) => {
return { ...prevState, selectedPage };
});
};
let selectedPageToolbar: JSX.Element | null = null;
if (state.selectedPage?.toolbarActions?.length > 0) {
selectedPageToolbar = (
<div className={classes.toolbar}>
{state.selectedPage.toolbarActions.map((toolbarAction) => (
<div className={classes.toolbarAction} key={toolbarAction.id}>
<ToolbarAction editingContextId={editingContextId} formId={id} readOnly={readOnly} widget={toolbarAction} />
</div>
))}
</div>
);
}
let page: JSX.Element | null = null;
if (state.selectedPage) {
page = (
<Page
editingContextId={editingContextId}
formId={id}
page={state.selectedPage}
widgetSubscriptions={widgetSubscriptions}
setSelection={setSelection}
readOnly={readOnly}
/>
);
}
const maxWidth: number = state.pages.length > 1 ? 100 : 390; // 390 is the maxWidth to fit to the default Details view
const variant: 'scrollable' | 'standard' = state.pages.length > 1 ? 'scrollable' : 'standard';
return (
<div data-testid="form" className={classes.form}>
<div className={classes.pagesListAndToolbar}>
<Tabs
classes={{ root: classes.tabsRoot }}
value={state.selectedPage.id}
onChange={onChangeTab}
variant={variant}
scrollButtons="on"
textColor="primary"
indicatorColor="primary">
{state.pages.map((page) => {
return (
<Tab
{...a11yProps(page.id)}
classes={{ root: classes.tabRoot }}
style={{ minWidth: 1, maxWidth: maxWidth }} // Set minWidth to one to force tab width to fit the page label length
value={page.id}
title={page.label}
label={
<div className={classes.tabLabel}>
<div className={classes.tabLabelText}>{page.label}</div>
</div>
}
key={page.id}
data-testid={`page-tab-${page.label}`}
/>
);
})}
</Tabs>
{selectedPageToolbar}
</div>
{page}
</div>
);
};