@@ -7,11 +7,14 @@ import ColorPicker from "./color-picker";
77import ColorScaleOptions from "./color-scale-options" ;
88import { getConfigValue } from "./utils" ;
99
10- const ConfigMapper = ( {
10+ const ControlComponent = ( {
11+ type,
12+ field,
1113 themeConfig,
12- activeColorScale,
1314 updateThemeConfig,
15+ activeColorScale,
1416 handleColorScaleChange,
17+ className,
1518} ) => {
1619 const handleColorChange = ( { newColor, index, colorScale } ) => {
1720 const updatedColors = themeConfig ?. palette ?. [ colorScale ] ?. map ( ( color , i ) =>
@@ -20,6 +23,91 @@ const ConfigMapper = ({
2023 updateThemeConfig ( `palette.${ colorScale } ` , updatedColors ) ;
2124 } ;
2225
26+ const handleChange = ( newValue ) => {
27+ updateThemeConfig ( field . path , newValue ) ;
28+ } ;
29+
30+ const configValue = getConfigValue ( themeConfig , field . path ) ;
31+ switch ( type ) {
32+ case "colorScale" :
33+ return (
34+ < ColorScaleOptions
35+ palette = { themeConfig ?. palette }
36+ activeColorScale = { activeColorScale }
37+ onColorChange = { handleColorChange }
38+ onColorScaleChange = { handleColorScaleChange }
39+ />
40+ ) ;
41+ case "section" :
42+ return (
43+ < section className = "mb-6" >
44+ < h3 className = "text-lg text-secondary font-bold mb-4" >
45+ { field . label }
46+ </ h3 >
47+ { field . fields . map ( ( subField , i ) => (
48+ < ControlComponent
49+ key = { subField . label + i }
50+ type = { subField . type }
51+ field = { subField }
52+ themeConfig = { themeConfig }
53+ updateThemeConfig = { updateThemeConfig }
54+ activeColorScale = { activeColorScale }
55+ handleColorScaleChange = { handleColorScaleChange }
56+ className = { className }
57+ />
58+ ) ) }
59+ </ section >
60+ ) ;
61+ case "slider" :
62+ return (
63+ < Slider
64+ id = { field . label }
65+ key = { field . label }
66+ label = { field . label }
67+ value = { configValue as number }
68+ unit = { field . unit }
69+ onChange = { handleChange }
70+ min = { field . min }
71+ max = { field . max }
72+ step = { field . step }
73+ className = { className }
74+ />
75+ ) ;
76+ case "select" :
77+ return (
78+ < Select
79+ id = { field . label }
80+ key = { field . label }
81+ label = { field . label }
82+ value = { configValue as string }
83+ onChange = { handleChange }
84+ options = { field . options }
85+ className = { className }
86+ />
87+ ) ;
88+ case "colorPicker" :
89+ return (
90+ < ColorPicker
91+ id = { field . label }
92+ key = { field . label }
93+ label = { field . label }
94+ color = { configValue as string }
95+ onColorChange = { handleChange }
96+ className = { className }
97+ showColorName
98+ />
99+ ) ;
100+ default :
101+ return null ;
102+ }
103+ } ;
104+
105+ const ConfigMapper = ( {
106+ themeConfig,
107+ activeColorScale,
108+ updateThemeConfig,
109+ handleColorScaleChange,
110+ } ) => {
23111 return (
24112 < >
25113 { optionsConfig . map ( ( section , index ) => (
@@ -29,62 +117,19 @@ const ConfigMapper = ({
29117 id = { section . title }
30118 defaultOpen = { index === 0 }
31119 >
32- { section . fields . map ( ( field ) => {
33- if ( field . type === "colorScale" ) {
34- return (
35- < ColorScaleOptions
36- key = { field . label }
37- activeColorScale = { activeColorScale }
38- palette = { themeConfig ?. palette }
39- onColorChange = { handleColorChange }
40- onColorScaleChange = { handleColorScaleChange }
41- />
42- ) ;
43- }
44- const configValue = getConfigValue ( themeConfig , field . path ) ;
45- if ( field . type === "slider" ) {
46- return (
47- < Slider
48- id = { field . label }
49- key = { field . label }
50- label = { field . label }
51- value = { configValue as number }
52- unit = { field . unit }
53- onChange = { ( newValue ) =>
54- updateThemeConfig ( field . path , newValue )
55- }
56- />
57- ) ;
58- }
59- if ( field . type === "select" ) {
60- return (
61- < Select
62- id = { field . label }
63- key = { field . label }
64- label = { field . label }
65- value = { configValue as string }
66- options = { field . options }
67- onChange = { ( newValue ) =>
68- updateThemeConfig ( field . path , newValue )
69- }
70- />
71- ) ;
72- }
73- if ( field . type === "colorPicker" ) {
74- return (
75- < ColorPicker
76- id = { field . label }
77- key = { field . label }
78- label = { field . label }
79- color = { configValue as string }
80- onColorChange = { ( newColor ) =>
81- updateThemeConfig ( field . path , newColor )
82- }
83- showColorName
84- />
85- ) ;
86- }
87- return null ;
120+ { section . fields . map ( ( field , i ) => {
121+ return (
122+ < ControlComponent
123+ key = { field . label + i }
124+ type = { field . type }
125+ field = { field }
126+ themeConfig = { themeConfig }
127+ updateThemeConfig = { updateThemeConfig }
128+ activeColorScale = { activeColorScale }
129+ handleColorScaleChange = { handleColorScaleChange }
130+ className = "mb-4"
131+ />
132+ ) ;
88133 } ) }
89134 </ Accordion >
90135 ) ) }
0 commit comments