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(apps/studio): Checkbox implementation for Storybook #688

Merged
merged 9 commits into from
Sep 1, 2023
201 changes: 180 additions & 21 deletions apps/studio/stories/Checkbox.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,34 +1,193 @@
import { Meta, StoryObj } from "@storybook/react";

import { Checkbox, Label } from "@kampus/ui";

const labels = ["köpekleri çıkarmak", "alışverişe gitmek", "kitap okumak"];
import { Button, Checkbox, Form, FormControl, FormDescription, FormField, FormItem, FormLabel, Label, toast, useForm, z } from "@kampus/ui";
import { zodResolver } from "@hookform/resolvers/zod"
import { useState } from "react";

const meta = {
title: "Checkbox",
tags: ["autodocs"],
component: Checkbox,
argTypes: {
disabled: {
control: "boolean",
},
checked: {
control: "boolean",
},
}
} satisfies Meta<typeof Checkbox>;

export default meta;
type NewType = StoryObj<typeof meta>;

export const Default = {
render: () => (
type Story = StoryObj<typeof meta>;

export const DefaultChecked = {
args: {
defaultChecked: true,
},
} satisfies Story;

const OnFocusTemplate = ({ ...props }) => {
const [isFocused, setIsFocused] = useState(false)
return (
<div className="flex flex-col space-y-2 ">
<h4>Günlük Yapmam Gerekenler</h4>
{labels.map((label) => {
return (
<div className="flex items-center space-x-2">
<Checkbox id={label} />
<Label
htmlFor={label}
className="text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70"
>
{label}
</Label>
</div>
);
})}
<div className="flex items-center space-x-2">
<Checkbox id="checkbox"
onFocus={() => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should also use onBlur handler in order to handle focus loss.

setIsFocused(true)
}}
{...props} />
<Label
htmlFor="checkbox"
className="text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need font color here too, please use text-primary

>
<span id="label">
{isFocused ? "Focused" : "Default"}
</span>
</Label>
</div>
</div>
)
}

export const OnFocus = {
render: ({ ...props }) => <OnFocusTemplate {...props} />,
} satisfies Story;

const DescriptionTemplate = ({ ...props }) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can rename this as DefaultTemplate

return (

<div className="items-center flex space-x-2">
<Checkbox id="terms1" {...props} />
<div className="grid gap-1.5 leading-none">
<Label
htmlFor="terms1"
className="text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should give a font color to this label, ideally text-primary.

>
{props?.children
? props?.children
: "Default"
}
</Label>
{props?.description && (
<p className="text-sm text-muted-foreground">

<p className="text-sm text-muted-foreground">
{props?.description}
</p>
</p>
)}
{props?.error && (
<p className="text-sm text-muted-foreground">
<p className="text-sm text-red-500">
{props?.error}
</p>
</p>
)}
</div>
</div >
)
}

export const WithError = {
argTypes: {
disabled: {
control: "boolean",
},
checked: {
control: "boolean",
}
},
render: ({ ...props }) => (
<DescriptionTemplate {...props} error="This is an error" />
),
};
} satisfies Story;

export const WithText = {
argTypes: {
disabled: {
control: "boolean",
},
checked: {
control: "boolean",
}
},
render: ({ ...props }) => (
<DescriptionTemplate {...props} description="This is a description" />
),
} satisfies Story;

const FormComponent = ({ ...props }) => {
const FormSchema = z.object({
mobile: z.boolean().default(false).optional(),
})

function onSubmit(data: z.infer<typeof FormSchema>) {
toast({
title: "You submitted the following values:",
description: (
<pre className="mt-2 w-[340px] rounded-md bg-slate-950 p-4">
<code className="text-white">{JSON.stringify(data, null, 2)}</code>
</pre>
),
})
}

const form = useForm<z.infer<typeof FormSchema>>(
FormSchema,
{
resolver: zodResolver(FormSchema),
values: {
mobile: props?.checked,
}
},
)

return (
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-6">
<FormField
control={form.control}
name="mobile"
render={({ field }) => (
<FormItem className="flex flex-row items-start space-x-3 space-y-0 rounded-md border p-4">
<FormControl>
<Checkbox
checked={field.value}
onCheckedChange={field.onChange}
{...props}
/>
</FormControl>
<div className="space-y-1 leading-none">
<FormLabel>
Use different settings for my mobile devices
</FormLabel>
<FormDescription>
You can manage your mobile notifications in the mobile settings page.
</FormDescription>
</div>
</FormItem>
)}
/>
<Button type="submit">Submit</Button>
</form>
</Form>
)
}

export const InsideForm = {
render: ({ ...props }) => <FormComponent {...props} />
} satisfies Story;


export const Default = {
args: {
children: "Default"
},
render: (
{ ...props }
) => (
<DescriptionTemplate {...props} />
)
} satisfies Story;