@@ -5,13 +5,6 @@ import { AppContext } from "./App";
55import globalStyles from "./App.css" ;
66import styles from "./Menu.css" ;
77
8- const testSettings = {
9- testNumber : 24 ,
10- testString : "Placeholder" ,
11- testBool : true ,
12- testColor : "#8000ff" ,
13- } ;
14-
158function MenuItem < T > ( {
169 name,
1710 initialValue,
@@ -97,42 +90,49 @@ function WidgetList() {
9790}
9891
9992function WidgetSettings ( ) {
100- const { widgets, setWidgets } = useContext ( AppContext ) ;
93+ const { editing , widgets, setWidgets, saveTemplate } = useContext ( AppContext ) ;
10194
10295 return (
10396 < >
104- { widgets . map ( ( state ) => {
105- return (
106- < >
107- < span >
108- { state . type . replace ( / ^ ./ , ( match ) => match . toUpperCase ( ) ) }
109- </ span >
110- { Object . entries ( state . settings ) . map ( ( [ key , value ] , i ) => {
111- return (
112- < MenuItem
113- key = { i }
114- name = { key
115- . replace ( / ( [ A - Z ] ) / g, ( match ) => ` ${ match } ` )
116- . replace ( / ( [ A - Z a - z ] ) (? = \d ) / g, "$1 " )
117- . replace ( / ^ ./ , ( match ) => match . toUpperCase ( ) )
118- . trim ( ) }
119- initialValue = { value }
120- onChange = { ( v ) => {
121- state . settings [ key ] = v ;
122- setWidgets ( [ ...widgets ] ) ;
123- } }
124- > </ MenuItem >
125- ) ;
126- } ) }
127- </ >
128- ) ;
129- } ) }
97+ { widgets . map ( ( state , i ) => (
98+ < React . Fragment key = { i } >
99+ < span >
100+ { state . type . replace ( / ^ ./ , ( match ) => match . toUpperCase ( ) ) }
101+ </ span >
102+ { Object . entries ( state . settings ) . map ( ( [ key , value ] , i ) => (
103+ < MenuItem
104+ key = { i }
105+ name = { key
106+ . replace ( / ( [ A - Z ] ) / g, ( match ) => ` ${ match } ` )
107+ . replace ( / ( [ A - Z a - z ] ) (? = \d ) / g, "$1 " )
108+ . replace ( / ^ ./ , ( match ) => match . toUpperCase ( ) )
109+ . trim ( ) }
110+ initialValue = { value }
111+ onChange = { ( v ) => {
112+ state . settings [ key ] = v ;
113+ setWidgets ( [ ...widgets ] ) ;
114+
115+ if ( ! editing ) saveTemplate ( ) ;
116+ } }
117+ />
118+ ) ) }
119+ </ React . Fragment >
120+ ) ) }
130121 </ >
131122 ) ;
132123}
133124
125+ enum MenuTab {
126+ Widget ,
127+ Add ,
128+ General ,
129+ Theme ,
130+ Template ,
131+ }
132+
134133export default function Menu ( { active } : { active : boolean } ) {
135134 const appContext = useContext ( AppContext ) ;
135+ const [ activeTab , setActiveTab ] = useState < MenuTab > ( MenuTab . Widget ) ;
136136
137137 return (
138138 < div
@@ -142,8 +142,29 @@ export default function Menu({ active }: { active: boolean }) {
142142 active ? styles . active : "" ,
143143 ] . join ( " " ) }
144144 >
145- { active && appContext . editing && < WidgetList > </ WidgetList > }
146- { ! appContext . editing && < WidgetSettings > </ WidgetSettings > }
145+ < div className = { [ globalStyles . container , styles . header ] . join ( " " ) } >
146+ { Object . entries ( MenuTab )
147+ . filter ( ( [ k , v ] ) => isNaN ( Number ( k ) ) )
148+ . map ( ( [ key , value ] , i ) => {
149+ return (
150+ < button
151+ key = { i }
152+ className = { [
153+ styles . headerTab ,
154+ activeTab === ( value as MenuTab ) ? styles . active : "" ,
155+ ] . join ( " " ) }
156+ onClick = { ( ) => setActiveTab ( value as MenuTab ) }
157+ >
158+ { key }
159+ </ button >
160+ ) ;
161+ } ) }
162+ </ div >
163+ { activeTab === MenuTab . Widget && < WidgetSettings > </ WidgetSettings > }
164+ { activeTab === MenuTab . Add && active && < WidgetList > </ WidgetList > }
165+ { ! [ MenuTab . Widget , MenuTab . Add ] . includes ( activeTab ) && (
166+ < span > No Settings Available</ span >
167+ ) }
147168 </ div >
148169 ) ;
149170}
0 commit comments