Skip to content

Commit

Permalink
feat(Radio): add render prop feature
Browse files Browse the repository at this point in the history
  • Loading branch information
ej9x committed Sep 9, 2020
1 parent 39b6a63 commit 13025ce
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
26 changes: 19 additions & 7 deletions src/components/Radio/RadioItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ type RadioItemClonedProps = {
}

type RadioItemProps = {
children?: React$Node | ({ checked: boolean }) => React$Node,
/** text of the label */
label?: string,
/** radio value */
value: string | number,
value: string | number | boolean,
/** possible colors */
color?: 'primary' | 'secondary',
/** when true then disable radio */
Expand Down Expand Up @@ -53,6 +54,7 @@ class RadioItem extends PureComponent<RadioItemProps & RadioItemClonedProps> {
selectedValue,
equalsFunc,
onChange,
children,
...rest
} = this.props;
const hasLabel = !!label;
Expand All @@ -61,12 +63,22 @@ class RadioItem extends PureComponent<RadioItemProps & RadioItemClonedProps> {
return (
<RadioWrapperTag tagName="label" { ...rest }>
<RadioTag modifiers={ rest } name={ name } tagName="input" type="radio" onChange={ this.onChange } checked={ checked } />
<RadioCircleTag modifiers={ rest } tagName="div" >
<RadioCircleInnerTag modifiers={ rest } tagName="div" checked={ checked } />
</RadioCircleTag>
<If condition={ hasLabel }>
<RadioTextTag modifiers={ rest } tagName="div">{ label }</RadioTextTag>
</If>
<Choose>
<When condition={ typeof children === 'function' }>
{ children({ checked }) }
</When>
<When condition={ !!children }>
{ children }
</When>
<Otherwise>
<RadioCircleTag modifiers={ rest } tagName="div" >
<RadioCircleInnerTag modifiers={ rest } tagName="div" checked={ checked } />
</RadioCircleTag>
<If condition={ hasLabel }>
<RadioTextTag modifiers={ rest } tagName="div">{ label }</RadioTextTag>
</If>
</Otherwise>
</Choose>
</RadioWrapperTag>
);
}
Expand Down
14 changes: 13 additions & 1 deletion src/components/RadioGroupField/RadioGroupField.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import React from 'react';
import * as formUtils from '../../utils/forms';
import { Radio } from '../Radio';
import { FormField } from '../Form/FormField';
import type { InputType, MetaType, PropSizes } from '../../types';
import type { InputType, MetaType, PropLayout, PropLayoutStretch, PropSizes } from '../../types';

type RadioGroupFieldProps = {
/** Grop.Item components */
Expand All @@ -26,6 +26,10 @@ type RadioGroupFieldProps = {
disabled?: boolean,
/** stretch */
stretch?: boolean,
justifyContent?: PropLayoutStretch,
alignContent?: PropLayout,
alignItems?: PropLayoutStretch,
flexWrap?: 'wrap' | 'nowrap' | 'wrap-reverse',
};

const RadioGroupField = ({
Expand All @@ -38,6 +42,10 @@ const RadioGroupField = ({
options,
disabled,
stretch,
justifyContent,
alignContent,
alignItems,
flexWrap,
...rest
}: RadioGroupFieldProps) => {
const { name, value, onChange } = input;
Expand All @@ -56,6 +64,10 @@ const RadioGroupField = ({
value={ value }
disabled={ disabled }
stretch={ stretch }
justifyContent={ justifyContent }
alignContent={ alignContent }
alignItems={ alignItems }
flexWrap={ flexWrap }
>
{ children }
</Radio.Group>
Expand Down

0 comments on commit 13025ce

Please sign in to comment.