GeneIT/geneit_app/src/widgets/forms/MemberInput.tsx

84 lines
2.3 KiB
TypeScript
Raw Normal View History

2023-08-11 08:49:40 +00:00
import ClearIcon from "@mui/icons-material/Clear";
2023-08-11 09:51:59 +00:00
import { Autocomplete, IconButton, TextField, Typography } from "@mui/material";
2023-08-11 08:49:40 +00:00
import React from "react";
import { useNavigate } from "react-router-dom";
2023-08-11 09:51:59 +00:00
import { Member } from "../../api/MemberApi";
import { useFamily } from "../BaseFamilyRoute";
2023-08-11 09:51:59 +00:00
import { MemberItem } from "../MemberItem";
2023-08-10 12:13:27 +00:00
export function MemberInput(p: {
editable: boolean;
current?: number;
onValueChange: (n?: number) => void;
label: string;
filter: (m: Member) => boolean;
}): React.ReactElement {
const n = useNavigate();
const family = useFamily();
const choices = family.members.filter(p.filter);
const [inputValue, setInputValue] = React.useState("");
if (p.current) {
const member = family.members.get(p.current)!;
return (
<div style={{ display: "flex", alignItems: "center" }}>
<Typography variant="body2">{p.label}</Typography>
<MemberItem
member={member}
onClick={
!p.editable
? () => {
n(family.family.URL(`member/${member.id}`));
}
: undefined
}
secondary={
p.editable ? (
<>
<IconButton
edge="end"
onClick={() => p.onValueChange(undefined)}
>
<ClearIcon />
</IconButton>
</>
) : undefined
}
/>
</div>
);
}
if (!p.editable) return <></>;
return (
<Autocomplete
value={p.current ? family.members.get(p.current) : undefined}
onChange={(_event: any, newValue: Member | null | undefined) => {
p.onValueChange(newValue?.id);
}}
inputValue={inputValue}
onInputChange={(_event, newInputValue) => {
setInputValue(newInputValue);
}}
options={choices}
sx={{ width: "100%" }}
filterOptions={(options, state) =>
options.filter((m) =>
m?.fullName.toLowerCase().includes(state.inputValue)
)
}
getOptionLabel={(o) => o?.fullName ?? ""}
renderInput={(params) => <TextField {...params} label={p.label} />}
renderOption={(_props, option, _state) => (
<MemberItem
member={option}
onClick={() => p.onValueChange(option?.id)}
/>
)}
/>
);
}