File tree Expand file tree Collapse file tree 3 files changed +38
-1
lines changed Expand file tree Collapse file tree 3 files changed +38
-1
lines changed Original file line number Diff line number Diff line change @@ -114,6 +114,17 @@ const Component = props => pug` //- const Component = props => (
114114` ;
115115```
116116
117+ #### Transform block to attribute
118+
119+ ``` jsx
120+ const Component = props => pug` //- const Component = props => (
121+ Menu //- <Menu icon={<img />}>
122+ button //- <button />
123+ block icon //- </Menu>
124+ img //- )
125+ ` ;
126+ ```
127+
117128#### Interpolation
118129
119130If you'd prefer to use interpolation, you can. This is possible by using ` ${} ` within your template.
Original file line number Diff line number Diff line change 1+ // @flow
2+
3+ import type Context from '../context' ;
4+ import { visitJsxExpressions } from '../visitors' ;
5+ import { buildJSXFragment } from '../utils/jsx' ;
6+
7+ const NamedBlockVisitor = {
8+ jsx ( node : Object , context : Context ) : JSXValue {
9+ const nodes = visitJsxExpressions ( node . nodes , context ) ;
10+ node . nodes = nodes . length === 1 ? nodes : [ buildJSXFragment ( nodes ) ] ;
11+ return node ;
12+ } ,
13+ } ;
14+
15+ export default NamedBlockVisitor ;
Original file line number Diff line number Diff line change @@ -135,13 +135,24 @@ function getAttributesAndChildren(
135135 attrs : Array < JSXAttribute | JSXSpreadAttribute > ,
136136 children : Array < JSXValue > ,
137137} {
138- const children = getChildren ( node , context ) ;
138+ let children = getChildren ( node , context ) ;
139139
140140 if ( node . attributeBlocks . length ) {
141141 throw new Error ( 'Attribute blocks are not yet supported in react-pug' ) ;
142142 }
143143
144144 const attrs = getAttributes ( node , context ) ;
145+
146+ const blocks = children . filter ( ( { type} ) => type === 'NamedBlock' ) ;
147+ if ( blocks . length ) {
148+ children = children . filter ( ( { type} ) => type !== 'NamedBlock' ) ;
149+ attrs . push (
150+ ...blocks . map ( block =>
151+ t . jSXAttribute ( t . jSXIdentifier ( block . name ) , block . nodes [ 0 ] ) ,
152+ ) ,
153+ ) ;
154+ }
155+
145156 context . key . handleAttributes ( attrs ) ;
146157
147158 return { attrs, children} ;
You can’t perform that action at this time.
0 commit comments