Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(radio): radio with tailwind forms #46

Merged
merged 16 commits into from
Feb 1, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat(radio): added size prop
  • Loading branch information
anuraghazra committed Jan 27, 2021
commit fefa60546ff11a4caa9a3821bf916fd528ab46d8
29 changes: 20 additions & 9 deletions src/radio/Radio.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import React from "react";
import { cx } from "@renderlesskit/react";
import {
RadioProps,
RadioGroupProps,
Radio as ReakitRadio,
RadioProps as ReakitRadioProps,
RadioGroup as ReakitRadioGroup,
RadioGroupProps as ReakitRadioGroupProps,
useRadio,
} from "reakit";
import { BoxProps, Box } from "../box";
import { useTheme } from "../theme";
Expand All @@ -18,19 +19,26 @@ const [RadioProvider, useRadioContext] = createContext({
errorMessage: "Radio must be used within RadioContextProvider",
});

export interface RadioLabelProps extends BoxProps {}
export interface RadioLabelProps extends BoxProps {
/**
* How large should the radio be?
*/
size?: keyof Renderlesskit.GetThemeValue<"radio", "size">;
}
export const RadioLabel = forwardRefWithAs<
RadioLabelProps,
HTMLLabelElement,
"label"
>(({ children, className, ...props }, ref) => {
>(({ children, className, size = "sm", ...props }, ref) => {
const theme = useTheme();
const radio = useRadio();
const _size = size || radio?.size || "md";

return (
<Box
as="label"
ref={ref}
className={cx(theme.radio.base, className)}
className={cx(theme.radio.base, theme.radio.size[_size], className)}
{...props}
>
{children}
Expand All @@ -39,7 +47,7 @@ export const RadioLabel = forwardRefWithAs<
});

export const Radio = forwardRefWithAs<
Partial<RadioProps>,
Partial<ReakitRadioProps>,
HTMLInputElement,
"input"
>(({ children, className, ...props }, ref) => {
Expand All @@ -56,15 +64,18 @@ export const Radio = forwardRefWithAs<
);
});

type RadioGroupProps = Partial<ReakitRadioGroupProps & RadioInitialState> &
anuraghazra marked this conversation as resolved.
Show resolved Hide resolved
Partial<Pick<RadioLabelProps, "size">>;

export const RadioGroup = forwardRefWithAs<
Partial<RadioGroupProps & RadioInitialState>,
RadioGroupProps,
HTMLDivElement,
"div"
>(({ children, ...props }, ref) => {
>(({ children, size, ...props }, ref) => {
const radio = useRadioState(props);

return (
<RadioProvider value={radio}>
<RadioProvider value={{ state: radio, size }}>
<ReakitRadioGroup {...radio} ref={ref}>
{children}
</ReakitRadioGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/radio/stories/Radio.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default {

export const Default = () => {
return (
<RadioGroup>
<RadioGroup size="sm">
<div className="flex gap-5">
<RadioLabel>
<Radio value="1" /> One
Expand Down
5 changes: 5 additions & 0 deletions src/theme/defaultTheme/radio.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
export const radio = {
base: "lib:flex lib:items-center lib:gap-2",
size: {
sm: "lib:text-xs",
md: "lib:text-sm",
lg: "lib:text-base",
},
anuraghazra marked this conversation as resolved.
Show resolved Hide resolved
input: "custom-radio lib:text-gray-800",
};