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
fix(radio): fix radio state context
  • Loading branch information
anuraghazra committed Jan 27, 2021
commit af8a92cfa0c61679d64b253835ed4a008236e2f4
30 changes: 19 additions & 11 deletions src/radio/Radio.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
import React from "react";
import { cx } from "@renderlesskit/react";
import {
Radio as ReakitRadio,
RadioProps as ReakitRadioProps,
RadioGroup as ReakitRadioGroup,
RadioGroupProps as ReakitRadioGroupProps,
useRadio,
} from "reakit";
import { BoxProps, Box } from "../box";
import { cx } from "@renderlesskit/react";

import { useTheme } from "../theme";
import { BoxProps, Box } from "../box";
import { createContext } from "../utils";
import { forwardRefWithAs } from "../utils/types";
import { useRadioState, RadioInitialState } from "./useRadioState";
import {
useRadioState,
RadioInitialState,
RadioStateReturn,
} from "./useRadioState";

const [RadioProvider, useRadioContext] = createContext({
const [RadioProvider, useRadioContext] = createContext<{
radioState: RadioStateReturn;
radioSize?: RadioLabelProps["size"];
}>({
name: "RadioContext",
strict: true,
errorMessage: "Radio must be used within RadioContextProvider",
Expand All @@ -25,14 +32,15 @@ export interface RadioLabelProps extends BoxProps {
*/
size?: keyof Renderlesskit.GetThemeValue<"radio", "size">;
}

export const RadioLabel = forwardRefWithAs<
RadioLabelProps,
HTMLLabelElement,
"label"
>(({ children, className, size = "sm", ...props }, ref) => {
>(({ children, className, size, ...props }, ref) => {
const theme = useTheme();
const radio = useRadio();
const _size = size || radio?.size || "md";
const { radioSize } = useRadioContext();
const _size = size || radioSize || "sm";

return (
<Box
Expand All @@ -51,13 +59,13 @@ export const Radio = forwardRefWithAs<
HTMLInputElement,
"input"
>(({ children, className, ...props }, ref) => {
const state = useRadioContext();
const { radioState } = useRadioContext();
const theme = useTheme();

return (
<ReakitRadio
className={theme.radio.input}
{...state}
{...radioState}
{...props}
ref={ref}
/>
Expand All @@ -75,7 +83,7 @@ export const RadioGroup = forwardRefWithAs<
const radio = useRadioState(props);

return (
<RadioProvider value={{ state: radio, size }}>
<RadioProvider value={{ radioState: radio, radioSize: size }}>
<ReakitRadioGroup {...radio} ref={ref}>
{children}
</ReakitRadioGroup>
Expand Down
4 changes: 2 additions & 2 deletions src/radio/useRadioState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export type RadioActions = CompositeActions & {
/**
* Sets `state`.
*/
setState: React.Dispatch<React.SetStateAction<string | number>>;
setState: React.Dispatch<React.SetStateAction<string | number | undefined>>;
};

export type RadioInitialState = CompositeInitialState &
Expand All @@ -40,7 +40,7 @@ export function useRadioState(
...props
} = initialState;

const [state, setState] = useControllableState({
const [state, setState] = useControllableState<string | number | undefined>({
value: initialValue,
defaultValue: defaultState,
onChange: onStateChange,
Expand Down