-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathVisualizationOptions.js
More file actions
159 lines (150 loc) · 4.8 KB
/
VisualizationOptions.js
File metadata and controls
159 lines (150 loc) · 4.8 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
152
153
154
155
156
157
158
159
import {
ButtonStrip,
Modal,
ModalTitle,
ModalContent,
ModalActions,
Button,
FieldSet,
Legend,
TabBar,
Tab,
Help,
} from '@dhis2/ui'
import PropTypes from 'prop-types'
import React, { useState } from 'react'
import i18n from '../../locales/index.js'
import {
modalContent,
tabSection,
tabSectionTitle,
tabSectionTitleDisabled,
tabSectionTitleMargin,
tabSectionOption,
tabSectionOptionItem,
tabSectionOptionToggleable,
tabSectionToggleableSubsection,
tabSectionOptionComplexInline,
tabSectionOptionText,
tabBar,
tabContent,
tabSectionOptionIcon,
} from './styles/VisualizationOptions.style.js'
const VisualizationOptions = ({
initiallyActiveTabKey,
optionsConfig,
onClose,
onUpdate,
}) => {
const [activeTabKey, setActiveTabKey] = useState(initiallyActiveTabKey)
const generateTabContent = (sections) =>
sections.map(({ key, label, content, helpText }) => (
<div key={key} className={tabSection.className}>
<FieldSet>
{label ? (
<Legend>
<span className={tabSectionTitle.className}>
{label}
</span>
</Legend>
) : null}
{content}
{helpText ? (
<Legend>
<Help>{helpText}</Help>
</Legend>
) : null}
</FieldSet>
</div>
))
const generateTabs = (tabs) =>
tabs.map(({ key, label, content }) => ({
key,
label,
content: generateTabContent(content),
}))
const getModalContent = () => {
const tabs = generateTabs(optionsConfig)
let activeTabIndex = tabs.findIndex((tab) => tab.key === activeTabKey)
if (activeTabIndex < 0) {
activeTabIndex = 0
}
return (
<>
<div className={tabBar.className}>
<TabBar dataTest={'options-modal-tab-bar'}>
{tabs.map(({ key, label }, index) => (
<Tab
key={key}
onClick={() => setActiveTabKey(key)}
selected={index === activeTabIndex}
>
{label}
</Tab>
))}
</TabBar>
{tabBar.styles}
</div>
<div className={tabContent.className}>
{tabs[activeTabIndex].content}
{tabContent.styles}
{tabSection.styles}
{tabSectionTitle.styles}
{tabSectionTitleDisabled.styles}
{tabSectionTitleMargin.styles}
{tabSectionOption.styles}
{tabSectionOptionItem.styles}
{tabSectionOptionToggleable.styles}
{tabSectionToggleableSubsection.styles}
{tabSectionOptionComplexInline.styles}
{tabSectionOptionText.styles}
{tabSectionOptionIcon.styles}
</div>
</>
)
}
return (
<Modal
onClose={onClose}
position="top"
large
dataTest={'options-modal'}
>
<ModalTitle>{i18n.t('Options')}</ModalTitle>
<ModalContent
className={modalContent.className}
dataTest={'options-modal-content'}
>
{getModalContent()}
</ModalContent>
<ModalActions dataTest={'options-modal-actions'}>
<ButtonStrip>
<Button
type="button"
secondary
onClick={onClose}
dataTest={'options-modal-action-cancel'}
>
{i18n.t('Hide')}
</Button>
<Button
onClick={onUpdate}
dataTest={'options-modal-action-confirm'}
type="button"
primary
>
{i18n.t('Update')}
</Button>
</ButtonStrip>
</ModalActions>
{modalContent.styles}
</Modal>
)
}
VisualizationOptions.propTypes = {
optionsConfig: PropTypes.array.isRequired,
initiallyActiveTabKey: PropTypes.string,
onClose: PropTypes.func,
onUpdate: PropTypes.func,
}
export default VisualizationOptions