Skip to content

Commit

Permalink
fix(radio): support styling the container based on state (#4741)
Browse files Browse the repository at this point in the history
  • Loading branch information
takethefake committed Oct 4, 2021
1 parent 172847d commit 6c93542
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/dirty-emus-grab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@chakra-ui/radio": patch
---

Add Support for styling the container element based on the radio state
4 changes: 3 additions & 1 deletion packages/radio/src/radio.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export const Radio = forwardRef<RadioProps, "input">((props, ref) => {
getInputProps,
getCheckboxProps,
getLabelProps,
getRootProps,
htmlProps,
} = useRadio({
...rest,
Expand All @@ -91,6 +92,7 @@ export const Radio = forwardRef<RadioProps, "input">((props, ref) => {
const checkboxProps = getCheckboxProps(otherProps)
const inputProps = getInputProps({}, ref)
const labelProps = getLabelProps()
const rootProps = Object.assign(layoutProps, getRootProps())

const rootStyles = {
width: isFullWidth ? "full" : undefined,
Expand All @@ -115,7 +117,7 @@ export const Radio = forwardRef<RadioProps, "input">((props, ref) => {
}

return (
<chakra.label className="chakra-radio" {...layoutProps} __css={rootStyles}>
<chakra.label className="chakra-radio" {...rootProps} __css={rootStyles}>
<input className="chakra-radio__input" {...inputProps} />
<chakra.span
className="chakra-radio__control"
Expand Down
9 changes: 9 additions & 0 deletions packages/radio/src/use-radio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,14 @@ export function useRadio(props: UseRadioProps = {}) {
"data-invalid": dataAttr(isInvalid),
})

const getRootProps: PropGetter = (pros, ref = null) => ({
...props,
ref,
"data-disabled": dataAttr(isDisabled),
"data-checked": dataAttr(isChecked),
"data-invalid": dataAttr(isInvalid),
})

return {
state: {
isInvalid,
Expand All @@ -270,6 +278,7 @@ export function useRadio(props: UseRadioProps = {}) {
getCheckboxProps,
getInputProps,
getLabelProps,
getRootProps,
htmlProps,
}
}
Expand Down

0 comments on commit 6c93542

Please sign in to comment.