This repository has been archived on 2024-11-11. You can view files and clone it, but cannot push or open issues or pull requests.
react-component-form/example/components/design/FormState.tsx

50 lines
1.2 KiB
TypeScript
Raw Normal View History

import classNames from "clsx"
import useTranslation from "next-translate/useTranslation"
import type { FetchState as FormStateType } from "react-component-form"
2022-08-26 20:19:31 +02:00
import { Loader } from "./Loader"
2022-08-26 20:19:31 +02:00
export interface FormStateProps extends React.ComponentPropsWithoutRef<"div"> {
2022-08-26 20:19:31 +02:00
state: FormStateType
message?: string
id?: string
}
export const FormState: React.FC<FormStateProps> = (props) => {
const { state, message, id, ...rest } = props
const { t } = useTranslation()
if (state === "loading") {
2022-08-26 20:19:31 +02:00
return (
<div data-cy="loader" className="mt-8 flex justify-center">
2022-08-26 20:19:31 +02:00
<Loader />
</div>
)
}
if (state === "idle" || message == null) {
2022-08-26 20:19:31 +02:00
return null
}
return (
<>
<div
{...rest}
className={classNames(
props.className,
"mt-6 flex max-w-xl items-center text-center font-medium",
2022-08-26 20:19:31 +02:00
{
"text-red-800 dark:text-red-400": state === "error",
"text-green-800 dark:text-green-400": state === "success",
},
2022-08-26 20:19:31 +02:00
)}
>
<div className="inline bg-cover font-headline" />
<span id={id} className="pl-2">
2022-08-26 20:19:31 +02:00
<b>{t(`common:${state}`)}:</b> {message}
</span>
</div>
</>
)
}