Cleaning up storybook files (#5290)

* storybook v2 init

* Merge config into storybook vite build

* Remove path

* Storybook config tweaks

* Added styles and settings for storybook v2, and started working on button documentation and examples.

* Badges + flex wrap on mobile

* Breadcrumbs+button+avatar

* Checkbox

* Input + moving files around

* WIP table

* WIP table grid

* Replaced imports for new components.

* Added first steps for varianttable.

* Small alignment fix.

* Custom Args Table - With scrollbar

* Adding table to components that need it + darkmode

* Add intro

* Fix types

* Remove V1 storybook and replace with V2

* Fix badge type error

* Fixed storybook dependencies

* Added cover image to storybook

* Remove vita from ts config, we dont use vite.

* Fixed button import.

* Explained postcss pseudo plugin.

* Fixed badge import.

* Add Avatar Stories

* ButtonGroup Stories

* Fixed imports

* Add checkbox stories

* Add  exports for differnt types of inputs

* Fix form and text area input

* Fix mass import errors

Co-authored-by: Jeroen Reumkens <[email protected]>
Co-authored-by: Alex van Andel <[email protected]>
This commit is contained in:
authored and GitHub committed 2022-11-04 15:40:46 +00:00
1 parent acad0619f5
commit b5b41da183
234 files changed
+2709 -2692

No files matched your search

@@ -0,0 +1,102 @@
import { X, Circle, Check } from "react-feather";
import { FieldValues, useFormContext } from "react-hook-form";
// TODO: Refactor import once V1 migration has happened
import { Icon } from "../../../Icon";
export function HintsOrErrors<T extends FieldValues = FieldValues>(props: {
hintErrors?: string[];
fieldName: string;
t: (key: string) => string;
}) {
const methods = useFormContext() as ReturnType<typeof useFormContext> | null;
/* If there's no methods it means we're using these components outside a React Hook Form context */
if (!methods) return null;
const { formState } = methods;
const { hintErrors, fieldName, t } = props;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const fieldErrors: FieldErrors<T> | undefined = formState.errors[fieldName];
if (!hintErrors && fieldErrors && !fieldErrors.message) {
// no hints passed, field errors exist and they are custom ones
return (
<div className="text-gray mt-2 flex items-center text-sm text-gray-700">
<ul className="ml-2">
{Object.keys(fieldErrors).map((key: string) => {
return (
<li key={key} className="text-blue-700">
{t(`${fieldName}_hint_${key}`)}
</li>
);
})}
</ul>
</div>
);
}
if (hintErrors && fieldErrors) {
// hints passed, field errors exist
return (
<div className="text-gray mt-2 flex items-center text-sm text-gray-700">
<ul className="ml-2">
{hintErrors.map((key: string) => {
const submitted = formState.isSubmitted;
const error = fieldErrors[key] || fieldErrors.message;
return (
<li
key={key}
className={error !== undefined ? (submitted ? "text-red-700" : "") : "text-green-600"}>
{error !== undefined ? (
submitted ? (
<X size="12" strokeWidth="3" className="mr-2 -ml-1 inline-block" />
) : (
<Circle fill="currentColor" size="5" className="mr-2 inline-block" />
)
) : (
<Check size="12" strokeWidth="3" className="mr-2 -ml-1 inline-block" />
)}
{t(`${fieldName}_hint_${key}`)}
</li>
);
})}
</ul>
</div>
);
}
// errors exist, not custom ones, just show them as is
if (fieldErrors) {
return (
<div className="text-gray mt-2 flex items-center text-sm text-red-700">
<Icon.FiInfo className="mr-1 h-3 w-3" />
<>{fieldErrors.message}</>
</div>
);
}
if (!hintErrors) return null;
// hints passed, no errors exist, proceed to just show hints
return (
<div className="text-gray mt-2 flex items-center text-sm text-gray-700">
<ul className="ml-2">
{hintErrors.map((key: string) => {
// if field was changed, as no error exist, show checked status and color
const dirty = formState.dirtyFields[fieldName];
return (
<li key={key} className={!!dirty ? "text-green-600" : ""}>
{!!dirty ? (
<Icon.FiCheck size="12" strokeWidth="3" className="mr-2 -ml-1 inline-block" />
) : (
<Icon.FiCircle fill="currentColor" size="5" className="mr-2 inline-block" />
)}
{t(`${fieldName}_hint_${key}`)}
</li>
);
})}
</ul>
</div>
);
}
@@ -0,0 +1,344 @@
import { forwardRef, ReactElement, ReactNode, Ref, useCallback, useId, useState } from "react";
import React from "react";
import { Eye, EyeOff } from "react-feather";
import { FieldValues, FormProvider, SubmitHandler, useFormContext, UseFormReturn } from "react-hook-form";
import classNames from "@calcom/lib/classNames";
import { getErrorFromUnknown } from "@calcom/lib/errors";
import { useLocale } from "@calcom/lib/hooks/useLocale";
import { Skeleton, Tooltip, Alert, showToast } from "@calcom/ui/v2";
import { HintsOrErrors } from "./HintOrErrors";
import { Label } from "./Label";
type InputProps = JSX.IntrinsicElements["input"];
export const Input = forwardRef<HTMLInputElement, InputProps>(function Input(props, ref) {
return (
<input
{...props}
ref={ref}
className={classNames(
"mb-2 block h-9 w-full rounded-md border border-gray-300 py-2 px-3 text-sm placeholder:text-gray-400 hover:border-gray-400 focus:border-neutral-300 focus:outline-none focus:ring-2 focus:ring-neutral-800 focus:ring-offset-1",
props.className
)}
/>
);
});
export function InputLeading(props: JSX.IntrinsicElements["div"]) {
return (
<span className="inline-flex flex-shrink-0 items-center rounded-l-sm border border-r-0 border-gray-300 bg-gray-50 px-3 text-gray-500 sm:text-sm">
{props.children}
</span>
);
}
type InputFieldProps = {
label?: ReactNode;
hint?: ReactNode;
hintErrors?: string[];
addOnLeading?: ReactNode;
addOnSuffix?: ReactNode;
addOnFilled?: boolean;
error?: string;
labelSrOnly?: boolean;
containerClassName?: string;
t?: (key: string) => string;
} & React.ComponentProps<typeof Input> & {
labelProps?: React.ComponentProps<typeof Label>;
labelClassName?: string;
};
type AddonProps = {
children: React.ReactNode;
isFilled?: boolean;
className?: string;
error?: boolean;
};
const Addon = ({ isFilled, children, className, error }: AddonProps) => (
<div
className={classNames(
"addon-wrapper h-9 border border-gray-300 px-3",
isFilled && "bg-gray-100",
className
)}>
<div className={classNames("flex h-full flex-col justify-center px-1 text-sm", error && "text-red-900")}>
<span className="whitespace-nowrap py-2.5">{children}</span>
</div>
</div>
);
export const InputField = forwardRef<HTMLInputElement, InputFieldProps>(function InputField(props, ref) {
const id = useId();
const { t: _t, isLocaleReady, i18n } = useLocale();
const t = props.t || _t;
const name = props.name || "";
const {
label = t(name),
labelProps,
labelClassName,
placeholder = isLocaleReady && i18n.exists(name + "_placeholder") ? t(name + "_placeholder") : "",
className,
addOnLeading,
addOnSuffix,
addOnFilled = true,
hint,
hintErrors,
labelSrOnly,
containerClassName,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
t: __t,
...passThrough
} = props;
return (
<div className={classNames(containerClassName)}>
{!!name && (
<Skeleton
as={Label}
htmlFor={id}
loadingClassName="w-16"
{...labelProps}
className={classNames(labelClassName, labelSrOnly && "sr-only", props.error && "text-red-900")}>
{label}
</Skeleton>
)}
{addOnLeading || addOnSuffix ? (
<div className="relative mb-1 flex items-center rounded-md focus-within:outline-none focus-within:ring-2 focus-within:ring-neutral-800 focus-within:ring-offset-1">
{addOnLeading && (
<Addon isFilled={addOnFilled} className="rounded-l-md border-r-0">
{addOnLeading}
</Addon>
)}
<Input
id={id}
placeholder={placeholder}
className={classNames(
className,
addOnLeading && "rounded-l-none",
addOnSuffix && "rounded-r-none",
"!my-0 !ring-0"
)}
{...passThrough}
ref={ref}
/>
{addOnSuffix && (
<Addon isFilled={addOnFilled} className="rounded-r-md border-l-0">
{addOnSuffix}
</Addon>
)}
</div>
) : (
<Input id={id} placeholder={placeholder} className={className} {...passThrough} ref={ref} />
)}
<HintsOrErrors hintErrors={hintErrors} fieldName={name} t={t} />
{hint && <div className="text-gray mt-2 flex items-center text-sm text-gray-700">{hint}</div>}
</div>
);
});
export const TextField = forwardRef<HTMLInputElement, InputFieldProps>(function TextField(props, ref) {
return <InputField ref={ref} {...props} />;
});
export const PasswordField = forwardRef<HTMLInputElement, InputFieldProps>(function PasswordField(
props,
ref
) {
const { t } = useLocale();
const [isPasswordVisible, setIsPasswordVisible] = useState(false);
const toggleIsPasswordVisible = useCallback(
() => setIsPasswordVisible(!isPasswordVisible),
[isPasswordVisible, setIsPasswordVisible]
);
const textLabel = isPasswordVisible ? t("hide_password") : t("show_password");
return (
<div className="relative [&_.group:hover_.addon-wrapper]:border-gray-400 [&_.group:focus-within_.addon-wrapper]:border-neutral-300">
<InputField
type={isPasswordVisible ? "text" : "password"}
placeholder={props.placeholder || "•••••••••••••"}
ref={ref}
{...props}
className={classNames("mb-0 border-r-0 pr-10", props.className)}
addOnFilled={false}
addOnSuffix={
<Tooltip content={textLabel}>
<button
className="absolute right-3 bottom-0 h-9 text-gray-900"
type="button"
onClick={() => toggleIsPasswordVisible()}>
{isPasswordVisible ? (
<EyeOff className="h-4 stroke-[2.5px]" />
) : (
<Eye className="h-4 stroke-[2.5px]" />
)}
<span className="sr-only">{textLabel}</span>
</button>
</Tooltip>
}
/>
</div>
);
});
export const EmailInput = forwardRef<HTMLInputElement, InputFieldProps>(function EmailInput(props, ref) {
return (
<Input
ref={ref}
type="email"
autoCapitalize="none"
autoComplete="email"
autoCorrect="off"
inputMode="email"
{...props}
/>
);
});
export const EmailField = forwardRef<HTMLInputElement, InputFieldProps>(function EmailField(props, ref) {
return (
<InputField
ref={ref}
type="email"
autoCapitalize="none"
autoComplete="email"
autoCorrect="off"
inputMode="email"
{...props}
/>
);
});
type TextAreaProps = JSX.IntrinsicElements["textarea"];
export const TextArea = forwardRef<HTMLTextAreaElement, TextAreaProps>(function TextAreaInput(props, ref) {
return (
<textarea
ref={ref}
{...props}
className={classNames(
"block w-full rounded-md border border-gray-300 py-2 px-3 hover:border-gray-400 focus:border-neutral-300 focus:outline-none focus:ring-2 focus:ring-neutral-800 focus:ring-offset-1 sm:text-sm",
props.className
)}
/>
);
});
type TextAreaFieldProps = {
label?: ReactNode;
t?: (key: string) => string;
} & React.ComponentProps<typeof TextArea> & {
name: string;
labelProps?: React.ComponentProps<typeof Label>;
};
export const TextAreaField = forwardRef<HTMLTextAreaElement, TextAreaFieldProps>(function TextField(
props,
ref
) {
const id = useId();
const { t: _t } = useLocale();
const t = props.t || _t;
const methods = useFormContext();
const {
label = t(props.name as string),
labelProps,
/** Prevents displaying untranslated placeholder keys */
placeholder = t(props.name + "_placeholder") !== props.name + "_placeholder"
? t(props.name + "_placeholder")
: "",
...passThrough
} = props;
return (
<div>
{!!props.name && (
<Label htmlFor={id} {...labelProps}>
{label}
</Label>
)}
<TextArea ref={ref} placeholder={placeholder} {...passThrough} />
{methods?.formState?.errors[props.name]?.message && (
<Alert
className="mt-1"
severity="error"
message={<>{methods.formState.errors[props.name]?.message}</>}
/>
)}
</div>
);
});
type FormProps<T extends object> = { form: UseFormReturn<T>; handleSubmit: SubmitHandler<T> } & Omit<
JSX.IntrinsicElements["form"],
"onSubmit"
>;
const PlainForm = <T extends FieldValues>(props: FormProps<T>, ref: Ref<HTMLFormElement>) => {
const { form, handleSubmit, ...passThrough } = props;
return (
<FormProvider {...form}>
<form
ref={ref}
onSubmit={(event) => {
event.preventDefault();
event.stopPropagation();
form
.handleSubmit(handleSubmit)(event)
.catch((err) => {
showToast(`${getErrorFromUnknown(err).message}`, "error");
});
}}
{...passThrough}>
{
/* @see https://react-hook-form.com/advanced-usage/#SmartFormComponent */
React.Children.map(props.children, (child) => {
return typeof child !== "string" &&
typeof child !== "number" &&
typeof child !== "boolean" &&
child &&
"props" in child &&
child.props.name
? React.createElement(child.type, {
...{
...child.props,
register: form.register,
key: child.props.name,
},
})
: child;
})
}
</form>
</FormProvider>
);
};
export const Form = forwardRef(PlainForm) as <T extends FieldValues>(
p: FormProps<T> & { ref?: Ref<HTMLFormElement> }
) => ReactElement;
export function FieldsetLegend(props: JSX.IntrinsicElements["legend"]) {
return (
<legend {...props} className={classNames("text-sm font-medium text-gray-700", props.className)}>
{props.children}
</legend>
);
}
export function InputGroupBox(props: JSX.IntrinsicElements["div"]) {
return (
<div
{...props}
className={classNames("space-y-2 rounded-sm border border-gray-300 bg-white p-2", props.className)}>
{props.children}
</div>
);
}
export const MinutesField = forwardRef<HTMLInputElement, InputFieldProps>(function MinutesField(props, ref) {
return <InputField ref={ref} type="number" min={0} {...props} addOnSuffix="mins" />;
});
@@ -0,0 +1,11 @@
import { classNames } from "@calcom/lib";
export function Label(props: JSX.IntrinsicElements["label"]) {
return (
<label
{...props}
className={classNames("mb-2 block text-sm font-medium leading-none text-gray-700", props.className)}>
{props.children}
</label>
);
}
@@ -0,0 +1,83 @@
import { Canvas, Meta, Story, ArgsTable } from '@storybook/addon-docs';
import { Examples, Example, Note, Title,CustomArgsTable, VariantRow,VariantsTable} from '@calcom/storybook/components'
import { Icon } from "@calcom/ui/Icon";
import {InputField} from "./Input"
<Meta title="UI/Form/Input Field" component={InputField} />
<Title title="Inputs" suffix="Brief" subtitle="Version 2.0 — Last Update: 22 Aug 2022"/>
## Definition
Text fields allow users to input and edit text into the product. Usually appear in forms and modals. Various options can be shown with the field to communicate the input requirements.## Structure
## Structure
<CustomArgsTable of={InputField} />
<Examples title="Inputs" footnote={
<ul>
<li>The width is flexible but the height is fixed for both desktop and mobile. </li>
</ul>
}>
<Example title="Default">
<InputField placeholder="Default"/>
</Example>
</Examples>
<Examples title="Input Types">
<Example title="Default">
<InputField placeholder="Default"/>
</Example>
<Example title="Prefix">
<InputField placeholder="Prefix" addOnLeading={<>Prefix</>}/>
</Example>
<Example title="Suffix">
<InputField placeholder="Suffic" addOnSuffix={<>Suffix</>}/>
</Example>
<Example title="Focused">
<InputField placeholder="Focused" className="sb-pseudo--focus"/>
</Example>
<Example title="Hovered">
<InputField placeholder="Hovered" className="sb-pseudo--hover"/>
</Example>
<Example title="Error">
<InputField placeholder="Error" error="Error"/>
</Example>
</Examples>
<Examples title="Input Text">
<Example title="Default">
<InputField />
</Example>
<Example title="Placeholder">
<InputField placeholder="Placeholder"/>
</Example>
<Example title="Filled">
<InputField value="Filled"/>
</Example>
</Examples>
<Title offset title="Input" suffix="Variants" />
<Canvas>
<Story name="All Variants">
<VariantsTable titles={['Default','Focused', 'Hovered']} columnMinWidth={150}>
<VariantRow variant="Default">
<InputField placeholder="Default"/>
<InputField placeholder="Focused" className="sb-pseudo--focus"/>
<InputField placeholder="Hovered" className="sb-pseudo--hover"/>
</VariantRow>
<VariantRow variant="Prefixed">
<InputField placeholder="Default" addOnLeading={<>Prefix</>}/>
<InputField placeholder="Focused" className="sb-pseudo--focus" addOnLeading={<>Prefix</>}/>
<InputField placeholder="Hovered" className="sb-pseudo--hover" addOnLeading={<>Prefix</>}/>
</VariantRow>
<VariantRow variant="Suffix">
<InputField placeholder="Default" addOnSuffix={<>Prefix</>}/>
<InputField placeholder="Focused" className="sb-pseudo--focus" addOnSuffix={<>Prefix</>}/>
<InputField placeholder="Hovered" className="sb-pseudo--hover" addOnSuffix={<>Prefix</>}/>
</VariantRow>
</VariantsTable>
</Story>
</Canvas>