Skip to content

Commit

Permalink
Code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
damiano-carradori committed Jan 21, 2019
1 parent d767ffc commit d6be1a3
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 20 deletions.
8 changes: 4 additions & 4 deletions src/Form/Form.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import React, {PureComponent} from 'react'
import {FormContext, FormContextProvider} from './FormContext'
import React, { PureComponent } from 'react'
import { FormContext, FormContextProvider } from './FormContext'
import Text from '../Text'

class Form extends PureComponent {
render() {
const {className, children, onSubmit} = this.props;
const { className, children, onSubmit } = this.props;
return (
<FormContextProvider onSubmit={onSubmit}>
<FormContext.Consumer>
{({onSubmit: contextSubmit}) => (
{({ onSubmit: contextSubmit }) => (
<form onSubmit={contextSubmit} className={className}>
{children}
</form>
Expand Down
24 changes: 12 additions & 12 deletions src/Form/FormContext.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {createContext, Component} from 'react'
import React, { Component, createContext } from 'react'

export const FormContext = createContext(null);

Expand All @@ -15,45 +15,45 @@ export class FormContextProvider extends Component {
onChange: this.onChange,
onMount: this.onMount,
fields: {},
values: {}
values: {},
}
}

shouldComponentUpdate(nextProps) {
const {onSubmit} = this.props;
const { onSubmit } = this.props;
return onSubmit !== nextProps.onSubmit;
}

onSubmit(event) {
event.preventDefault();
const {onSubmit} = this.props;
const { onSubmit } = this.props;
onSubmit(this.state.values);
}

onChange({target}) {
const {name, value} = target;
onChange({ target }) {
const { name, value } = target;

this.setState(({values}) => ({
this.setState(({ values }) => ({
values: {
...values,
[name]: value || undefined,
},
}));
}

onMount(field){
const {name} = field;
onMount(field) {
const { name } = field;

this.setState(({fields}) => ({
this.setState(({ fields }) => ({
fields: {
...fields,
[name]: field,
}
},
}));
}

render() {
const {children} = this.props;
const { children } = this.props;
return (
<FormContext.Provider value={this.state}>
{children}
Expand Down
4 changes: 2 additions & 2 deletions src/Form/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export {default} from './Form'
export {FormContext} from './FormContext';
export { default } from './Form'
export { FormContext } from './FormContext';
2 changes: 1 addition & 1 deletion src/Text/index.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export {default} from './Text'
export { default } from './Text'
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export {default} from './Form'
export { default } from './Form'

0 comments on commit d6be1a3

Please sign in to comment.