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
chore: added controllable state
  • Loading branch information
anuraghazra committed Jan 22, 2021
commit cf0615178c9465332c2ded9f7e68cf42c61dceaa
4 changes: 2 additions & 2 deletions src/radio/Radio.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import React from "react";
import { cx } from "@renderlesskit/react";
import {
Radio as ReakitRadio,
RadioGroup as ReakitRadioGroup,
RadioGroupProps,
RadioInitialState,
RadioProps,
useRadioState,
} from "reakit";
import { useTheme } from "../theme";
import { createContext } from "../utils";
import { forwardRefWithAs } from "../utils/types";
import { useRadioState, RadioInitialState } from "./useRadioState";

const [RadioProvider, useRadioContext] = createContext({
name: "RadioContext",
Expand Down
27 changes: 27 additions & 0 deletions src/radio/stories/Radio.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from "react";
import { Meta } from "@storybook/react";
import { Radio, RadioGroup } from "../Radio";
import { Button } from "../../";

export default {
title: "Radio",
anuraghazra marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -22,6 +23,32 @@ export const Default = () => {
);
};

export const Controlled = () => {
const [state, setState] = React.useState("3");

console.log(state);
return (
<>
<RadioGroup
state={state}
onStateChange={e => {
setState(e as string);
}}
>
<div className="flex gap-5">
<Radio value="1">One</Radio>
<Radio value="2">Two</Radio>
<Radio value="3">Three</Radio>
<Radio value="4" disabled>
Disabled
</Radio>
</div>
</RadioGroup>
<Button onClick={() => setState("2")}>change</Button>
</>
);
};

export const States = () => {
return (
<RadioGroup state={"2"}>
Expand Down
55 changes: 55 additions & 0 deletions src/radio/useRadioState.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { useControllableState } from "@renderlesskit/react";
import * as React from "react";
import {
CompositeState,
CompositeActions,
CompositeInitialState,
useCompositeState,
} from "reakit";

export type RadioState = CompositeState & {
/**
* The `value` attribute of the current checked radio.
*/
state: string | number | undefined;
};

export type RadioActions = CompositeActions & {
/**
* Sets `state`.
*/
setState: React.Dispatch<React.SetStateAction<string | number>>;
};

export type RadioInitialState = CompositeInitialState &
Partial<Pick<RadioState, "state">> & {
defaultState?: RadioState["state"];
onStateChange?: (v: RadioState["state"]) => void;
};

export type RadioStateReturn = RadioState & RadioActions;

export function useRadioState(
initialState: RadioInitialState = {},
): RadioStateReturn {
const {
state: initialValue,
defaultState,
loop = true,
onStateChange,
...props
} = initialState;

const [state, setState] = useControllableState({
value: initialValue,
defaultValue: defaultState,
onChange: onStateChange,
});

const composite = useCompositeState({ ...props, loop });
return {
...composite,
state,
setState,
};
}