Skip to content

Commit

Permalink
fieldAttributes and fieldsRestyle documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
mprzodala committed Mar 9, 2017
1 parent 0644b3b commit 3e1113c
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 23 deletions.
81 changes: 79 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<img src="https://img.shields.io/badge/build-passing-brightgreen.svg" />
<img src="https://img.shields.io/badge/license-MIT-blue.svg" />
<img src="https://img.shields.io/badge/npm-v1.1.2-blue.svg" />
<img src="https://img.shields.io/badge/npm-v1.2.0-blue.svg" />

1. [Installation](#installation)
2. [Description](#description)
Expand Down Expand Up @@ -74,6 +74,7 @@ Now go to http://localhost:8080 in your browser.
| model | Object |
| onSubmit | Function(model) |
| onError | Function(errors, model) |
| className | String |

###Fields

Expand Down Expand Up @@ -103,6 +104,7 @@ You can use current fields or create new fields. Here You have list of fields.
| placeholder | String |
| options | [String], [{label: String, value: String}] |
| errorStyles | {className, itemClassName, ErrorComponent} |
| fieldAttributes | Object with html attributes for input |

####How create new field
If You want create Your own custom field You must create component that use onChange method from props when value is changed and on export use FieldConnect method. FieldConnect will wrap Your field component and give props from form to Your field. Abow You have example of custom text field that have icon.
Expand All @@ -121,6 +123,7 @@ const CustomTextField = ({
label,
placeholder,
errorStyles = {}
fieldAttributes = {}
}) => (
<div className={wrapperClassName}>
{label && <label>{label}</label>}
Expand All @@ -131,6 +134,7 @@ const CustomTextField = ({
value={value}
placeholder={placeholder}
className={className}
{...fieldAttributes}
/>
<icon className="some-icon-class" />
{error && <ErrorField errors={errors} {...errorStyles} />}
Expand Down Expand Up @@ -321,6 +325,9 @@ export default CompanyForm;
```

####Example of use ObjectField

You can group fields as object by using ObjectField component.

```js
import React from 'react';
import { Form, TextField, ObjectField, SubmitField } from 'react-components-form';
Expand Down Expand Up @@ -366,6 +373,9 @@ export default CompanyForm;
```

####Example of use ListField

You can use ListField to create list of fields or groups of fields

```js
import React from 'react';
import { Form, TextField, ListField, SubmitField } from 'react-components-form';
Expand Down Expand Up @@ -402,6 +412,73 @@ const MemberForm = () => (
export default MemberForm;
```

####Example of use FieldsRestyle

You can restyle all fields by FieldRestyle method

```js
import { FieldsRestyle } from 'react-components-form';

const errorStyles = {
className: 'alert alert-danger'
};

const Fields = FieldsRestyle({
TextField: {
className: 'form-control',
errorStyles
},
DateField: {
className: 'form-control',
errorStyles
},
TextareaField: {
className: 'form-control',
errorStyles
},
NumberField: {
className: 'form-control',
errorStyles
},
SubmitField: {
className: 'btn btn-primary',
errorStyles
},
SelectField: {
className: 'form-control',
errorStyles
},
CheckboxField: {
wrapperClassName: 'checkbox',
errorStyles
},
ListField: {
className: 'form-control',
errorStyles,
addButton: {
className: 'btn btn-success btn-xs'
},
removeButton: {
className: 'btn btn-danger btn-xs pull-right'
}
}
});

export const Form = Fields.Form;
export const TextField = Fields.TextField;
export const DateField = Fields.DateField;
export const TextareaField = Fields.TextareaField;
export const NumberField = Fields.NumberField;
export const SubmitField = Fields.SubmitField;
export const SelectField = Fields.SelectField;
export const CheckboxField = Fields.CheckboxField;
export const ObjectField = Fields.ObjectField;
export const ListField = Fields.ListField;
export default Fields;
```



####Separate fields

You can use fields without Form context by import it from "react-components-form/Separate"
Expand All @@ -415,7 +492,7 @@ const exampleSearchComponent = ({onSearch}) => (
</div>
);

export default exampleComponent;
export default exampleSearchComponent;
```

####Bootstrap fields
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-components-form",
"version": "1.1.2",
"version": "1.2.0",
"description": "React form components",
"main": "main.js",
"jsnext:main": "src/index.js",
Expand Down
7 changes: 5 additions & 2 deletions src/components/CheckboxField.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ export class CheckboxField extends React.Component {
error,
label,
placeholder,
errorStyles = {}
errorStyles = {},
fieldAttributes = {}
} = this.props;
return (
<div className={wrapperClassName}>
Expand All @@ -42,6 +43,7 @@ export class CheckboxField extends React.Component {
onChange={this.toggleValue}
placeholder={placeholder}
className={className}
{...fieldAttributes}
/>
{label}
</label>
Expand All @@ -68,7 +70,8 @@ CheckboxField.propTypes = {
errorStyles: PropTypes.shape({
className: PropTypes.string,
itemClassName: PropTypes.string
})
}),
fieldAttributes: PropTypes.shape({})
};

export default FieldConnect(CheckboxField);
7 changes: 5 additions & 2 deletions src/components/DateField.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export const DateField = ({
value,
label,
placeholder,
errorStyles = {}
errorStyles = {},
fieldAttributes = {}
}) => (
<div className={wrapperClassName}>
{label && <label>{label}</label>}
Expand All @@ -34,6 +35,7 @@ export const DateField = ({
value={getDateString(value)}
placeholder={placeholder}
className={className}
{...fieldAttributes}
/>
{error && <ErrorField errors={errors} {...errorStyles} />}
</div>
Expand All @@ -55,7 +57,8 @@ DateField.propTypes = {
errorStyles: PropTypes.shape({
className: PropTypes.string,
itemClassName: PropTypes.string
})
}),
fieldAttributes: PropTypes.shape({})
};

export default FieldConnect(DateField);
8 changes: 5 additions & 3 deletions src/components/ListField.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,13 @@ export class ListField extends React.Component {
className,
wrapperClassName,
label,
addButton = {}
addButton = {},
fieldAttributes = {}
} = this.props;
return (
<div className={wrapperClassName}>
{label && <label>{label}</label>}
<div className={className}>{this.getList(children)}</div>
<div className={className} {...fieldAttributes}>{this.getList(children)}</div>
<span
onClick={this.addListElement}
className={addButton.className}
Expand Down Expand Up @@ -144,7 +145,8 @@ ListField.propTypes = {
}),
onChange: PropTypes.func.isRequired,
name: PropTypes.string,
value: PropTypes.any
value: PropTypes.any,
fieldAttributes: PropTypes.shape({})
};

export default FieldConnect(ListField);
7 changes: 5 additions & 2 deletions src/components/NumberField.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ export const NumberField = ({
value,
label,
placeholder,
errorStyles = {}
errorStyles = {},
fieldAttributes = {}
}) => (
<div className={wrapperClassName}>
{label && <label>{label}</label>}
Expand All @@ -23,6 +24,7 @@ export const NumberField = ({
value={value}
placeholder={placeholder}
className={className}
{...fieldAttributes}
/>
{error && <ErrorField errors={errors} {...errorStyles} />}
</div>
Expand All @@ -44,7 +46,8 @@ NumberField.propTypes = {
errorStyles: PropTypes.shape({
className: PropTypes.string,
itemClassName: PropTypes.string
})
}),
fieldAttributes: PropTypes.shape({})
};

export default FieldConnect(NumberField);
7 changes: 4 additions & 3 deletions src/components/ObjectField.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ export class ObjectField extends React.Component {
}

render() {
const { children, wrapperClassName } = this.props;
const { children, wrapperClassName, fieldAttributes } = this.props;
return (
<div className={wrapperClassName}>
<div className={wrapperClassName} {...fieldAttributes}>
{children}
</div>
);
Expand All @@ -86,7 +86,8 @@ ObjectField.propTypes = {
name: PropTypes.string,
onChange: PropTypes.func.isRequired,
children: PropTypes.node,
wrapperClassName: PropTypes.string
wrapperClassName: PropTypes.string,
fieldAttributes: PropTypes.shape({})
};

export default FieldConnect(ObjectField);
7 changes: 5 additions & 2 deletions src/components/SelectField.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export const SelectField = ({
value,
label,
placeholder,
errorStyles = {}
errorStyles = {},
fieldAttributes = {}
}) => (
<div className={wrapperClassName}>
{label && <label>{label}</label>}
Expand All @@ -23,6 +24,7 @@ export const SelectField = ({
value={value}
placeholder={placeholder}
className={className}
{...fieldAttributes}
>
{options.map(option => (
<option
Expand Down Expand Up @@ -61,7 +63,8 @@ SelectField.propTypes = {
errorStyles: PropTypes.shape({
className: PropTypes.string,
itemClassName: PropTypes.string
})
}),
fieldAttributes: PropTypes.shape({})
};

export default FieldConnect(SelectField);
7 changes: 5 additions & 2 deletions src/components/SubmitField.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ export const SubmitField = ({
wrapperClassName,
className,
submit,
value
value,
fieldAttributes = {}
}) => (
<div className={wrapperClassName}>
<button
onClick={submit}
className={className}
{...fieldAttributes}
>
{value}
</button>
Expand All @@ -21,7 +23,8 @@ SubmitField.propTypes = {
wrapperClassName: PropTypes.string,
className: PropTypes.string,
submit: PropTypes.func.isRequired,
value: PropTypes.string
value: PropTypes.string,
fieldAttributes: PropTypes.shape({})
};

export default FieldConnect(SubmitField);
7 changes: 5 additions & 2 deletions src/components/TextField.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export const TextField = ({
value,
label,
placeholder,
errorStyles = {}
errorStyles = {},
fieldAttributes = {}
}) => (
<div className={wrapperClassName}>
{label && <label>{label}</label>}
Expand All @@ -24,6 +25,7 @@ export const TextField = ({
value={value}
placeholder={placeholder}
className={className}
{...fieldAttributes}
/>
{error && <ErrorField errors={errors} {...errorStyles} />}
</div>
Expand All @@ -46,7 +48,8 @@ TextField.propTypes = {
errorStyles: PropTypes.shape({
className: PropTypes.string,
itemClassName: PropTypes.string
})
}),
fieldAttributes: PropTypes.shape({})
};

export default FieldConnect(TextField);
Loading

0 comments on commit 3e1113c

Please sign in to comment.