Skip to content

Commit

Permalink
feat(Radio): Add Radio Component DEV-136
Browse files Browse the repository at this point in the history
  • Loading branch information
itsweliton committed May 17, 2021
1 parent b41d5cf commit a7e7b89
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/components/Radio/Radio.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#### Default:

This is a simple use case for radio buttons
```js
<Radio id="sttuf" name="drax" value="etc" onChange={() => {}} label="Option X" />
<Radio id="stttf" name="drax" value="etc" onChange={() => {}} label="Option Y" />
```
21 changes: 21 additions & 0 deletions src/components/Radio/Radio.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import React from "react"
import Radio, { RadioProps } from "./Radio"
import { Meta, Story } from "@storybook/react/types-6-0"
import { action } from "@storybook/addon-actions"

export default {
title: "Design System/Radio",
component: Radio,
} as Meta

const Template: Story<RadioProps> = (props) => <Radio {...props} />

// Each story then reuses that template
export const Example = Template.bind({})

Example.args = {
label: "Radio Option One",
value: "opt1",
onChange: action("chosen"),
name: "Radio_opt1",
}
86 changes: 86 additions & 0 deletions src/components/Radio/Radio.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/** @jsxRuntime classic /
/** @jsx jsx */
import { jsx } from "theme-ui"
import { ChangeEvent, forwardRef } from "react"

export interface RadioProps {
/** The label content */
label?: string
/** The value of the `input` element, required for a controlled component */
value?: string
/** Callback fired when the value is changed */
onChange?: (event: ChangeEvent<HTMLInputElement>) => void
/** If `true`, the `input` is required */
required?: boolean
/** The id of the `input` element. Use this prop to make label and `helperText` accessible for screen readers */
id?: string

/** Radio identification */
name?: string
}

const Radio = forwardRef<HTMLInputElement, RadioProps>(function Radio(
{ id, value, label, name, onChange, required = false }: RadioProps,
ref
) {
return (
<label
sx={{
position: "relative",
display: "flex",
paddingLeft: "28px",
margin: 2,
overflow: "auto",
"&:hover input ~ span": {
backgroundColor: "secondary.90",
},
}}
>
{label}
<input
sx={{
position: "absolute",
opacity: 0,
cursor: "pointer",
height: 0,
width: 0,
display: "flex",
"&:checked + span": {
backgroundColor: "secondary.90",
"&:after": {
content: `""`,
display: "block",
width: "8px",
height: "8px",
borderRadius: "8px",
backgroundColor: "secondary.0",
},
},
}}
id={id}
type="radio"
ref={ref}
value={value}
name={name}
onChange={onChange}
required={required}
/>
<span
sx={{
position: "absolute",
top: 0,
left: 0,
height: "20px",
width: "20px",
borderRadius: "20px",
backgroundColor: "text.95",
display: "flex",
justifyContent: "center",
alignItems: "center",
}}
/>
</label>
)
})

export default Radio

0 comments on commit a7e7b89

Please sign in to comment.