@cdc/markup-include 4.25.8 → 4.25.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,10 +1,8 @@
1
- import React, { useState, useEffect, memo, useContext, useRef, useMemo, useReducer } from 'react'
1
+ import React, { useState, useEffect, useContext, useRef } from 'react'
2
2
 
3
- // Third Party
4
- import _ from 'lodash'
3
+ import { cloneConfig } from '@cdc/core/helpers/cloneConfig'
5
4
 
6
5
  // Context
7
- import { Variable } from '../types/Variable'
8
6
  import ConfigContext from '../ConfigContext'
9
7
 
10
8
  // Helpers
@@ -13,19 +11,16 @@ import { updateFieldFactory } from '@cdc/core/helpers/updateFieldFactory'
13
11
  // Components
14
12
  import InputCheckbox from '@cdc/core/components/inputs/InputCheckbox'
15
13
  import ErrorBoundary from '@cdc/core/components/ErrorBoundary'
16
- import Icon from '@cdc/core/components/ui/Icon'
17
14
  import InputText from '@cdc/core/components/inputs/InputText'
18
15
  import Layout from '@cdc/core/components/Layout'
19
- import Tooltip from '@cdc/core/components/ui/Tooltip'
20
16
  import Accordion from '@cdc/core/components/ui/Accordion'
17
+ import MarkupVariablesEditor from '@cdc/core/components/EditorPanel/components/MarkupVariablesEditor'
18
+ import FootnotesEditor from '@cdc/core/components/EditorPanel/FootnotesEditor'
19
+ import { Datasets } from '@cdc/core/types/DataSet'
21
20
 
22
21
  // styles
23
22
  import '@cdc/core/styles/v2/components/editor.scss'
24
23
  import './editorPanel.style.css'
25
- import VariableSection from './Variables'
26
- import { CheckBox } from '@cdc/core/components/EditorPanel/Inputs'
27
- import FootnotesEditor from '@cdc/core/components/EditorPanel/FootnotesEditor'
28
- import { Datasets } from '@cdc/core/types/DataSet'
29
24
 
30
25
  const headerColors = [
31
26
  'theme-blue',
@@ -48,13 +43,11 @@ type MarkupIncludeEditorPanelProps = {
48
43
  const EditorPanel: React.FC<MarkupIncludeEditorPanelProps> = ({ datasets }) => {
49
44
  const { config, data, isDashboard, loading, setParentConfig, updateConfig } = useContext(ConfigContext)
50
45
  const { contentEditor, theme, visual } = config
51
- const { inlineHTML, markupVariables, srcUrl, title, useInlineHTML, allowHideSection, shoNoDataMessage } =
52
- contentEditor
46
+ const { inlineHTML, srcUrl, title, useInlineHTML } = contentEditor
53
47
  const [displayPanel, setDisplayPanel] = useState(true)
54
48
  const updateField = updateFieldFactory(config, updateConfig, true)
55
- const hasData = data?.[0] !== undefined ?? false
56
49
 
57
- const openVariableControls = useState<boolean[]>([])
50
+ const textAreaInEditorContainer = useRef(null)
58
51
 
59
52
  useEffect(() => {
60
53
  // Pass up to Editor if needed
@@ -80,52 +73,25 @@ const EditorPanel: React.FC<MarkupIncludeEditorPanelProps> = ({ datasets }) => {
80
73
  }
81
74
 
82
75
  const convertStateToConfig = () => {
83
- const strippedState = _.cloneDeep(config)
76
+ const strippedState = cloneConfig(config)
84
77
  delete strippedState.newViz
85
78
  delete strippedState.runtime
86
79
 
87
80
  return strippedState
88
81
  }
89
82
 
90
- const [variableArray, setVariableArray] = useState<Variable[]>([...markupVariables])
91
- const [isCreatingVariable, setIsCreatingVariable] = useState(false)
92
-
93
- const textAreaInEditorContainer = useRef(null)
94
- const [controls, setControls] = openVariableControls
95
-
96
- const handleCreateNewVariableButtonClick = () => {
97
- const newVariableArray = [..._.cloneDeep(variableArray)]
98
- const newVariable = {
99
- columnName: '',
100
- conditions: [],
101
- name: '',
102
- tag: ''
103
- }
104
-
105
- setControls({ ...controls, [variableArray.length + 1]: true })
106
-
107
- newVariableArray.push(newVariable)
108
- setVariableArray(newVariableArray)
109
- setIsCreatingVariable(!isCreatingVariable)
110
- }
111
-
112
- const updateVariableArray = (newVariable: Variable, variableIndex: number) => {
113
- const newVariableArray = _.cloneDeep(variableArray)
114
- newVariableArray[variableIndex] = newVariable
115
- setVariableArray(newVariableArray)
116
- updateField('contentEditor', null, 'markupVariables', newVariableArray)
117
- return
83
+ const handleMarkupVariablesChange = (variables: any[]) => {
84
+ updateConfig({
85
+ ...config,
86
+ markupVariables: variables
87
+ })
118
88
  }
119
89
 
120
- const deleteVariable = (variableIndex: number) => {
121
- const newVariableArray = _.cloneDeep(variableArray)
122
- newVariableArray.splice(variableIndex, 1)
123
- setVariableArray(newVariableArray)
124
- updateField('contentEditor', null, 'markupVariables', newVariableArray)
125
-
126
- const newControls = _.cloneDeep(controls)
127
- delete newControls[variableIndex]
128
- setControls(newControls)
90
+ const handleToggleEnable = (enabled: boolean) => {
91
+ updateConfig({
92
+ ...config,
93
+ enableMarkupVariables: enabled
94
+ })
129
95
  }
130
96
 
131
97
  const editorContent = (
@@ -165,85 +131,7 @@ const EditorPanel: React.FC<MarkupIncludeEditorPanelProps> = ({ datasets }) => {
165
131
  rows={10}
166
132
  updateField={updateField}
167
133
  />
168
-
169
- <hr className='accordion__divider' />
170
134
  </div>
171
- {/* Create New Variable*/}
172
-
173
- {/* Variable Options List */}
174
- <fieldset>
175
- <label>
176
- <span className='edit-label'>
177
- Variables
178
- <Tooltip style={{ textTransform: 'none' }}>
179
- <Tooltip.Target>
180
- <Icon display='question' style={{ marginLeft: '0.5rem' }} />
181
- </Tooltip.Target>
182
- <Tooltip.Content>{`To use created variables wrap the variable name in curly brackets, e.g. {{some_variable}}, and place the variable directly in your Inline HTML`}</Tooltip.Content>
183
- </Tooltip>
184
- </span>
185
- </label>
186
- {hasData === false && (
187
- <span className='need-data-source-prompt'>To use variables, add data source.</span>
188
- )}
189
- {variableArray && variableArray.length > 0 && (
190
- <div className='section-border'>
191
- {variableArray?.map((variableObject, index) => {
192
- return (
193
- <VariableSection
194
- key={`${variableObject.name}-${index}`}
195
- controls={openVariableControls}
196
- data={data}
197
- deleteVariable={deleteVariable}
198
- updateVariableArray={updateVariableArray}
199
- variableConfig={variableObject}
200
- variableIndex={index}
201
- />
202
- )
203
- })}
204
- </div>
205
- )}
206
- <div className='pt-2'>
207
- <CheckBox
208
- value={allowHideSection}
209
- section='contentEditor'
210
- fieldName='allowHideSection'
211
- label='Hide Section on Null'
212
- updateField={updateField}
213
- tooltip={
214
- <Tooltip style={{ textTransform: 'none' }}>
215
- <Tooltip.Target>
216
- <Icon
217
- display='question'
218
- style={{ marginLeft: '0.5rem', display: 'inline-block', whiteSpace: 'nowrap' }}
219
- />
220
- </Tooltip.Target>
221
- <Tooltip.Content>
222
- <p>{`Hide this entire Markup Include section if any variable is null or blank.`}</p>
223
- </Tooltip.Content>
224
- </Tooltip>
225
- }
226
- />
227
-
228
- <CheckBox
229
- value={contentEditor.showNoDataMessage}
230
- section='contentEditor'
231
- fieldName='showNoDataMessage'
232
- label='Add No Data Message'
233
- updateField={updateField}
234
- />
235
- </div>
236
-
237
- <div className='mb-1 d-flex'>
238
- <button
239
- className={'btn btn-primary'}
240
- onClick={handleCreateNewVariableButtonClick}
241
- disabled={!hasData}
242
- >
243
- Create New Variable
244
- </button>
245
- </div>
246
- </fieldset>
247
135
  </>
248
136
  ) : (
249
137
  <InputText
@@ -316,6 +204,15 @@ const EditorPanel: React.FC<MarkupIncludeEditorPanelProps> = ({ datasets }) => {
316
204
  <FootnotesEditor config={config} updateField={updateField} datasets={datasets} />
317
205
  </Accordion.Section>
318
206
  )}
207
+ <Accordion.Section title='Markup Variables'>
208
+ <MarkupVariablesEditor
209
+ markupVariables={config.markupVariables || []}
210
+ data={data || []}
211
+ onChange={handleMarkupVariablesChange}
212
+ enableMarkupVariables={config.enableMarkupVariables || false}
213
+ onToggleEnable={handleToggleEnable}
214
+ />
215
+ </Accordion.Section>
319
216
  </Accordion>
320
217
  )
321
218
 
@@ -1,7 +1,6 @@
1
1
  export default {
2
2
  contentEditor: {
3
3
  inlineHTML: '<h2>Inline HTML</h2>',
4
- markupVariables: [],
5
4
  showHeader: true,
6
5
  srcUrl: '#example',
7
6
  title: 'Markup Include',
@@ -21,5 +20,7 @@ export default {
21
20
  background: false,
22
21
  hideBackgroundColor: false,
23
22
  borderColorTheme: false
24
- }
23
+ },
24
+ markupVariables: [],
25
+ enableMarkupVariables: false
25
26
  }
package/src/index.jsx CHANGED
@@ -4,7 +4,6 @@ import ReactDOM from 'react-dom/client'
4
4
  import { GlobalContextProvider } from '@cdc/core/components/GlobalContext'
5
5
 
6
6
  import '@cdc/core/styles/cove-main.scss'
7
- import './coreStyles_markupinclude.scss'
8
7
 
9
8
  import CdcMarkupInclude from './CdcMarkupInclude'
10
9
 
@@ -1,6 +1,11 @@
1
- // Placeholder test until we add them in.
1
+ import path from 'path'
2
+ import { testStandaloneBuild } from '@cdc/core/helpers/tests/testStandaloneBuild.ts'
3
+ import { describe, it, expect } from 'vitest'
4
+
2
5
  describe('Markup Include', () => {
3
- it('has a test.', async () => {
4
- return true
6
+ it('Can be built in isolation', async () => {
7
+ const pkgDir = path.join(__dirname, '..')
8
+ const result = testStandaloneBuild(pkgDir)
9
+ expect(result).toBe(true)
5
10
  })
6
11
  })
@@ -1,5 +1,5 @@
1
1
  export type Condition = {
2
2
  columnName: string
3
- isOrIsNotEqualTo: string
3
+ isOrIsNotEqualTo: 'is' | 'is not'
4
4
  value: string
5
5
  }
@@ -6,7 +6,6 @@ export type Config = {
6
6
  contentEditor: {
7
7
  // Changing the base config object creates an infinite loop, nesting it is a workaround
8
8
  inlineHTML: string
9
- markupVariables: Variable[]
10
9
  showHeader: boolean
11
10
  srcUrl: string
12
11
  title: string
@@ -29,4 +28,6 @@ export type Config = {
29
28
  borderColorTheme: boolean
30
29
  }
31
30
  version: Version
31
+ markupVariables: Variable[]
32
+ enableMarkupVariables: boolean
32
33
  }
package/vite.config.js CHANGED
@@ -1,4 +1,4 @@
1
- import GenerateViteConfig from '../../generateViteConfig.js'
1
+ import GenerateViteConfig from '@cdc/core/generateViteConfig.js'
2
2
  import { moduleName } from './package.json'
3
3
 
4
4
  export default GenerateViteConfig(moduleName)
@@ -1 +0,0 @@
1
- @import '@cdc/core/styles/base';