import { HTMLProps, forwardRef } from 'react';
import { RefCallBack } from 'react-hook-form';
import { Box } from '../box/Box';
import { Input, InputSize } from '../inputv2/inputv2';
import { AddButton, SubButton } from './InputButtons';
export type InputListProps = Omit, 'size'> & {
ref?: RefCallBack;
min?: string | number;
max?: string | number;
maxLength?: number;
minLength?: number;
pattern?: string;
required?: boolean;
disabled?: boolean;
maxItems?: number;
value: T[];
size?: InputSize;
};
function InternalInputList<
T extends string | number | readonly string[] | undefined,
>(
{
onChange,
onBlur,
min,
max,
maxLength,
minLength,
pattern,
required,
disabled,
maxItems,
value,
name,
size = '1/2',
...rest
}: InputListProps,
_,
) {
const isMaxItemsReached =
maxItems !== undefined && maxItems !== null && value.length === maxItems;
const insertEntry = () => {
if (!isMaxItemsReached) {
onChange?.({
target: { value: [...(value ?? []), ''] },
} as unknown as React.ChangeEvent);
}
};
const deleteEntry = (entryIndex: number) => {
const newValues = value.filter((_, index) => index !== entryIndex);
const updatedValues = newValues.length === 0 ? ([''] as T[]) : newValues;
onChange?.({
target: { value: updatedValues },
} as unknown as React.ChangeEvent);
};
return (
<>
{value.map((val, index) => (
{
const tempValues = [...value];
tempValues[index] = evt.target.value as T;
onChange?.({
target: { value: tempValues },
} as unknown as React.ChangeEvent);
}}
{...rest}
/>
))}
>
);
}
export const InputList = forwardRef(InternalInputList);