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
124 lines (116 loc) · 4.43 KB
/
index.tsx
File metadata and controls
124 lines (116 loc) · 4.43 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
import ConfigurableFormItem from '@/components/formDesigner/components/formItem';
import ComponentsContainer from '@/components/formDesigner/containers/componentsContainer';
import { migrateCustomFunctions, migratePropertyName } from '@/designer-components/_common-migrations/migrateSettings';
import { IInputStyles } from '@/providers';
import { evaluateString, validateConfigurableComponentSettings } from '@/providers/form/utils';
import ParentProvider from '@/providers/parentProvider/index';
import { LinkOutlined } from '@ant-design/icons';
import React, { CSSProperties, ReactNode } from 'react';
import { migrateFormApi } from '../_common-migrations/migrateFormApi1';
import { migratePrevStyles } from '../_common-migrations/migrateStyles';
import { ILinkProps, LinkComponentDefinition } from './interfaces';
import { getSettings } from './settingsForm';
import { defaultStyles } from './utils';
const LinkComponent: LinkComponentDefinition = {
type: 'link',
isInput: false,
name: 'link',
preserveDimensionsInDesigner: true,
icon: <LinkOutlined />,
calculateModel: (model, allData) => ({
isDesignerMode: allData.form.formMode === 'designer',
href: evaluateString(model.href, allData.data),
}),
Factory: ({ model, calculatedModel }) => {
const {
content = 'Link',
target,
direction,
justifyContent,
id,
alignItems,
justifyItems,
hasChildren,
} = model;
// Create link container style with textAlign from fontStyles
const linkStyle: CSSProperties = {};
if (direction === 'horizontal' && justifyContent) {
linkStyle['display'] = 'flex';
linkStyle['justifyContent'] = justifyContent;
linkStyle['alignItems'] = alignItems;
linkStyle['justifyItems'] = justifyItems;
}
if (model.hidden) return null;
return (
<ConfigurableFormItem model={model}>
{() => {
if (!hasChildren) {
return (
<div style={{ ...linkStyle, alignItems: 'center', display: 'flex', height: '100%' }}>
<a href={calculatedModel.href} target={target} className="sha-link" style={{ ...model.allStyles.fullStyle, height: 'unset' }}>
{content}
</a>
</div>
);
}
const containerHolder = (): ReactNode => (
<ParentProvider model={model}>
<ComponentsContainer
style={{ ...linkStyle, ...model.allStyles.fullStyle }}
containerId={id}
direction={direction}
justifyContent={model.direction === 'horizontal' ? model?.justifyContent : null}
alignItems={model.direction === 'horizontal' ? model?.alignItems : null}
justifyItems={model.direction === 'horizontal' ? model?.justifyItems : null}
className={model.className}
itemsLimit={1}
dynamicComponents={model?.isDynamic ? model?.components : []}
/>
</ParentProvider>
);
if (calculatedModel.isDesignerMode) {
return containerHolder();
}
return (
<a href={calculatedModel.href} target={target} className="sha-link">
{containerHolder()}
</a>
);
}}
</ConfigurableFormItem>
);
},
settingsFormMarkup: getSettings,
validateSettings: (model) => validateConfigurableComponentSettings(getSettings, model),
initModel: (model: ILinkProps) => {
const customProps: ILinkProps = {
...model,
direction: 'vertical',
target: '_self',
justifyContent: 'left',
hideLabel: true,
};
return customProps;
},
migrator: (m) =>
m
.add<ILinkProps>(0, (prev) => ({ ...prev }) as ILinkProps)
.add<ILinkProps>(1, (prev) => {
return {
...prev,
label: prev.label ?? prev['name'],
href: prev.content,
content: prev['name'],
};
})
.add<ILinkProps>(2, (prev) => migratePropertyName(migrateCustomFunctions(prev)))
.add<ILinkProps>(3, (prev) => ({ ...migrateFormApi.properties(prev) }))
.add<ILinkProps>(4, (prev) => {
const styles: IInputStyles = {
style: prev.style,
};
return { ...prev, desktop: { ...styles }, tablet: { ...styles }, mobile: { ...styles } };
})
.add<ILinkProps>(5, (prev) => ({ ...migratePrevStyles(prev, defaultStyles()) })),
};
export default LinkComponent;