Skip to content

Commit

Permalink
Refactor fieldset
Browse files Browse the repository at this point in the history
  • Loading branch information
paradoxxxzero committed May 3, 2018
1 parent 56af31d commit 05bbf82
Show file tree
Hide file tree
Showing 4 changed files with 320 additions and 309 deletions.
41 changes: 16 additions & 25 deletions src/fields/CheckboxesField.jsx
Original file line number Diff line number Diff line change
@@ -1,35 +1,26 @@
import React from 'react'

import { block, cleanProps, normalizeChoices } from '../utils'
import { block } from '../utils'
import FieldSet from '../utils/FieldSet'

@block
export default class CheckboxesField extends React.Component {
static defaultProps = { value: [] }

render(b) {
const { value, onChange, ...props } = this.props
const values = value || []
const { onChange, ...props } = this.props
return (
<fieldset className={b}>
{normalizeChoices(props).map(([choice, choiceLabel]) => (
<label
key={choice}
className={b.e('label').m({ on: values.includes(choice) })}
>
<input
{...cleanProps(props)}
type="checkbox"
checked={values.includes(choice)}
onChange={e =>
onChange(
e.target.checked
? [...values, choice]
: values.filter(val => val !== choice)
)
}
/>
<span className={b.e('label-text')}>{choiceLabel}</span>
</label>
))}
</fieldset>
<FieldSet
className={b}
isChecked={(choice, value) => value.includes(choice)}
onChange={(choice, value, checked) =>
onChange(
checked ? [...value, choice] : value.filter(val => val !== choice)
)
}
{...props}
type="checkbox"
/>
)
}
}
28 changes: 10 additions & 18 deletions src/fields/RadiosField.jsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,20 @@
import React from 'react'

import { block, cleanProps, normalizeChoices } from '../utils'
import { block } from '../utils'
import FieldSet from '../utils/FieldSet'

@block
export default class RadiosField extends React.Component {
render(b) {
const { value, onChange, ...props } = this.props
const { onChange, ...props } = this.props
return (
<fieldset className={b}>
{normalizeChoices(props).map(([choice, choiceLabel]) => (
<label
key={choice}
className={b.e('label').m({ on: choice === value })}
>
<input
{...cleanProps(props)}
type="radio"
checked={choice === value}
onChange={e => e.target.checked && onChange(choice)}
/>
<span className={b.e('label-text')}>{choiceLabel}</span>
</label>
))}
</fieldset>
<FieldSet
className={b}
isChecked={(choice, value) => choice === value}
onChange={(choice, value, checked) => checked && onChange(choice)}
{...props}
type="radio"
/>
)
}
}
28 changes: 28 additions & 0 deletions src/utils/FieldSet.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import React from 'react'

import { block, cleanProps, normalizeChoices } from '../utils'

@block
export default class FieldSet extends React.Component {
render(b) {
const { type, isChecked, value, onChange, ...props } = this.props
return (
<fieldset className={b}>
{normalizeChoices(props).map(([choice, choiceLabel]) => (
<label
key={choice}
className={b.e('label').m({ on: choice === value })}
>
<input
{...cleanProps(props)}
type={type}
checked={isChecked(choice, value)}
onChange={e => onChange(choice, value, e.target.checked)}
/>
<span className={b.e('label-text')}>{choiceLabel}</span>
</label>
))}
</fieldset>
)
}
}

0 comments on commit 05bbf82

Please sign in to comment.