'use client';
import { InputHTMLAttributes, useRef, RefObject, ReactNode } from 'react';
import { useFormStatus } from 'react-dom';
import Spinner from './Spinner';
import { clsx } from 'clsx/lite';
import { FieldSetType, AnnotatedTag } from '@/photo/form';
import TagInput from './TagInput';
import { parameterize } from '@/utility/string';
import Checkbox from './Checkbox';
import ResponsiveText from './primitives/ResponsiveText';
import Tooltip from './Tooltip';
import { SelectMenuOptionType } from './SelectMenuOption';
import SelectMenu from './SelectMenu';
export default function FieldsetWithStatus({
id: _id,
label,
icon,
note,
noteShort,
tooltip,
error,
value,
isModified,
onChange,
className,
selectOptions,
selectOptionsDefaultLabel,
tagOptions,
tagOptionsLimit,
tagOptionsLimitValidationMessage,
tagOptionsDefaultIcon,
placeholder,
loading,
required,
readOnly,
spellCheck,
capitalize,
type = 'text',
inputRef: inputRefProp,
accessory,
hideLabel,
tabIndex,
}: {
id?: string
label: string
icon?: ReactNode
note?: string
noteShort?: string
tooltip?: string
error?: string
value: string
isModified?: boolean
onChange?: (value: string) => void
className?: string
selectOptions?: SelectMenuOptionType[]
selectOptionsDefaultLabel?: string
tagOptions?: AnnotatedTag[]
tagOptionsLimit?: number
tagOptionsLimitValidationMessage?: string
tagOptionsDefaultIcon?: ReactNode
placeholder?: string
loading?: boolean
required?: boolean
readOnly?: boolean
spellCheck?: boolean
capitalize?: boolean
type?: FieldSetType
inputRef?: RefObject
accessory?: React.ReactNode
hideLabel?: boolean
tabIndex?: number
}) {
const inputRefInternal = useRef(null);
const inputRef = inputRefProp ?? inputRefInternal;
const id = _id || parameterize(label);
const { pending } = useFormStatus();
const inputProps: InputHTMLAttributes = {
id,
name: id,
type,
value,
checked: type === 'checkbox'
? value === 'true' ? true : false
: undefined,
placeholder,
onChange: e => onChange?.(type === 'checkbox'
? e.target.value === 'true' ? 'false' : 'true'
: e.target.value),
spellCheck,
autoComplete: 'off',
autoCapitalize: !capitalize ? 'off' : undefined,
readOnly: readOnly || pending || loading,
disabled: type === 'checkbox' && (
readOnly || pending || loading
),
className: clsx(
(
type === 'text' ||
type === 'email' ||
type === 'password'
) && 'w-full',
type === 'checkbox' && (
readOnly || pending || loading
) && 'opacity-50 cursor-not-allowed',
Boolean(error) && 'error',
),
tabIndex,
};
return (
type === 'hidden'
?
:
{!hideLabel &&
}
{selectOptions
?
: tagOptions
?
: type === 'textarea'
?
);
};