2022-05-04 17:09:48 +01:00
|
|
|
/*
|
2024-09-06 10:22:13 +02:00
|
|
|
Copyright 2022-2024 New Vector Ltd.
|
2022-05-04 17:09:48 +01:00
|
|
|
|
2025-02-18 17:59:58 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
2024-09-06 10:22:13 +02:00
|
|
|
Please see LICENSE in the repository root for full details.
|
2022-05-04 17:09:48 +01:00
|
|
|
*/
|
|
|
|
|
|
2023-06-30 18:21:18 -04:00
|
|
|
import {
|
2024-12-11 09:27:55 +00:00
|
|
|
type ChangeEvent,
|
|
|
|
|
type FC,
|
|
|
|
|
type ForwardedRef,
|
|
|
|
|
type ReactNode,
|
2023-06-30 18:21:18 -04:00
|
|
|
useId,
|
2025-01-13 14:54:42 +00:00
|
|
|
type JSX,
|
2025-06-23 22:48:37 -04:00
|
|
|
type Ref,
|
2023-06-30 18:21:18 -04:00
|
|
|
} 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;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-05 18:53:05 +02:00
|
|
|
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 {
|
2025-06-23 22:48:37 -04:00
|
|
|
ref?: Ref<HTMLInputElement | HTMLTextAreaElement>;
|
2023-03-01 13:47:36 +01:00
|
|
|
label?: string;
|
2022-08-09 11:44:46 +02:00
|
|
|
type: string;
|
|
|
|
|
prefix?: string;
|
|
|
|
|
suffix?: string;
|
|
|
|
|
id?: string;
|
|
|
|
|
checked?: boolean;
|
|
|
|
|
className?: string;
|
2023-03-01 13:47:36 +01:00
|
|
|
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;
|
2024-12-02 17:15:33 +00:00
|
|
|
min?: number;
|
2023-06-30 16:43:28 +01:00
|
|
|
onChange?: (event: ChangeEvent<HTMLInputElement>) => void;
|
2022-08-09 11:44:46 +02:00
|
|
|
}
|
|
|
|
|
|
2025-06-23 22:48:37 -04:00
|
|
|
export const InputField: FC<InputFieldProps> = ({
|
|
|
|
|
ref,
|
|
|
|
|
id,
|
|
|
|
|
label,
|
|
|
|
|
className,
|
|
|
|
|
type,
|
|
|
|
|
checked,
|
|
|
|
|
prefix,
|
|
|
|
|
suffix,
|
|
|
|
|
description,
|
|
|
|
|
disabled,
|
|
|
|
|
min,
|
|
|
|
|
...rest
|
|
|
|
|
}) => {
|
|
|
|
|
const descriptionId = useId();
|
2022-11-07 12:28:54 +00:00
|
|
|
|
2025-06-23 22:48:37 -04:00
|
|
|
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
|
|
|
|
2025-06-23 22:48:37 -04:00
|
|
|
<label htmlFor={id}>
|
|
|
|
|
{type === "checkbox" && (
|
|
|
|
|
<div className={styles.checkbox}>
|
|
|
|
|
<CheckIcon />
|
|
|
|
|
</div>
|
2022-11-04 18:10:53 +00:00
|
|
|
)}
|
2025-06-23 22:48:37 -04: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
|
|
|
|
2023-12-19 11:00:33 -05: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>
|
|
|
|
|
);
|