-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1132 from nsbno/add-radio-card
Add RadioCard with RadioCardGroup to spor
- Loading branch information
Showing
9 changed files
with
270 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"@vygruppen/spor-react": minor | ||
--- | ||
|
||
New component: RadioCard & RadioCardGroup | ||
|
||
- Should be used togehter for optimal functionality |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { | ||
BoxProps, | ||
UseRadioProps, | ||
chakra, | ||
forwardRef, | ||
useRadio, | ||
useStyleConfig, | ||
} from "@chakra-ui/react"; | ||
import { dataAttr } from "@chakra-ui/utils"; | ||
import React, { useId } from "react"; | ||
|
||
type RadioCardProps = UseRadioProps & | ||
BoxProps & { | ||
children: React.ReactNode; | ||
variant: "floating" | "base"; | ||
}; | ||
|
||
/** | ||
* Renders a radio card. | ||
* | ||
* The most basic version looks like this: | ||
* | ||
* ```tsx | ||
* <RadioCard> | ||
* Content | ||
* </RadioCard> | ||
* ``` | ||
* | ||
* In order to use RadioCard, you typically want to place these components in a group with several other RadioCards. | ||
* | ||
* ```tsx | ||
* <RadioCardGroup name="ticket"> | ||
* <RadioCard value="economy">Economy</RadioCard> | ||
* <RadioCard value="business">Business</RadioCard> | ||
* <RadioCard value="first-class">First Class</RadioCard> | ||
* </RadioCardGroup> | ||
* ``` | ||
* | ||
* You can add styling to each card seperately, or you can add a common style to the group. | ||
* Group styling overrides single styling if both are present. | ||
* | ||
* This example shows how to style all cards in a group: | ||
* | ||
* ```tsx | ||
* <RadioCardGroup name="ticket" variant="floating" padding={3}> | ||
* <RadioCard value="economy">Economy</RadioCard> | ||
* <RadioCard value="business">Business</RadioCard> | ||
* <RadioCard value="first-class">First Class</RadioCard> | ||
* </RadioCardGroup> | ||
* ``` | ||
* | ||
* This example shows how to style a single card: | ||
* | ||
* ```tsx | ||
* <RadioCard variant="floating" padding={3}> | ||
* Economy | ||
* </RadioCard> | ||
* ``` | ||
*/ | ||
|
||
export const RadioCard = forwardRef<RadioCardProps, "div">( | ||
({ children, variant = "base", ...props }, ref) => { | ||
const { getInputProps, getRadioProps, getRootProps, state } = | ||
useRadio(props); | ||
|
||
const styles = useStyleConfig("RadioCard", { variant }); | ||
|
||
const input = getInputProps({}, ref); | ||
const radio = getRadioProps(); | ||
|
||
const id = `radio-card-${useId()}`; | ||
|
||
return ( | ||
<chakra.label | ||
htmlFor={id} | ||
{...getRootProps()} | ||
aria-label={String(children)} | ||
> | ||
<chakra.input {...input} id={id} disabled={state.isDisabled} /> | ||
<chakra.div | ||
__css={styles} | ||
{...radio} | ||
data-checked={dataAttr(state.isChecked)} | ||
data-hover={dataAttr(state.isHovered)} | ||
data-focus={dataAttr(state.isFocused)} | ||
data-active={dataAttr(state.isActive)} | ||
data-disabled={dataAttr(state.isDisabled)} | ||
{...props} | ||
> | ||
{children} | ||
</chakra.div> | ||
</chakra.label> | ||
); | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { | ||
useRadioGroup, | ||
RadioGroupProps, | ||
StackDirection, | ||
Stack, | ||
} from "@chakra-ui/react"; | ||
import React, { Children } from "react"; | ||
|
||
type RadioCardGroupProps = RadioGroupProps & { | ||
children: React.ReactNode; | ||
props?: RadioGroupProps; | ||
direction?: StackDirection; | ||
}; | ||
|
||
/** | ||
* Radio card groups are used to group several radio cards together. | ||
* | ||
* You can and should pass the common `name` prop to the `RadioGroup`, instead of to each `Radio` component. | ||
* | ||
* ```tsx | ||
* <RadioCardGroup name="ticket"> | ||
* <RadioCard>Economy</RadioCard> | ||
* <RadioCard>Business</RadioCard> | ||
* <RadioCard>First Class</RadioCard> | ||
* </RadioCardGroup> | ||
* ``` | ||
* | ||
* By default, radio cards show up horizontally. If you want them to show up vertically, please specify the `direction="column"` prop. | ||
* | ||
* ```tsx | ||
* <RadioCardGroup name="ticket" direction="column"> | ||
* <RadioCard>Economy</RadioCard> | ||
* <RadioCard>Business</RadioCard> | ||
* <RadioCard>First Class</RadioCard> | ||
* </RadioCardGroup> | ||
* ``` | ||
* | ||
* You can also specify the `defaultValue` prop to set the default value of the radio group. | ||
* | ||
* ```tsx | ||
* <RadioCardGroup name="ticket" defaultValue="Economy"> | ||
* <RadioCard>Economy</RadioCard> | ||
* <RadioCard>Business</RadioCard> | ||
* <RadioCard>First Class</RadioCard> | ||
* </RadioCardGroup> | ||
* ``` | ||
* | ||
* Check out RadioCard for more information on how to style the radio cards. | ||
* @see RadioCard | ||
*/ | ||
|
||
export const RadioCardGroup = ({ | ||
children, | ||
name, | ||
direction = "row", | ||
onChange, | ||
defaultValue, | ||
variant = "base", | ||
...props | ||
}: RadioCardGroupProps) => { | ||
const { getRootProps, getRadioProps } = useRadioGroup({ | ||
defaultValue: defaultValue, | ||
name: name, | ||
onChange: onChange, | ||
...props, | ||
}); | ||
|
||
const group = getRootProps(); | ||
|
||
return ( | ||
<Stack direction={direction} {...group}> | ||
{Children.map( | ||
children as React.ReactElement[], | ||
(child: React.ReactElement) => { | ||
const radio = getRadioProps({ value: child.props.value }); | ||
return React.cloneElement(child, { ...radio, variant, ...props }); | ||
}, | ||
)} | ||
</Stack> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { defineStyleConfig } from "@chakra-ui/react"; | ||
import { baseBackground, baseBorder, baseText } from "../utils/base-utils"; | ||
import { floatingBackground, floatingBorder } from "../utils/floating-utils"; | ||
import { focusVisibleStyles } from "../utils/focus-utils"; | ||
|
||
const config = defineStyleConfig({ | ||
baseStyle: (props: any) => ({ | ||
appearance: "none", | ||
border: "none", | ||
overflow: "hidden", | ||
fontSize: "inherit", | ||
display: "block", | ||
borderRadius: "sm", | ||
...focusVisibleStyles(props), | ||
_checked: { | ||
outline: "1px solid", | ||
outlineColor: "greenHaze", | ||
...floatingBackground("active", props), | ||
_hover: { | ||
...floatingBackground("active", props), | ||
}, | ||
}, | ||
_disabled: { | ||
pointerEvents: "none", | ||
...baseBackground("disabled", props), | ||
...baseBorder("disabled", props), | ||
...baseText("disabled", props), | ||
}, | ||
}), | ||
variants: { | ||
base: (props) => ({ | ||
...baseBackground("default", props), | ||
...baseBorder("default", props), | ||
_hover: { | ||
...baseBackground("hover", props), | ||
...baseBorder("hover", props), | ||
}, | ||
_active: { | ||
...baseBackground("active", props), | ||
}, | ||
}), | ||
floating: (props) => ({ | ||
...floatingBackground("default", props), | ||
boxShadow: "sm", | ||
_hover: { | ||
...floatingBackground("hover", props), | ||
...floatingBorder("hover", props), | ||
boxShadow: "md", | ||
}, | ||
_active: { | ||
...floatingBackground("active", props), | ||
...floatingBorder("active", props), | ||
}, | ||
_checked: { | ||
_hover: { | ||
outline: "1px solid", | ||
outlineColor: "silver", | ||
}, | ||
}, | ||
}), | ||
}, | ||
}); | ||
|
||
export default config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters