From 5cc3f8a0fbaf61d0cf701c4ac83e1afea30f6c29 Mon Sep 17 00:00:00 2001 From: Vincent Composieux Date: Sun, 15 Jan 2023 18:41:06 +0100 Subject: [PATCH] Frontend: added ability to set custom resource (for using wildcard) --- Dockerfile.standalone | 2 +- .../component/MultipleAutocompleteInput.tsx | 28 +++++++++++++-- .../src/component/SingleAutocompleteInput.tsx | 34 +++++++++++++++++-- frontend/src/page/check/component/Check.tsx | 1 + .../policies/component/PolicyCreateOrEdit.tsx | 1 + 5 files changed, 60 insertions(+), 6 deletions(-) diff --git a/Dockerfile.standalone b/Dockerfile.standalone index bbe3295b..cfef97e5 100644 --- a/Dockerfile.standalone +++ b/Dockerfile.standalone @@ -37,7 +37,7 @@ FROM nginx:1.23-alpine ARG GRPC_SERVER_ADDR=:8081 ARG HTTP_SERVER_ADDR=:8080 -ARG NGINX_VHOST=./frontend/.docker/vhost.demo.conf +ARG NGINX_VHOST=./frontend/.docker/vhost.conf ENV GRPC_SERVER_ADDR=$GRPC_SERVER_ADDR ENV HTTP_SERVER_ADDR=$HTTP_SERVER_ADDR diff --git a/frontend/src/component/MultipleAutocompleteInput.tsx b/frontend/src/component/MultipleAutocompleteInput.tsx index 1b2fba73..393fc3b5 100644 --- a/frontend/src/component/MultipleAutocompleteInput.tsx +++ b/frontend/src/component/MultipleAutocompleteInput.tsx @@ -1,10 +1,11 @@ import * as React from 'react'; -import { InputAdornment, SxProps } from '@mui/material'; +import { FilterOptionsState, InputAdornment, SxProps } from '@mui/material'; import { useTheme } from '@mui/material/styles'; import CloseIcon from '@mui/icons-material/Close'; import SearchIcon from '@mui/icons-material/Search'; import Autocomplete, { AutocompleteCloseReason, + createFilterOptions, } from '@mui/material/Autocomplete'; import Box from '@mui/material/Box'; import { Button, List, ListItem, ListItemText, TextField } from '@mui/material'; @@ -22,6 +23,7 @@ type SetValueFunc = (items: ItemType[]) => void; type AutocompleteInputProps = { disabled?: boolean + allowAdd?: boolean defaultValues?: ItemType[] errorField?: any fetcher: FetcherFunc @@ -34,6 +36,7 @@ type AutocompleteInputProps = { export default function MultipleAutocompleteInput({ disabled, + allowAdd, defaultValues, errorField, fetcher, @@ -89,16 +92,37 @@ export default function MultipleAutocompleteInput({ setItems(defaultValues); }, [defaultValues]); + const filter = createFilterOptions(); + + const filterOptions = (options: ItemType[], params: FilterOptionsState) => { + const filtered = filter(options, params); + + const { inputValue } = params; + const isExisting = options.some((option) => inputValue === option.id); + if (inputValue !== '' && !isExisting) { + filtered.push({ + id: inputValue, + label: inputValue, + }); + } + + return filtered; + }; + return (
{ return option.id === value.id; }} @@ -124,7 +148,7 @@ export default function MultipleAutocompleteInput({ )} options={[...listItems]} - getOptionLabel={(option) => option.label} + getOptionLabel={(option) => typeof(option) === 'string' ? option : option.label} onFocus={handleOnKeyUp} onKeyUp={handleOnKeyUp} renderInput={(params) => ( diff --git a/frontend/src/component/SingleAutocompleteInput.tsx b/frontend/src/component/SingleAutocompleteInput.tsx index 2b1f96df..ab209057 100644 --- a/frontend/src/component/SingleAutocompleteInput.tsx +++ b/frontend/src/component/SingleAutocompleteInput.tsx @@ -1,9 +1,10 @@ import * as React from 'react'; -import { InputAdornment, SxProps } from '@mui/material'; +import { InputAdornment, SxProps, FilterOptionsState } from '@mui/material'; import { useTheme } from '@mui/material/styles'; import SearchIcon from '@mui/icons-material/Search'; import Autocomplete, { AutocompleteCloseReason, + createFilterOptions, } from '@mui/material/Autocomplete'; import Box from '@mui/material/Box'; import { TextField } from '@mui/material'; @@ -14,6 +15,7 @@ type SetValueFunc = (items: ItemType) => void; type AutocompleteInputProps = { disabled?: boolean + allowAdd?: boolean defaultValue?: ItemType errorField?: any fetcher: FetcherFunc @@ -26,6 +28,7 @@ type AutocompleteInputProps = { export default function SingleAutocompleteInput({ disabled, + allowAdd, defaultValue, errorField, fetcher, @@ -57,18 +60,39 @@ export default function SingleAutocompleteInput({ } }; - const handleOnChange = (event: React.SyntheticEvent, newItem: ItemType | null, reason: string) => { + const handleOnChange = (event: React.SyntheticEvent, newItem: NonNullable, reason: string) => { if (event.type === 'keydown' && (event as React.KeyboardEvent).key === 'Backspace' && reason === 'removeOption') { return; } + if (typeof(newItem) === 'string') { + return; + } + if (newItem !== null) { setValue(newItem); } }; + const filter = createFilterOptions(); + + const filterOptions = (options: ItemType[], params: FilterOptionsState) => { + const filtered = filter(options, params); + + const { inputValue } = params; + const isExisting = options.some((option) => inputValue === option.id); + if (inputValue !== '' && !isExisting) { + filtered.push({ + id: inputValue, + label: inputValue, + }); + } + + return filtered; + }; + React.useEffect(() => { if (defaultValue === undefined) { return; @@ -82,10 +106,14 @@ export default function SingleAutocompleteInput({ { return option.id === value.id; }} @@ -106,7 +134,7 @@ export default function SingleAutocompleteInput({ )} options={[...listItems]} - getOptionLabel={(option) => option.label} + getOptionLabel={(option) => typeof(option) === 'string' ? option : option.label} onFocus={handleOnKeyUp} onKeyUp={handleOnKeyUp} renderInput={(params) => ( diff --git a/frontend/src/page/check/component/Check.tsx b/frontend/src/page/check/component/Check.tsx index ea3eb505..d34c1a23 100644 --- a/frontend/src/page/check/component/Check.tsx +++ b/frontend/src/page/check/component/Check.tsx @@ -128,6 +128,7 @@ export default function Check() {