forked from shesha-io/shesha-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.tsx
More file actions
151 lines (139 loc) · 5.56 KB
/
index.tsx
File metadata and controls
151 lines (139 loc) · 5.56 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
146
147
148
149
150
151
import ComponentsContainer from '@/components/formDesigner/containers/componentsContainer';
import React, { Fragment } from 'react';
import ShaIcon from '@/components/shaIcon';
import { FolderOutlined } from '@ant-design/icons';
import { useAvailableConstantsData } from '@/providers/form/utils';
import { IFormComponentContainer } from '@/providers/form/models';
import { ITabsComponentProps } from './models';
import { IToolboxComponent } from '@/interfaces';
import { migrateCustomFunctions, migratePropertyName, migrateReadOnly } from '@/designer-components/_common-migrations/migrateSettings';
import { nanoid } from '@/utils/uuid';
import { Tabs, TabsProps } from 'antd';
import { useDeepCompareMemo } from '@/hooks';
import { useSheshaApplication } from '@/providers';
import ParentProvider from '@/providers/parentProvider/index';
import { migrateFormApi } from '../_common-migrations/migrateFormApi1';
import { removeComponents } from '../_common-migrations/removeComponents';
import { getSettings } from './settingsForm';
import { defaultCardStyles, defaultStyles } from './utils';
import { useStyles } from './styles';
import { migratePrevStyles } from '../_common-migrations/migrateStyles';
import { useFormComponentStyles } from '@/hooks/formComponentHooks';
type TabItem = TabsProps['items'][number];
const TabsComponent: IToolboxComponent<ITabsComponentProps> = {
type: 'tabs',
isInput: false,
name: 'Tabs',
icon: <FolderOutlined />,
Factory: ({ model }) => {
const { anyOfPermissionsGranted } = useSheshaApplication();
const allData = useAvailableConstantsData();
const { tabs, defaultActiveKey, tabType = 'card', size, tabPosition = 'top', tabLineColor } = model;
const actionKey = defaultActiveKey || (tabs?.length && tabs[0]?.key);
const cardStyles = useFormComponentStyles({ ...model.card });
const { styles } = useStyles({ styles: model.allStyles.fullStyle, cardStyles: tabType === 'line' ? { ...cardStyles.fontStyles, ...cardStyles.dimensionsStyles, } : cardStyles.fullStyle, position: tabPosition, tabType, tabLineColor, overflow: model.allStyles.overflowStyles });
const items = useDeepCompareMemo(() => {
const tabItems: TabItem[] = [];
(tabs ?? [])?.forEach((item) => {
const {
id,
key,
title,
icon,
closable,
className,
forceRender,
animated,
destroyInactiveTabPane,
closeIcon,
permissions,
hidden,
readOnly,
selectMode,
components,
} = item;
const granted = anyOfPermissionsGranted(permissions || []);
if ((!granted || hidden) && allData.form?.formMode !== 'designer') return;
const tab: TabItem = {
key: key,
label: icon ? (
<Fragment>
<ShaIcon iconName={icon as any} /> {title}
</Fragment>
) : (
<Fragment>
{icon} {title}
</Fragment>
),
closable: closable,
className: className,
forceRender: forceRender,
animated: animated,
destroyInactiveTabPane: destroyInactiveTabPane,
closeIcon: closeIcon ? <ShaIcon iconName={closeIcon as any} /> : null,
disabled: selectMode === 'readOnly' || selectMode === 'inherited' && readOnly,
children: (
<ParentProvider model={item}>
<ComponentsContainer
containerId={id} dynamicComponents={model?.isDynamic ? components : []} />
</ParentProvider>
),
};
tabItems.push(tab);
});
return tabItems;
}, [tabs]);
return model.hidden ? null : (
<Tabs defaultActiveKey={actionKey} size={size} type={tabType} tabPosition={tabPosition} items={items} className={styles.content} />
);
},
initModel: (model) => {
const id = nanoid();
const tabsModel: ITabsComponentProps = {
...model,
propertyName: 'custom Name',
tabPosition: "top",
tabs: [{
id: id,
name: 'Tab 1',
key: id,
title: 'Tab 1',
editMode: 'inherited',
selectMode: 'editable',
components: []
}],
};
return tabsModel;
},
migrator: (m) => m
.add<ITabsComponentProps>(0, (prev) => {
const newModel = { ...prev };
newModel['tabs'] = prev['tabs']?.map((item) => migrateCustomFunctions(item as any));
return migratePropertyName(migrateCustomFunctions(newModel)) as ITabsComponentProps;
})
.add<ITabsComponentProps>(1, (prev) => {
const newModel = { ...prev };
newModel.tabs = newModel.tabs.map(x => migrateReadOnly(x, 'inherited'));
return newModel;
})
.add<ITabsComponentProps>(2, (prev) => ({ ...migrateFormApi.properties(prev) }))
.add<ITabsComponentProps>(3, (prev) => removeComponents(prev))
.add<ITabsComponentProps>(4, (prev) => {
const newModel = migratePrevStyles(prev, defaultStyles);
const initialCardStyle = { ...defaultCardStyles, font: { ...defaultCardStyles.font, color: '#000000' } };
return {
...newModel,
overflow: true,
card: { ...initialCardStyle },
desktop: { ...newModel.desktop, card: { ...initialCardStyle } },
tablet: { ...newModel.tablet, card: { ...initialCardStyle } },
mobile: { ...newModel.mobile, card: { ...initialCardStyle } }
};
}),
settingsFormMarkup: () => getSettings(),
customContainerNames: ['tabs'],
getContainers: (model) => {
return model.tabs.map<IFormComponentContainer>((t) => ({ id: t.id }));
},
};
export default TabsComponent;