Skip to content

Commit

Permalink
refactor(Radio Button Group): Using generator on unique id module
Browse files Browse the repository at this point in the history
  • Loading branch information
ddsilva committed Apr 5, 2019
1 parent c436c42 commit 1b90f8a
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 70 deletions.
4 changes: 3 additions & 1 deletion components/Input/Input.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import Icon from '../Icon';
import InputTypes from './InputTypes';
import uniqId from '../shared/uniqId';

const ID_GENERATOR = uniqId('input-');

const {
default: DEFAULT_INPUT_STYLE,
LABEL_STYLE,
Expand Down Expand Up @@ -118,7 +120,7 @@ class Input extends React.Component {

this.state = { type };

this._id = id || uniqId('input-');
this._id = id || ID_GENERATOR.next().value;
}

_changeType = type => {
Expand Down
94 changes: 53 additions & 41 deletions components/RadioGroup/RadioButton.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import HiddenRadio from '../shared/HiddenRadio';
import Icon from '../Icon';
import uniqId from '../shared/uniqId';

const ID_GENERATOR = uniqId('radio-button-');

const LockIcon = styled(Icon).attrs({
name: 'lock',
})``;
Expand Down Expand Up @@ -33,48 +35,58 @@ const Wrapper = styled.div`
}
`;

const Radio = ({
children,
label,
error,
disabled,
onChange,
value,
checked,
icon,
id,
inline,
...rest
}) => {
const skin = checked ? 'primary' : 'secondary';
const _id = id || uniqId('radio-button-');
class Radio extends React.Component {
constructor(props) {
super(props);

return (
<Wrapper inline={inline}>
<HiddenRadio
checked={checked}
disabled={disabled}
id={_id}
onChange={e => onChange({ value, label }, e)}
value={value}
skin={skin}
error={error}
{...rest}
/>
<ButtonGroupLabel
checked={checked}
disabled={disabled}
error={error}
htmlFor={_id}
skin={skin}
>
{icon && <ButtonIcon name={icon} />}
{children || label}
{disabled && <LockIcon />}
</ButtonGroupLabel>
</Wrapper>
);
};
const { id } = props;

this._id = id || ID_GENERATOR.next().value;
}

render() {
const {
children,
label,
error,
disabled,
onChange,
value,
checked,
icon,
id,
inline,
...rest
} = this.props;
const skin = checked ? 'primary' : 'secondary';

return (
<Wrapper inline={inline}>
<HiddenRadio
checked={checked}
disabled={disabled}
id={this._id}
onChange={e => onChange({ value, label }, e)}
value={value}
skin={skin}
error={error}
{...rest}
/>
<ButtonGroupLabel
checked={checked}
disabled={disabled}
error={error}
htmlFor={this._id}
skin={skin}
>
{icon && <ButtonIcon name={icon} />}
{children || label}
{disabled && <LockIcon />}
</ButtonGroupLabel>
</Wrapper>
);
}
}

Radio.displayName = 'RadioGroup.Button';

Expand Down
59 changes: 38 additions & 21 deletions components/TextArea/TextArea.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import styled from 'styled-components';
import { INPUT_STYLE, Label, FieldGroup, ErrorMessage } from '../shared';
import uniqId from '../shared/uniqId';

const ID_GENERATOR = uniqId('textarea-');

const {
default: DEFAULT_INPUT_STYLE,
LABEL_STYLE,
Expand Down Expand Up @@ -43,27 +45,42 @@ const TextAreaErrorMessage = styled(ErrorMessage)`
`}
`;

const TextArea = ({ label, required, helperText, error, id, ...rest }) => {
const _id = id || uniqId('textarea-');

return (
<FieldGroup>
{label && (
<TextAreaLabel htmlFor={_id}>
{label}
{required && <RequiredMark>*</RequiredMark>}
</TextAreaLabel>
)}
<TextAreaTag error={error} id={_id} required={required} {...rest} />
{helperText && <HelperText>{helperText}</HelperText>}
{error && (
<TextAreaErrorMessage helperText={helperText}>
{error}
</TextAreaErrorMessage>
)}
</FieldGroup>
);
};
class TextArea extends React.Component {
constructor(props) {
super(props);

const { id } = props;

this._id = id || ID_GENERATOR.next().value;
}

render() {
const { label, required, helperText, error, id, ...rest } = this.props;

return (
<FieldGroup>
{label && (
<TextAreaLabel htmlFor={this._id}>
{label}
{required && <RequiredMark>*</RequiredMark>}
</TextAreaLabel>
)}
<TextAreaTag
error={error}
id={this._id}
required={required}
{...rest}
/>
{helperText && <HelperText>{helperText}</HelperText>}
{error && (
<TextAreaErrorMessage helperText={helperText}>
{error}
</TextAreaErrorMessage>
)}
</FieldGroup>
);
}
}

TextArea.defaultProps = {
disabled: false,
Expand Down
12 changes: 5 additions & 7 deletions components/shared/uniqId.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
let _counter = 0;
import 'babel-polyfill';

export default (prefix = '') => {
const _id = `${prefix}${_counter}`;
_counter += 1;

return _id;
};
export default function*(prefix = '') {
let _counter = -1;
while (true) yield `${prefix}${(_counter += 1)}`;
}

0 comments on commit 1b90f8a

Please sign in to comment.