Skip to content

Instantly share code, notes, and snippets.

@cristianoliveira
Last active April 2, 2018 22:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cristianoliveira/0ca9d9b4038ef6fc3436fd583b337177 to your computer and use it in GitHub Desktop.
Save cristianoliveira/0ca9d9b4038ef6fc3436fd583b337177 to your computer and use it in GitHub Desktop.
Same same but different - post
// 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>
);
}
// 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});
}
// 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