@cdc/data-bite 4.25.8 → 4.25.11
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.
- package/dist/cdcdatabite.js +9723 -8357
- package/package.json +14 -5
- package/src/CdcDataBite.tsx +55 -16
- package/src/_stories/DataBite.Editor.stories.tsx +862 -0
- package/src/_stories/DataBite.stories.tsx +59 -35
- package/src/components/EditorPanel.jsx +53 -47
- package/src/constants.ts +0 -52
- package/src/data/initial-state.js +3 -1
- package/src/index.jsx +1 -1
- package/src/scss/editor-panel.scss +12 -484
- package/src/scss/main.scss +0 -33
- package/src/store/db.reducer.ts +1 -1
- package/src/test/CdcDataBite.test.jsx +8 -3
- package/src/types/Config.ts +4 -2
- package/tests/fixtures/example-data.json +833 -0
- package/tests/fixtures/test-config.json +26 -0
- package/vite.config.js +1 -1
- package/vitest.config.ts +16 -0
- package/src/coreStyles_databite.scss +0 -3
- package/src/scss/variables.scss +0 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cdc/data-bite",
|
|
3
|
-
"version": "4.25.
|
|
3
|
+
"version": "4.25.11",
|
|
4
4
|
"description": "React component for displaying a single piece of data in a card module",
|
|
5
5
|
"moduleName": "CdcDataBite",
|
|
6
6
|
"main": "dist/cdcdatabite",
|
|
@@ -27,13 +27,22 @@
|
|
|
27
27
|
"license": "Apache-2.0",
|
|
28
28
|
"homepage": "https://github.com/CDCgov/cdc-open-viz#readme",
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@cdc/core": "^4.25.
|
|
31
|
-
"chroma": "
|
|
32
|
-
"react-
|
|
30
|
+
"@cdc/core": "^4.25.11",
|
|
31
|
+
"chroma-js": "3.1.2",
|
|
32
|
+
"html-react-parser": "5.2.3",
|
|
33
|
+
"lodash": "^4.17.21",
|
|
34
|
+
"react-accessible-accordion": "^5.0.1",
|
|
35
|
+
"resize-observer-polyfill": "^1.5.1"
|
|
33
36
|
},
|
|
34
37
|
"peerDependencies": {
|
|
35
38
|
"react": "^18.2.0",
|
|
36
39
|
"react-dom": "^18.2.0"
|
|
37
40
|
},
|
|
38
|
-
"
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@vitejs/plugin-react": "^4.3.4",
|
|
43
|
+
"vite": "^4.4.11",
|
|
44
|
+
"vite-plugin-css-injected-by-js": "^2.4.0",
|
|
45
|
+
"vite-plugin-svgr": "^2.4.0"
|
|
46
|
+
},
|
|
47
|
+
"gitHead": "5f09a137c22f454111ab5f4cd7fdf1d2d58e31bd"
|
|
39
48
|
}
|
package/src/CdcDataBite.tsx
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { useEffect, useCallback, useReducer } from 'react'
|
|
2
2
|
import { Fragment } from 'react'
|
|
3
3
|
|
|
4
4
|
// contexts & initial state
|
|
@@ -17,6 +17,9 @@ import Layout from '@cdc/core/components/Layout'
|
|
|
17
17
|
import ResizeObserver from 'resize-observer-polyfill'
|
|
18
18
|
import parse from 'html-react-parser'
|
|
19
19
|
|
|
20
|
+
// markup processing
|
|
21
|
+
import { processMarkupVariables } from '@cdc/core/helpers/markupProcessor'
|
|
22
|
+
|
|
20
23
|
// helpers
|
|
21
24
|
import getViewport from '@cdc/core/helpers/getViewport'
|
|
22
25
|
import { DataTransform } from '@cdc/core/helpers/DataTransform'
|
|
@@ -28,24 +31,21 @@ import cacheBustingString from '@cdc/core/helpers/cacheBustingString'
|
|
|
28
31
|
import coveUpdateWorker from '@cdc/core/helpers/coveUpdateWorker'
|
|
29
32
|
import { Config } from './types/Config'
|
|
30
33
|
import dataBiteReducer from './store/db.reducer'
|
|
34
|
+
import { IMAGE_POSITION_LEFT, IMAGE_POSITION_RIGHT, IMAGE_POSITION_TOP, IMAGE_POSITION_BOTTOM } from './constants'
|
|
35
|
+
|
|
31
36
|
import {
|
|
32
37
|
DATA_FUNCTION_COUNT,
|
|
33
38
|
DATA_FUNCTION_MAX,
|
|
34
39
|
DATA_FUNCTION_MEAN,
|
|
35
40
|
DATA_FUNCTION_MEDIAN,
|
|
36
|
-
DATA_FUNCTION_MIN,
|
|
37
41
|
DATA_FUNCTION_MODE,
|
|
42
|
+
DATA_FUNCTION_MIN,
|
|
38
43
|
DATA_FUNCTION_RANGE,
|
|
39
|
-
DATA_FUNCTION_SUM
|
|
40
|
-
|
|
41
|
-
IMAGE_POSITION_RIGHT,
|
|
42
|
-
IMAGE_POSITION_TOP,
|
|
43
|
-
IMAGE_POSITION_BOTTOM
|
|
44
|
-
} from './constants'
|
|
44
|
+
DATA_FUNCTION_SUM
|
|
45
|
+
} from '@cdc/core/helpers/constants'
|
|
45
46
|
|
|
46
47
|
// styles
|
|
47
48
|
import './scss/main.scss'
|
|
48
|
-
import { publishAnalyticsEvent } from '@cdc/core/helpers/metrics/helpers'
|
|
49
49
|
|
|
50
50
|
type CdcDataBiteProps = {
|
|
51
51
|
config: Config
|
|
@@ -90,7 +90,9 @@ const CdcDataBite = (props: CdcDataBiteProps) => {
|
|
|
90
90
|
dataFormat,
|
|
91
91
|
biteStyle,
|
|
92
92
|
filters,
|
|
93
|
-
subtext
|
|
93
|
+
subtext,
|
|
94
|
+
markupVariables,
|
|
95
|
+
enableMarkupVariables
|
|
94
96
|
} = config
|
|
95
97
|
|
|
96
98
|
const { innerContainerClasses, contentClasses } = useDataVizClasses(config)
|
|
@@ -124,7 +126,25 @@ const CdcDataBite = (props: CdcDataBiteProps) => {
|
|
|
124
126
|
|
|
125
127
|
//@ts-ignore
|
|
126
128
|
const loadConfig = async () => {
|
|
127
|
-
let response = configObj
|
|
129
|
+
let response = configObj
|
|
130
|
+
|
|
131
|
+
if (!response && configUrl) {
|
|
132
|
+
try {
|
|
133
|
+
const fetchResponse = await fetch(configUrl)
|
|
134
|
+
if (fetchResponse.ok) {
|
|
135
|
+
const text = await fetchResponse.text()
|
|
136
|
+
response = text ? JSON.parse(text) : {}
|
|
137
|
+
} else {
|
|
138
|
+
console.warn(`Failed to fetch config from ${configUrl}: ${fetchResponse.status}`)
|
|
139
|
+
response = {}
|
|
140
|
+
}
|
|
141
|
+
} catch (error) {
|
|
142
|
+
console.warn(`Error loading config from ${configUrl}:`, error)
|
|
143
|
+
response = {}
|
|
144
|
+
}
|
|
145
|
+
} else if (!response) {
|
|
146
|
+
response = {}
|
|
147
|
+
}
|
|
128
148
|
|
|
129
149
|
// If data is included through a URL, fetch that and store
|
|
130
150
|
let responseData = response.data ?? []
|
|
@@ -148,10 +168,25 @@ const CdcDataBite = (props: CdcDataBiteProps) => {
|
|
|
148
168
|
const processedConfig = { ...coveUpdateWorker(response) }
|
|
149
169
|
|
|
150
170
|
updateConfig({ ...defaults, ...processedConfig })
|
|
151
|
-
publishAnalyticsEvent('data-bite_loaded', 'load', interactionLabel, 'data-bite')
|
|
152
171
|
dispatch({ type: 'SET_LOADING', payload: false })
|
|
153
172
|
}
|
|
154
173
|
|
|
174
|
+
// Process markup variables in content
|
|
175
|
+
const processContentWithMarkup = (content: string) => {
|
|
176
|
+
if (!enableMarkupVariables || !markupVariables || markupVariables.length === 0) {
|
|
177
|
+
return content
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const result = processMarkupVariables(content, config.data, markupVariables, {
|
|
181
|
+
isEditor,
|
|
182
|
+
showNoDataMessage: false,
|
|
183
|
+
allowHideSection: false,
|
|
184
|
+
filters: config.filters || []
|
|
185
|
+
})
|
|
186
|
+
|
|
187
|
+
return result.processedContent
|
|
188
|
+
}
|
|
189
|
+
|
|
155
190
|
const calculateDataBite = (includePrefixSuffix = true) => {
|
|
156
191
|
//If either the column or function aren't set, do not calculate
|
|
157
192
|
if (!dataColumn || !dataFunction) {
|
|
@@ -566,7 +601,7 @@ const CdcDataBite = (props: CdcDataBiteProps) => {
|
|
|
566
601
|
<Title
|
|
567
602
|
showTitle={config.visual?.showTitle}
|
|
568
603
|
config={config}
|
|
569
|
-
title={title}
|
|
604
|
+
title={processContentWithMarkup(title)}
|
|
570
605
|
isDashboard={isDashboard}
|
|
571
606
|
classes={['bite-header', `${config.theme}`]}
|
|
572
607
|
/>
|
|
@@ -600,14 +635,16 @@ const CdcDataBite = (props: CdcDataBiteProps) => {
|
|
|
600
635
|
{calculateDataBite()}
|
|
601
636
|
</span>
|
|
602
637
|
)}
|
|
603
|
-
{parse(biteBody)}
|
|
638
|
+
{parse(processContentWithMarkup(biteBody))}
|
|
604
639
|
</p>
|
|
605
640
|
{showBite && 'end' === biteStyle && (
|
|
606
641
|
<span className='bite-value data-bite-body' style={{ fontSize: biteFontSize + 'px' }}>
|
|
607
642
|
{calculateDataBite()}
|
|
608
643
|
</span>
|
|
609
644
|
)}
|
|
610
|
-
{subtext && !config.general.isCompactStyle &&
|
|
645
|
+
{subtext && !config.general.isCompactStyle && (
|
|
646
|
+
<p className='bite-subtext'>{parse(processContentWithMarkup(subtext))}</p>
|
|
647
|
+
)}
|
|
611
648
|
</div>
|
|
612
649
|
</Fragment>
|
|
613
650
|
</div>
|
|
@@ -630,7 +667,9 @@ const CdcDataBite = (props: CdcDataBiteProps) => {
|
|
|
630
667
|
}
|
|
631
668
|
|
|
632
669
|
return (
|
|
633
|
-
<Context.Provider
|
|
670
|
+
<Context.Provider
|
|
671
|
+
value={{ config, updateConfig, loading, data: config.data, setParentConfig, isDashboard, isEditor }}
|
|
672
|
+
>
|
|
634
673
|
{biteStyle !== 'gradient' && (
|
|
635
674
|
<Layout.VisualizationWrapper
|
|
636
675
|
ref={outerContainerRef}
|