(ui): Make GroupSelect controlled input

This commit is contained in:
Ken Hibino 2022-03-25 07:00:31 -07:00
parent f31f248937
commit 55de4e84eb
2 changed files with 22 additions and 1 deletions

View File

@ -28,15 +28,27 @@ const useStyles = makeStyles((theme) => ({
})); }));
interface Props { interface Props {
selected: GroupInfo | null;
onSelect: (newVal: GroupInfo | null) => void;
groups: GroupInfo[]; groups: GroupInfo[];
error: string; error: string;
} }
export default function GroupSelect(props: Props) { export default function GroupSelect(props: Props) {
const classes = useStyles(); const classes = useStyles();
const [inputValue, setInputValue] = React.useState("");
return ( return (
<Autocomplete <Autocomplete
id="task-group-selector" id="task-group-selector"
value={props.selected}
onChange={(event: any, newValue: GroupInfo | null) => {
props.onSelect(newValue);
}}
inputValue={inputValue}
onInputChange={(event, newInputValue) => {
setInputValue(newInputValue);
}}
disableListWrap disableListWrap
ListboxComponent={ ListboxComponent={
ListboxComponent as React.ComponentType< ListboxComponent as React.ComponentType<

View File

@ -5,6 +5,7 @@ import { listGroupsAsync } from "../actions/groupsActions";
import GroupSelect from "./GroupSelect"; import GroupSelect from "./GroupSelect";
import { usePolling } from "../hooks"; import { usePolling } from "../hooks";
import { AppState } from "../store"; import { AppState } from "../store";
import { GroupInfo } from "../api";
const useStyles = makeStyles((theme) => ({ const useStyles = makeStyles((theme) => ({
groupSelector: { groupSelector: {
@ -34,6 +35,9 @@ interface Props {
} }
function TaskGroupsTable(props: Props & ConnectedProps<typeof connector>) { function TaskGroupsTable(props: Props & ConnectedProps<typeof connector>) {
const [selectedGroup, setSelectedGroup] = React.useState<GroupInfo | null>(
null
);
const { pollInterval, listGroupsAsync, queue } = props; const { pollInterval, listGroupsAsync, queue } = props;
const classes = useStyles(); const classes = useStyles();
@ -46,7 +50,12 @@ function TaskGroupsTable(props: Props & ConnectedProps<typeof connector>) {
return ( return (
<div> <div>
<div className={classes.groupSelector}> <div className={classes.groupSelector}>
<GroupSelect groups={props.groups} error={props.groupsError} /> <GroupSelect
selected={selectedGroup}
onSelect={setSelectedGroup}
groups={props.groups}
error={props.groupsError}
/>
</div> </div>
</div> </div>
); );