Skip to content

Commit

Permalink
structure of list
Browse files Browse the repository at this point in the history
  • Loading branch information
yeojz committed Mar 9, 2017
1 parent a1a6af9 commit 5a23e31
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 10 deletions.
21 changes: 11 additions & 10 deletions src/branch.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ const propTypes = {
onChange: PropTypes.func
};

const defaultProps = {
formData: {},
formMeta: {}
};

const cloneData = (name, data) => ({
...get(data, name, {})
});
Expand All @@ -40,13 +35,19 @@ const handleChange = (name, props) => (evt) => {
return props.onChange(event);
};

const branch = (defaultName = 'default') => (Component) => {
const branch = (config = {}) => (Component) => {
const name = get(config, 'defaultName', 'default');

const defaultProps = {
name,
formData: {},
formMeta: {}
};

class BranchedForm extends React.Component {
render() {
const name = this.props.name || defaultName;
const formData = cloneData(name, this.props.formData);
const formMeta = cloneData(name, this.props.formMeta);
const formData = cloneData(this.props.name, this.props.formData);
const formMeta = cloneData(this.props.name, this.props.formMeta);

return (
<Component
Expand All @@ -55,7 +56,7 @@ const branch = (defaultName = 'default') => (Component) => {
formMeta={formMeta}
getFormData={getDataFromKey(formData)}
getFormMeta={getDataFromKey(formMeta)}
onChange={handleChange(name, this.props)}
onChange={handleChange(this.props.name, this.props)}
/>
);
}
Expand Down
37 changes: 37 additions & 0 deletions src/list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import React, {PropTypes} from 'react';
import get from 'lodash/get';
import FormComponent from './utils/FormComponent';

const propTypes = {
name: PropTypes.string,
formData: PropTypes.object,
formMeta: PropTypes.object,
onChange: PropTypes.func
};

const list = (config = {}) => (Component) => {
const name = get(config, 'defaultName', 'default');
const Container = get(config, 'container', FormComponent);

const defaultProps = {
name,
formData: {},
formMeta: {}
};

class ListForm extends React.component {
render() {
return (
<Container>
<Component />
</Container>
);
}
}

ListForm.propTypes = propTypes;
ListForm.defaultProps = defaultProps;
return ListForm;
}

export default list;
14 changes: 14 additions & 0 deletions src/utils/FormComponent.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React, {PropTypes, Children} from 'react';

const propTypes = {
children: PropTypes.element
}

class FormComponent extends React.Component {
render() {
return Children.only(this.props.children);
}
}

FormComponent.propTypes = propTypes;
export default FormComponent;

0 comments on commit 5a23e31

Please sign in to comment.