From b56450fe09c67ae47bfb05f5dd4159c0ddf49959 Mon Sep 17 00:00:00 2001 From: Amit Fainholts Date: Sun, 23 Jan 2022 18:24:09 +0200 Subject: [PATCH] custom select list component added --- .../components/SettingsModal/SettingModal.tsx | 54 +----------- ui/src/components/UI/SelectList.tsx | 87 +++++++++++++++++++ 2 files changed, 90 insertions(+), 51 deletions(-) create mode 100644 ui/src/components/UI/SelectList.tsx diff --git a/ui/src/components/SettingsModal/SettingModal.tsx b/ui/src/components/SettingsModal/SettingModal.tsx index 504342a8d..9535bb8fb 100644 --- a/ui/src/components/SettingsModal/SettingModal.tsx +++ b/ui/src/components/SettingsModal/SettingModal.tsx @@ -1,12 +1,12 @@ -import React, {useEffect, useMemo, useState} from "react"; +import React, {useEffect, useState} from "react"; import {Modal, Backdrop, Fade, Box, Button} from "@material-ui/core"; import {modalStyle} from "../Filters"; -import Checkbox from "../UI/Checkbox"; import './SettingsModal.sass'; import Api from "../../helpers/api"; import spinner from "../assets/spinner.svg"; import {useCommonStyles} from "../../helpers/commonStyle"; import {toast} from "react-toastify"; +import SelectList from "../UI/SelectList"; interface SettingsModalProps { isOpen: boolean @@ -47,14 +47,6 @@ export const SettingsModal: React.FC = ({isOpen, onClose, is })() }, [isFirstLogin, isOpen]) - const setAllNamespacesTappedValue = (isTap: boolean) => { - const newNamespaces = {}; - Object.keys(namespaces).forEach(key => { - newNamespaces[key] = isTap; - }) - setNamespaces(newNamespaces); - } - const updateTappingSettings = async () => { try { await api.setTapConfig(namespaces); @@ -66,44 +58,6 @@ export const SettingsModal: React.FC = ({isOpen, onClose, is } } - const toggleTapNamespace = (namespace) => { - const newNamespaces = {...namespaces}; - newNamespaces[namespace] = !namespaces[namespace] - setNamespaces(newNamespaces); - } - - const toggleAll = () => { - const isChecked = Object.values(namespaces).every(tap => tap === true); - setAllNamespacesTappedValue(!isChecked); - } - - const filteredNamespaces = useMemo(() => { - return Object.keys(namespaces).filter((namespace) => namespace.includes(searchValue)); - },[namespaces, searchValue]) - - const buildNamespacesTable = () => { - return - - - - - - - - {filteredNamespaces?.map(namespace => { - return - - - - } - )} - -
tap === true)} - onToggle={toggleAll}/>Namespace
- toggleTapNamespace(namespace)}/> - {namespace}
- } - const onModalClose = (reason) => { if(reason === 'backdropClick' && isFirstLogin) return; onClose(); @@ -133,9 +87,7 @@ export const SettingsModal: React.FC = ({isOpen, onClose, is
setSearchValue(event.target.value)}/>
-
- {buildNamespacesTable()} -
+ } diff --git a/ui/src/components/UI/SelectList.tsx b/ui/src/components/UI/SelectList.tsx new file mode 100644 index 000000000..0659de6b2 --- /dev/null +++ b/ui/src/components/UI/SelectList.tsx @@ -0,0 +1,87 @@ +import { useMemo, useState } from "react"; +import Checkbox from "./Checkbox" + +export interface Props { + valuesListInput; + tableName:string; + multiSelect:boolean; + searchValue?:string; + setValues: (newValues)=> void; + tabelClassName +} + +const SelectList: React.FC = ({valuesListInput,tableName,multiSelect=true,searchValue="",setValues,tabelClassName}) => { + const [valuesList, setValuesList] = useState(valuesListInput); + + const toggleValues = (value) => { + if (!multiSelect){ + unToggleAll(value); + } + else { + const newValues = {...valuesList}; + newValues[value] = !valuesList[value]; + setValuesList(newValues); + setValues(newValues); + } + } + + const unToggleAll = (value) => { + const newValues = {}; + Object.keys(valuesList).forEach(key => { + if (key !== value) + newValues[key] = false; + else + newValues[key] = true; + }) + setValuesList(newValues); + setValues(newValues); + } + + const toggleAll = () => { + const isChecked = Object.values(valuesList).every(tap => tap === true); + setAllCheckedValue(!isChecked); + } + + const setAllCheckedValue = (isTap: boolean) => { + const newValues = {}; + Object.keys(valuesList).forEach(key => { + newValues[key] = isTap; + }) + setValuesList(newValues); + setValues(newValues); + } + + const tableHead = multiSelect ? + tap === true)} + onToggle={toggleAll}/> + {tableName} + : + + {tableName} + + + const filteredValues = useMemo(() => { + return Object.keys(valuesList).filter((listValue) => listValue.includes(searchValue)); + },[valuesList, searchValue]) + + return
+ + + {tableHead} + + + {filteredValues?.map(listValue => { + return + + + + } + )} + +
+ toggleValues(listValue)}/> + {listValue}
+
+} + +export default SelectList; \ No newline at end of file