Skip to content
This repository was archived by the owner on Aug 27, 2018. It is now read-only.

Better core component props support #38

Merged
merged 1 commit into from
Apr 2, 2017
Merged

Better core component props support #38

merged 1 commit into from
Apr 2, 2017

Conversation

myitcv
Copy link
Owner

@myitcv myitcv commented Apr 1, 2017

Via an improved reactGen, we now support props type declaration "explosion"

Prior to this PR, the pattern for writing core React component props (or indeed any props type that embed another struct type) was:

// PPropsDef defines the properties for the <p> element
type PPropsDef struct {
	*BasicHTMLElement
}

// PProps creates a new instance of <p> properties, mutating the props
// by the provided initiapser
func PProps(f func(p *PPropsDef)) *PPropsDef {
	res := &PPropsDef{
		BasicHTMLElement: newBasicHTMLElement(),
	}
	f(res)
	return res
}

This pattern was necessary so that one could easily provide a field value without having to initialise deeply nested embedded struct values.

This callback pattern however made code much harder to write and read. Painful in fact.

Props type template explosion

reactGen now knows how to "explode" template struct type declarations for props types:

// _PProps are the props for a <div> component
type _PProps struct {
	*BasicHTMLElement
}

gets exploded into:

// PProps are the props for a <div> component
type PProps struct {
	ID                      string
	Key                     string
	ClassName               string
	Role                    string
	OnChange                func(e *SyntheticEvent)
	OnClick                 func(e *SyntheticMouseEvent)
	DangerouslySetInnerHTML *DangerousInnerHTMLDef
}

This allows a far more natural instantiation of components.

Compare how we used to have to do things

r.Div(
        r.DivProps(func(dp *r.DivPropsDef) {
                dp.ClassName = "row"
        }),
        r.Div(
                r.DivProps(func(dp *r.DivPropsDef) {
                        dp.ClassName = "col-md-8"
                }),
                r.Div(
                        r.DivProps(func(dp *r.DivPropsDef) {
                                dp.ClassName = "panel panel-default with-nav-tabs"
                        }),
                        r.Div(
                                r.DivProps(func(dp *r.DivPropsDef) {
                                        dp.ClassName = "panel-heading"
                                }),
                                r.Ul(
                                        r.UlProps(func(ulp *r.UlPropsDef) {
                                                ulp.ClassName = "nav nav-tabs"
                                        }),

                                        p.buildExampleNavTab(key, tabGo, "GopherJS"),
                                        p.buildExampleNavTab(key, tabJsx, "JSX"),
                                ),
                        ),
                        r.Div(
                                r.DivProps(func(dp *r.DivPropsDef) {
                                        dp.ClassName = "panel-body"
                                }),
                                r.Pre(nil, code),
                        ),
                ),
        ),
        r.Div(
                r.DivProps(func(dp *r.DivPropsDef) {
                        dp.ClassName = "col-md-4"
                }),
                plainPanel(elem),
        ),
)

with the new world post this PR:

r.Div(&r.DivProps{ClassName: "row"},
        r.Div(&r.DivProps{ClassName: "col-md-8"},
                r.Div(&r.DivProps{ClassName: "panel panel-default with-nav-tabs"},
                        r.Div(&r.DivProps{ClassName: "panel-heading"},
                                r.Ul(
                                        &r.UlProps{ClassName: "nav nav-tabs"},

                                        p.buildExampleNavTab(key, tabGo, "GopherJS"),
                                        p.buildExampleNavTab(key, tabJsx, "JSX"),
                                ),
                        ),
                        r.Div(&r.DivProps{ClassName: "panel-body"},
                                r.Pre(nil, code),
                        ),
                ),
        ),
        r.Div(&r.DivProps{ClassName: "col-md-4"},
                plainPanel(elem),
        ),
)

@myitcv myitcv force-pushed the props_gen branch 4 times, most recently from 5b3176c to 400715a Compare April 2, 2017 00:44
@myitcv myitcv changed the title [WIP] Props explosion Better core component props support Apr 2, 2017
@myitcv myitcv merged commit 74ef2df into master Apr 2, 2017
@myitcv myitcv deleted the props_gen branch April 2, 2017 01:00
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant