Skip to content

Instantly share code, notes, and snippets.

@kaneel
Last active January 11, 2019 10:19
Show Gist options
  • Save kaneel/6f811c9568c3d54f716d01c48d7a12d2 to your computer and use it in GitHub Desktop.
Save kaneel/6f811c9568c3d54f716d01c48d7a12d2 to your computer and use it in GitHub Desktop.
Simple Radio Component that can take any children and wrap it into a Radio choice.
import * as React from 'react';
import { v4 as uuid } from 'uuid';
import { styled } from '../styled';
export interface IProps {
name: string;
}
const StyledRadio = styled.div`
`;
const StyledChoice = styled.p`
`
export default class Radio extends React.Component<IProps, {}> {
public render() {
const { children, name } = this.props;
return (
<StyledRadio>
{
React.Children.toArray(children).map(
(child: any, key) => {
const id = uuid();
return (
<StyledChoice key={key}>
<input type="radio" name={name} id={id} value={key} />
<label htmlFor={id}>{child}</label>
</StyledChoice>
);
}
)
}
</StyledRadio>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment