@@ -2,6 +2,7 @@ const fs = require("fs");
2
2
const { htmlProps, svgProps, voids, types, typesByElement, reserved } = require ( "./consts" ) ;
3
3
const changeCase = require ( 'change-case' )
4
4
const htmlGenFile = "../src/React/Basic/DOM/Generated.purs" ;
5
+ const htmlSimplifiedGenFile = "../src/React/Basic/DOM/Simplified/Generated.purs" ;
5
6
const svgGenFile = "../src/React/Basic/DOM/SVG.purs" ;
6
7
7
8
const htmlHeader = `-- | ----------------------------------------
@@ -22,6 +23,26 @@ import Web.DOM (Node)
22
23
23
24
` ;
24
25
26
+ const simplifiedHtmlHeader = `-- | ----------------------------------------
27
+ -- | THIS FILE IS GENERATED -- DO NOT EDIT IT
28
+ -- | ----------------------------------------
29
+
30
+ module React.Basic.DOM.Simplified.Generated where
31
+
32
+ import Data.Nullable (Nullable)
33
+ import Effect.Unsafe (unsafePerformEffect)
34
+ import Foreign.Object (Object)
35
+ import Prim.Row (class Nub, class Union)
36
+ import React.Basic (JSX, ReactComponent, Ref, element)
37
+ import React.Basic.DOM.Internal (CSS, unsafeCreateDOMComponent)
38
+ import React.Basic.DOM.Simplified.ToJSX (class ToJSX, toJSX)
39
+ import React.Basic.Events (EventHandler)
40
+ import Record as Record
41
+ import Unsafe.Coerce (unsafeCoerce)
42
+ import Web.DOM (Node)
43
+
44
+ ` ;
45
+
25
46
const propType = ( e , p ) => {
26
47
const elPropTypes = typesByElement [ p ] ;
27
48
if ( elPropTypes ) {
@@ -127,15 +148,67 @@ const generatePropTypes = (elements, props, sharedPropType) =>
127
148
_${ e } '
128
149
:: ReactComponent (Record ${ propType } )
129
150
_${ e } ' = unsafePerformEffect (unsafeCreateDOMComponent "${ e } ")
151
+ ` ;
152
+ } ) . map ( x => x . replace ( / ^ \n \ { 4 } / , "" ) . replace ( / \n \ { 4 } / g, "\n" ) )
153
+ . join ( "\n" ) ;
154
+
155
+ const generateSimplifiedPropTypes = ( elements , props , sharedPropType ) =>
156
+ elements . map ( e => {
157
+ const noChildren = voids . includes ( e ) ;
158
+ const symbol = reserved . includes ( e ) ? `${ e } '` : e ;
159
+
160
+ const propType = sharedPropType ? `(${ sharedPropType } Props_${ e } )` : `Props_${ e } `
161
+
162
+ return noChildren ? `` : `
163
+ type Props_${ e } =${ printRecord ( e ,
164
+ ( reactProps . concat ( "children" )
165
+ )
166
+ . concat ( props [ e ] || [ ] , props [ "*" ] || [ ] )
167
+ . sort ( )
168
+ ) }
169
+
170
+ ${ symbol }
171
+ :: forall attrsNoChildren attrsWithDuplicate attrs attrs_ jsx
172
+ . Union attrs attrs_ ${ propType }
173
+ => ToJSX jsx
174
+ => Union (children :: Array JSX) attrsNoChildren attrsWithDuplicate
175
+ => Nub (children :: Array JSX | attrsNoChildren) attrs
176
+ => Record attrsNoChildren
177
+ -> jsx
178
+ -> JSX
179
+ ${ symbol } props children = element _internal${ symbol } propsWithChildren
180
+ where
181
+ propsWithChildren :: { | attrs }
182
+ propsWithChildren = Record.merge { children: toJSX children } props
183
+
184
+ ${ symbol } ' :: forall jsx. ToJSX jsx => jsx -> JSX
185
+ ${ symbol } ' = ${ symbol } {}
186
+
187
+ _internal${ symbol }
188
+ :: forall attrs attrs_
189
+ . Union attrs attrs_ ${ propType }
190
+ => ReactComponent (Record attrs)
191
+ _internal${ symbol } = unsafeCoerce _internal${ symbol } '
192
+
193
+ _internal${ symbol } '
194
+ :: ReactComponent (Record ${ propType } )
195
+ _internal${ symbol } ' = unsafePerformEffect (unsafeCreateDOMComponent "${ symbol } ")
196
+
130
197
` ;
131
198
} ) . map ( x => x . replace ( / ^ \n \ { 4 } / , "" ) . replace ( / \n \ { 4 } / g, "\n" ) )
132
199
. join ( "\n" ) ;
133
200
134
201
const htmlTagTypes = generatePropTypes ( htmlProps . elements . html , htmlProps , null ) ;
202
+ const htmlSimplifiedTagTypes = generateSimplifiedPropTypes ( htmlProps . elements . html , htmlProps , null ) ;
135
203
const svgTagTypes = generatePropTypes ( Object . keys ( camelCaseSvgProps ) , camelCaseSvgProps , 'SharedSVGProps' ) ;
136
204
137
205
console . log ( `Writing "${ htmlGenFile } " ...` ) ;
138
206
fs . writeFileSync ( htmlGenFile , htmlHeader + htmlTagTypes ) ;
207
+
208
+ console . log ( `Writing "${ htmlSimplifiedGenFile } " ...` ) ;
209
+ fs . writeFileSync ( htmlSimplifiedGenFile , simplifiedHtmlHeader + htmlSimplifiedTagTypes ) ;
210
+
139
211
console . log ( `Writing "${ svgGenFile } " ...` ) ;
140
212
fs . writeFileSync ( svgGenFile , svgHeader + svgTagTypes ) ;
213
+
141
214
console . log ( "Done." ) ;
0 commit comments