Styling
To style this form you have three different options, all requires to pass a React component to a form prop.
CSS-in-JS
In this section we will use a library that uses
styled-componentsto style our form
First you have to import the this library and the Form Components you want to use for your styling. In this example we will use styled-bootstrap-components.
import {Form,Field,Button,Select,RadioGroup,} from 'react-form-package';import {FormGroup,FormControl,FormCheck,FormCheckInput,Column,Button as Btn,} from 'styled-bootstrap-components';
The next step is to create you form and pass the styled components as props to the <Form /> component.
style prop
In this section we will use a the
styleprop to style our form
To style your form pass components to the <Form /> component that are styled with the style tag. When you create your own <input />, <button /> components do not forget do bind functions like onClick, onChange, onFocus, onBlur. This functions are needed to make react-form-package work properly.
const MyButton = (props) => (<buttontype="submit"style={{margin: '10px 0px',padding: '8px 24px',borderRadius: '5px',border: 'none',background: '#7FDBFF',color: '#001f3f',cursor: 'pointer',}}onClick={props.onClick}>{props.children}</button>);const MyInput = (props) => (<inputstyle={{margin: '10px 0px',padding: '8px 3px',border: '1px solid #7FDBFF',borderRadius: '5px',color: 'black',}}value={props.value}placeholder={props.placeholder}onChange={props.onChange}onBlur={props.onBlur}onFocus={props.onFocus}/>);
The next step is to create you form and pass the styled components as props to the <Form /> component.
CSS and className
In this section we will use a the
classNameprop and a css file to style our form
To style your form pass components to the <Form /> component that have className prop and use css files (make sure to have an appropriate loader for your bundler).
.button {background: #001f3f;border: 1px solid black;padding: 8px 24px;border-radius: 3px;color: white;cursor: pointer;}.input {margin: 10px 0px;padding: 8px 3px;border: 1px solid #001f3f;border-radius: 5px;}
Import your CSS file to your application and make sure you have a proper loader for your stylesheet.
import './style.css';const MyClassNameButton = (props) => (<buttonclssName="button">{children}</button>);const MyClassNameInput = (props) => (<inputclassName="input"value={props.value}placeholder={props.placeholder}onChange={props.onChange}onBlur={props.onBlur}onFocus={props.onFocus}/>);
The next step is to create you form and pass the styled components as props to the <Form /> component.
Passing props
To make your own components work with react-form-package you have to pass some props.
components
- The
Propis the prop on the<Form />component. HTMLis the actual HTML element to use.Propsare the props you have to pass these components e.g.id={prop.id} or id={this.prop.id}oronChange={prop.onChange} or onChange={this.prop.onChange}.Infoshows you which props you have to bind.
| Prop | HTML | Props | Info |
|---|---|---|---|
| input | input | id, type, onChange, onBlur, onFocus | |
| checkbox | input | id,type, onChange, onBlur, onFocus | |
| radio | div | id,type, onChange, onBlur, onFocus, children | this is a wrapper that renders every child, and watch out for <input type="radio" value="someValue" name="theIdRadioGroup" /> and handles their state |
| button | input | id, type, onClick, children | |
| select | select | id, type, onChange, onBlur, onFocus, children | this is a wrapper that renders every child, and watch out for <option value="someValue" /> and handles their state |
| textarea | textarea | id, type, onChange, onBlur, onFocus |
A textarea example:
const myTextarea = (props) => (<textareaid={props.id}type={props.type}onChange={props.onChange}onBlur={props.onBlur}onFocus={props.onFocus}/>);