Skip to content

Instantly share code, notes, and snippets.

@jonjaques
Created August 26, 2016 21:06
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 jonjaques/2878d539f3c8ea08bd268d37170cd1b6 to your computer and use it in GitHub Desktop.
Save jonjaques/2878d539f3c8ea08bd268d37170cd1b6 to your computer and use it in GitHub Desktop.
Render helpers for redux-form@6.0.0-rc4
import React from 'react'
import {
FormGroup,
InputGroup,
FormControl,
Col,
ControlLabel,
Checkbox,
Radio,
HelpBlock
} from 'react-bootstrap'
export function renderField(field) {
let state, compClass
let {
addonBefore,
addonAfter,
componentClass,
input,
label,
children,
rows,
placeholder
} = field
let {
dirty,
error,
invalid,
pristine,
touched
} = field.meta
if (!pristine && touched && invalid) {
state = 'error'
}
if (rows) {
input.rows = rows
}
if (placeholder) {
input.placeholder = placeholder
}
let formControl = <FormControl {...input}
componentClass={componentClass}
children={children} />
if (addonBefore || addonAfter) {
formControl = <InputGroup>
{addonBefore && <InputGroup.Addon>{addonBefore}</InputGroup.Addon>}
{formControl}
{addonAfter && <InputGroup.Addon>{addonAfter}</InputGroup.Addon>}
</InputGroup>
}
return <FormGroup controlId={input.name} validationState={state}>
<ControlLabel>{label}</ControlLabel>
{formControl}
<FormControl.Feedback />
{(!pristine && touched && error) && <HelpBlock>{error}</HelpBlock>}
</FormGroup>
}
export function renderInlineField(field) {
let state, compClass
let {
dirty,
invalid,
error,
pristine,
touched
} = field.meta
let {
input,
label,
children,
type,
componentClass,
props,
rows,
placeholder
} = field
if (!pristine && touched && invalid) {
state = 'error'
}
if (type) {
input.type = type
}
if (rows) {
input.rows = rows
}
if (placeholder) {
input.placeholder = placeholder
}
return <FormGroup controlId={input.name} validationState={state}>
<Col componentClass={ControlLabel} sm={3}>
{type !== 'checkbox' && label}
</Col>
<Col sm={6}>
{type === 'checkbox'
? <Checkbox {...input}>{label}</Checkbox>
: <FormControl {...input} componentClass={componentClass} children={children} /> }
<FormControl.Feedback />
{(!pristine && touched && error) && <HelpBlock>{error}</HelpBlock>}
</Col>
</FormGroup>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment