Skip to content
This repository has been archived by the owner on Mar 7, 2023. It is now read-only.

Commit

Permalink
refactor(input): correct checkbox logic for use with redux-form
Browse files Browse the repository at this point in the history
  • Loading branch information
tbuchann committed Feb 6, 2019
1 parent 45fffe7 commit 886e3a8
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 28 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
"prop-types": "15.6.2",
"react": "16.7.0",
"react-dom": "16.7.0",
"redux-form": "^8.1.0",
"rimraf": "2.6.3",
"standard": "12.0.1",
"styled-components": "4.1.3",
Expand Down
21 changes: 9 additions & 12 deletions src/Atoms/Inputs/Checkbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,15 @@ const Checkbox = ({
forwardRef: ref,
input,
...rest
}: PropsType) => (
<Wrapper className={className}>
<Input
{...input}
{...rest}
id={toId(rest.name)}
ref={ref}
type="checkbox"
/>
<CheckboxLabel htmlFor={toId(rest.name)}>{rest.label}</CheckboxLabel>
</Wrapper>
)
}: PropsType) => {
const name = input ? input.name : rest.name
return (
<Wrapper className={className}>
<Input {...input} {...rest} id={toId(name)} ref={ref} type="checkbox" />
<CheckboxLabel htmlFor={toId(name)}>{rest.label}</CheckboxLabel>
</Wrapper>
)
}

type RefPropsType = { ...PropsType }

Expand Down
8 changes: 3 additions & 5 deletions src/Atoms/Inputs/Input.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
theme,
type ThemePropType,
} from '../../Tools/interpolation'
import type { InputProps } from 'redux-form/lib/FieldProps.types'
import Checkbox from './Checkbox'
import Radio from './Radio'

Expand All @@ -28,7 +29,7 @@ export type PropsType = {
+e2e?: string,
+error?: string,
+forwardRef?: React.ElementRef<*>,
+input?: {},
+input?: $Shape<InputProps>,
+label?: string,
+name?: string,
+renderSuffix?: (disabled?: boolean) => React.Node,
Expand All @@ -37,10 +38,7 @@ export type PropsType = {
+width?: string,
}

type InputPropsType = {
...$Exact<$Diff<PropsType, { input: * }>>,
...$Exact<$ElementType<PropsType, 'input'>>,
}
type InputPropsType = PropsType

type LabelPropsType = {
children?: React.Node,
Expand Down
25 changes: 14 additions & 11 deletions src/Atoms/Inputs/Radio.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import injectE2E from '../../Tools/injectE2E'
import { fontSize, theme } from '../../Tools/interpolation'
import type { PropsType } from './Input'

const toId = (name?: string | number) =>
`${name != null ? name : '' || ''}-checkbox`
const toId = (value?: string | number) =>
`${value != null ? value : '' || ''}-checkbox`

const Wrapper = styled.div`
display: flex;
Expand Down Expand Up @@ -70,15 +70,18 @@ export const RadioLabel = styled.label`
font-size: ${fontSize('md')};
`

const Radio = ({ className, forwardRef: ref, input, ...rest }: PropsType) => (
<Wrapper className={className}>
<Input {...input} {...rest} id={toId(rest.value)} ref={ref} type="radio" />
<RadioLabel htmlFor={toId(rest.value)}>
<RadioButton />
{rest.label}
</RadioLabel>
</Wrapper>
)
const Radio = ({ className, forwardRef: ref, input, ...rest }: PropsType) => {
const value = input ? input.value : rest.value
return (
<Wrapper className={className}>
<Input {...input} {...rest} id={toId(value)} ref={ref} type="radio" />
<RadioLabel htmlFor={toId(value)}>
<RadioButton />
{rest.label}
</RadioLabel>
</Wrapper>
)
}

type RefPropsType = { ...PropsType }

Expand Down

0 comments on commit 886e3a8

Please sign in to comment.