diff --git a/frontend/components/input/SelectionField.tsx b/frontend/components/input/SelectionField.tsx new file mode 100644 index 00000000..5d382405 --- /dev/null +++ b/frontend/components/input/SelectionField.tsx @@ -0,0 +1,137 @@ +import { useState } from "react"; + +import { Text } from "@/components/Text"; +import { Icon, IconName } from "@/components/graphics/Icon"; + +export type SelectionListItem = { + title: string; + description: string; + isSelected: boolean; + icon: IconName; +}; +interface SelectionFieldProps { + /** + * Title + */ + title: string; + /** + * Selection List + */ + list: SelectionListItem[]; + /** + * Additional classes + */ + className?: string; +} + +/** + * UI component for displaying a SelectionField + */ +export const SelectionField = ({ + title, + list, + className = "", + ...props +}: SelectionFieldProps) => { + const [visible, setVisible] = useState(false); + const [selected, setSelected] = useState( + getListSelected(list) + ); + return ( + <> +
+ + {title} + +
{ + setVisible(!visible); + }} + > +
+ {selected.icon && } +
+ {selected.title} + +
+ + {visible && ( + <> +
+ {list?.map((selectionItem) => ( + <> +
{ + setSelected(selectionItem); + setVisible(false); + setListSelected( + list, + selectionItem + ); + }} + > + +
+ + {" "} + {selectionItem.title} + +
+ + {selectionItem.description} + + + +
+
+
+ + ))} +
+ + )} +
+ + ); +}; + +function getListSelected(list: SelectionListItem[]) { + for (var i = 0; i < list.length; i++) { + if (list[i].isSelected) return list[i]; + } + const other: SelectionListItem = { + title: "Select", + description: "", + isSelected: true, + icon: "Search", + }; + return other; +} +function setListSelected(list: SelectionListItem[], item: SelectionListItem) { + for (var i = 0; i < list.length; i++) { + list[i].isSelected = false; + if (list[i] === item) list[i].isSelected = true; + } +}