Last active
April 2, 2018 22:31
-
-
Save cristianoliveira/0ca9d9b4038ef6fc3436fd583b337177 to your computer and use it in GitHub Desktop.
Same same but different - post
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// CountryAddressForm.js | |
return ( | |
<form | |
className="country-form" | |
action="#" | |
method="post" | |
onSubmit={this.handleSubmit.bind(this)} | |
onChange={this.handleChange.bind(this)}> | |
<CountrySelector countries={countries} /> | |
{children(this.state)} | |
<button>Submit</button> | |
</form> | |
); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// CountryFieldsFactory.js | |
// import addresses fields set... | |
const ADDRESS_FIELDS = { | |
DE: PostCodePriorAddress, | |
SE: PostCodePriorAddress, | |
SP: RegionAddress, | |
IT: RegionAddress, | |
IE: IrishAddressFields, | |
BR: BrazilianAddressFields, | |
}; | |
// USAGE: | |
// | |
// <CountryAddressForm> | |
// {state => <CountryFields {...state} />} | |
// </CountryAddressForm> | |
export function CountryFields({country, ...props}) { | |
const Fields = ADDRESS_FIELDS[country] || GeneraInternationalAddress; | |
const regions = formatRegions(COUNTRY_REGIONS[country]); | |
return <Fields {...{...props, regions, country}} />; | |
} | |
// It can be used as a simple function too :) | |
// | |
// USAGE: | |
// | |
// <CountryAddressForm> | |
// {fieldsFactory} | |
// </CountryAddressForm> | |
export default function fieldsFactory({country, ...props}) { | |
const fields = ADDRESS_FIELDS[country] || GeneraInternationalAddress; | |
const regions = formatRegions(COUNTRY_REGIONS[country]); | |
return React.createElement(fields, {...props, regions, country}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Usages | |
return ( | |
<div className="App"> | |
<CountryAddressForm> | |
{state => <CountryFields {...state} />} | |
</CountryAddressForm> | |
</div> | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment