Skip to content

Commit

Permalink
chore: added react-hook-form examples (#35)
Browse files Browse the repository at this point in the history
* chore: added react hook form examples

* chore: review updates
  • Loading branch information
anuraghazra authored Sep 11, 2020
1 parent 3355e47 commit b0d89db
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 17 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
"prettier": "2.1.1",
"react": "16.13.1",
"react-dom": "16.13.1",
"react-hook-form": "^6.8.1",
"react-spring": "8.0.27",
"react-test-renderer": "16.13.1",
"react-transition-group": "4.4.1",
Expand Down
37 changes: 37 additions & 0 deletions src/number-input/stories/NumberInput.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from "react";
import { Meta } from "@storybook/react";
import { useForm, Controller } from "react-hook-form";

import { UseNumberInputProps, useNumberInputState } from "../NumberInputState";
import { NumberInput } from "../NumberInput";
Expand Down Expand Up @@ -30,6 +31,42 @@ export const Default = () => {
return <NumberInputComp {...props} />;
};

const NumberComponent: React.FC<any> = ({ onChange, value, name }) => {
const state = useNumberInputState({
onChange,
value,
});
return (
<>
<NumberInputDecrementButton {...state}>-</NumberInputDecrementButton>
<NumberInput name={name} {...state} />
<NumberInputIncrementButton {...state}>+</NumberInputIncrementButton>
</>
);
};

export const ReactHookForm = () => {
const { control, handleSubmit } = useForm<{
num: number;
}>({ defaultValues: { num: 20 } });

return (
<form
onSubmit={handleSubmit(values => {
alert(JSON.stringify(values));
})}
>
<div>
<Controller
name="num"
control={control}
render={NumberComponent as any}
/>
</div>
</form>
);
};

export const DefaultValue = () => {
const props = {
defaultValue: 15,
Expand Down
75 changes: 58 additions & 17 deletions src/slider/stories/Slider.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from "react";
import { Meta } from "@storybook/react";
import { useForm, Controller } from "react-hook-form";

import {
Slider,
Expand All @@ -19,34 +20,74 @@ import {
sliderVerticalThumbStyle,
sliderVerticalTrackStyle,
} from "./styles";
import { Button } from "../../button";

export default {
title: "Component/Slider",
} as Meta;

export const Default = () => {
const slider = useSliderState();
const SliderComp: React.FC<any> = props => {
const slider = useSliderState(props);

return (
<Slider {...slider} style={sliderHorizontalStyle}>
<SliderTrack {...slider} style={sliderHorizontalTrackStyle}>
<SliderFilledTrack
<>
<label htmlFor="slider">{slider.state.value}</label>

<Slider {...slider} style={sliderHorizontalStyle}>
<SliderTrack {...slider} style={sliderHorizontalTrackStyle}>
<SliderFilledTrack
{...slider}
style={sliderHorizontalFilledTractStyle}
/>
</SliderTrack>
<SliderThumb
{...slider}
style={sliderHorizontalFilledTractStyle}
aria-label="slider-thumb"
style={{
...sliderHorizontalThumbStyle,
transform: slider.state.isDragging
? "translateY(-50%) scale(1.15)"
: sliderHorizontalThumbStyle.transform,
}}
/>
</SliderTrack>
<SliderThumb
{...slider}
aria-label="slider-thumb"
style={{
...sliderHorizontalThumbStyle,
transform: slider.state.isDragging
? "translateY(-50%) scale(1.15)"
: sliderHorizontalThumbStyle.transform,

<SliderInput name={name} {...slider} />
</Slider>
</>
);
};

export const Default = () => {
return <SliderComp />;
};

export const ReactHookForm = () => {
const { handleSubmit, control, errors } = useForm<{
slider: number;
}>({
mode: "onChange",
defaultValues: { slider: 10 },
});

return (
<form
onSubmit={handleSubmit(values => {
alert(JSON.stringify(values));
})}
>
<Controller
name="slider"
control={control}
rules={{
required: true,
max: { value: 70, message: "Overflow" },
min: { value: 30, message: "Underflow" },
}}
render={SliderComp as any}
/>
<SliderInput {...slider} name="slider" />
</Slider>
<span>{errors.slider?.message}</span>
<Button type="submit">Submit</Button>
</form>
);
};

Expand Down

0 comments on commit b0d89db

Please sign in to comment.