48 lines
1.3 KiB
TypeScript
48 lines
1.3 KiB
TypeScript
import { FC } from "react";
|
|
import InputWrapper from "./InputWrapper";
|
|
import { useInputListStyles } from "./styles";
|
|
|
|
|
|
export interface Props<T extends string> {
|
|
current: T;
|
|
options: readonly T[];
|
|
onChange: (n: T) => void;
|
|
id?: string;
|
|
className?: string;
|
|
label: string;
|
|
}
|
|
export const InputList: FC<Props<any>> = ({
|
|
current, options, onChange, label,
|
|
...selattr
|
|
}) => {
|
|
const invalidCurrent = !options.includes(current);
|
|
const { classes } = useInputListStyles({
|
|
invalidCurrent
|
|
});
|
|
return <InputWrapper label={label} labelSize={'small'} noMargin error={invalidCurrent && "Invalid Selection"}>
|
|
<select
|
|
title={invalidCurrent ? "Invalid Selection" : ""}
|
|
className={classes.input}
|
|
{...selattr} value={current}
|
|
onChange={e => {
|
|
e.preventDefault();
|
|
console.log(e.currentTarget.id)
|
|
console.log(e.currentTarget.value)
|
|
console.log(e.currentTarget.innerHTML)
|
|
if (e.currentTarget.id === 'invalid') {
|
|
onChange(options[0])
|
|
} else {
|
|
onChange(e.currentTarget.value as any);
|
|
}
|
|
}}
|
|
>
|
|
{options.map((t, i) => <option id={String(i)} key={i} value={t}>{t}</option>)}
|
|
{invalidCurrent ? <option
|
|
id={'invalid'}
|
|
value={current}
|
|
style={{ display: 'none' }}
|
|
>!{current}!</option> : <></>}
|
|
</select>
|
|
</InputWrapper>
|
|
}
|