Files
element-call/src/input/Input.tsx

169 lines
3.7 KiB
TypeScript
Raw Normal View History

2022-05-04 17:09:48 +01:00
/*
Copyright 2022-2024 New Vector Ltd.
2022-05-04 17:09:48 +01:00
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
2022-05-04 17:09:48 +01:00
*/
import {
type ChangeEvent,
type FC,
type ForwardedRef,
type ReactNode,
useId,
type JSX,
type Ref,
} from "react";
2021-08-19 17:49:45 -07:00
import classNames from "classnames";
2022-08-09 11:44:46 +02:00
2021-08-19 17:49:45 -07:00
import styles from "./Input.module.css";
2023-09-27 19:06:10 -04:00
import CheckIcon from "../icons/Check.svg?react";
2022-10-10 09:19:10 -04:00
import { TranslatedError } from "../TranslatedError";
2021-08-19 17:49:45 -07:00
2022-08-09 11:44:46 +02:00
interface FieldRowProps {
children: ReactNode;
rightAlign?: boolean;
className?: string;
}
export function FieldRow({
children,
rightAlign,
className,
}: FieldRowProps): JSX.Element {
2021-08-19 17:49:45 -07:00
return (
<div
className={classNames(
styles.fieldRow,
{ [styles.rightAlign]: rightAlign },
2023-10-11 10:42:04 -04:00
className,
2021-08-19 17:49:45 -07:00
)}
>
{children}
</div>
);
}
2022-08-09 11:44:46 +02:00
interface FieldProps {
children: ReactNode;
className?: string;
}
function Field({ children, className }: FieldProps): JSX.Element {
2021-08-19 17:49:45 -07:00
return <div className={classNames(styles.field, className)}>{children}</div>;
}
2022-08-09 11:44:46 +02:00
interface InputFieldProps {
ref?: Ref<HTMLInputElement | HTMLTextAreaElement>;
label?: string;
2022-08-09 11:44:46 +02:00
type: string;
prefix?: string;
suffix?: string;
id?: string;
checked?: boolean;
className?: string;
description?: string | ReactNode;
2022-08-09 11:44:46 +02:00
disabled?: boolean;
required?: boolean;
// this is a hack. Those variables should be part of `HTMLAttributes<HTMLInputElement> | HTMLAttributes<HTMLTextAreaElement>`
// but extending from this union type does not work
name?: string;
autoComplete?: string;
autoCorrect?: string;
autoCapitalize?: string;
value?: string;
2023-05-22 14:33:20 -04:00
defaultValue?: string;
2022-08-09 11:44:46 +02:00
placeholder?: string;
defaultChecked?: boolean;
min?: number;
onChange?: (event: ChangeEvent<HTMLInputElement>) => void;
2022-08-09 11:44:46 +02:00
}
export const InputField: FC<InputFieldProps> = ({
ref,
id,
label,
className,
type,
checked,
prefix,
suffix,
description,
disabled,
min,
...rest
}) => {
const descriptionId = useId();
return (
<Field
className={classNames(
type === "checkbox" ? styles.checkboxField : styles.inputField,
{
[styles.prefix]: !!prefix,
[styles.disabled]: disabled,
},
className,
)}
>
{prefix && <span>{prefix}</span>}
{type === "textarea" ? (
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
<textarea
id={id}
ref={ref as ForwardedRef<HTMLTextAreaElement>}
disabled={disabled}
aria-describedby={descriptionId}
{...rest}
/>
) : (
<input
id={id}
ref={ref as ForwardedRef<HTMLInputElement>}
type={type}
checked={checked}
disabled={disabled}
aria-describedby={descriptionId}
min={min}
{...rest}
/>
)}
2022-02-23 16:07:14 -08:00
<label htmlFor={id}>
{type === "checkbox" && (
<div className={styles.checkbox}>
<CheckIcon />
</div>
2022-11-04 18:10:53 +00:00
)}
{label}
</label>
{suffix && <span>{suffix}</span>}
{description && (
<p
id={descriptionId}
className={
label
? styles.description
: classNames(styles.description, styles.noLabel)
}
>
{description}
</p>
)}
</Field>
);
};
2021-08-19 17:49:45 -07:00
InputField.displayName = "InputField";
2022-10-10 09:19:10 -04:00
interface ErrorMessageProps {
error: Error;
2021-08-20 16:23:12 -07:00
}
2022-10-10 09:19:10 -04:00
export const ErrorMessage: FC<ErrorMessageProps> = ({ error }) => (
<p className={styles.errorMessage}>
{error instanceof TranslatedError ? error.translatedMessage : error.message}
</p>
);