diff --git a/src/Components/CreateImageWizard/ChippingInput.tsx b/src/Components/CreateImageWizard/ChippingInput.tsx new file mode 100644 index 000000000..86bcfb16c --- /dev/null +++ b/src/Components/CreateImageWizard/ChippingInput.tsx @@ -0,0 +1,118 @@ +import React, { useState } from 'react'; + +import { + Button, + Chip, + ChipGroup, + HelperText, + HelperTextItem, + TextInputGroup, + TextInputGroupMain, + TextInputGroupUtilities, +} from '@patternfly/react-core/dist/esm'; +import { PlusCircleIcon, TimesIcon } from '@patternfly/react-icons'; +import { UnknownAction } from 'redux'; + +import { useAppDispatch } from '../../store/hooks'; + +type ChippingInputProps = { + ariaLabel: string; + placeholder: string; + validator: (value: string) => boolean; + list: string[] | undefined; + item: string; + addAction: (value: string) => UnknownAction; + removeAction: (value: string) => UnknownAction; +}; + +const ChippingInput = ({ + ariaLabel, + placeholder, + validator, + list, + item, + addAction, + removeAction, +}: ChippingInputProps) => { + const dispatch = useAppDispatch(); + + const [inputValue, setInputValue] = useState(''); + const [errorText, setErrorText] = useState(''); + + const onTextInputChange = ( + _event: React.FormEvent, + value: string + ) => { + setInputValue(value); + }; + + const handleKeyDown = (e: React.KeyboardEvent, value: string) => { + if (e.key === ' ' || e.key === 'Enter' || e.key === ',') { + e.preventDefault(); + + if (validator(value) && !list?.includes(value)) { + dispatch(addAction(value)); + setInputValue(''); + setErrorText(''); + } + + if (list?.includes(value)) { + setErrorText(`${item} already exists.`); + } + + if (!validator(value)) { + setErrorText('Invalid format.'); + } + } + }; + + const handleAddItem = (e: React.MouseEvent, value: string) => { + dispatch(addAction(value)); + setInputValue(''); + }; + + return ( + <> + + handleKeyDown(e, inputValue)} + /> + + + + + + {errorText && ( + + {errorText} + + )} + + {list?.map((item) => ( + dispatch(removeAction(item))}> + {item} + + ))} + + + ); +}; + +export default ChippingInput; diff --git a/src/Components/CreateImageWizard/steps/Timezone/components/NtpServersInput.tsx b/src/Components/CreateImageWizard/steps/Timezone/components/NtpServersInput.tsx index f04389579..827df37e8 100644 --- a/src/Components/CreateImageWizard/steps/Timezone/components/NtpServersInput.tsx +++ b/src/Components/CreateImageWizard/steps/Timezone/components/NtpServersInput.tsx @@ -1,104 +1,30 @@ -import React, { useState } from 'react'; +import React from 'react'; -import { - Button, - Chip, - ChipGroup, - FormGroup, - HelperText, - HelperTextItem, - TextInputGroup, - TextInputGroupMain, - TextInputGroupUtilities, -} from '@patternfly/react-core'; -import { PlusCircleIcon, TimesIcon } from '@patternfly/react-icons'; +import { FormGroup } from '@patternfly/react-core'; -import { useAppDispatch, useAppSelector } from '../../../../../store/hooks'; +import { useAppSelector } from '../../../../../store/hooks'; import { addNtpServer, removeNtpServer, selectNtpServers, } from '../../../../../store/wizardSlice'; +import ChippingInput from '../../../ChippingInput'; import { isNtpServerValid } from '../../../validators'; const NtpServersInput = () => { - const dispatch = useAppDispatch(); const ntpServers = useAppSelector(selectNtpServers); - const [inputValue, setInputValue] = useState(''); - const [errorText, setErrorText] = useState(''); - - const onTextInputChange = ( - _event: React.FormEvent, - value: string - ) => { - setInputValue(value); - }; - - const handleKeyDown = (e: React.KeyboardEvent, value: string) => { - if (e.key === ' ' || e.key === 'Enter' || e.key === ',') { - e.preventDefault(); - - if (isNtpServerValid(value) && !ntpServers?.includes(value)) { - dispatch(addNtpServer(value)); - setInputValue(''); - setErrorText(''); - } - - if (ntpServers?.includes(value)) { - setErrorText('NTP server already exists.'); - } - - if (!isNtpServerValid(value)) { - setErrorText('Invalid format.'); - } - } - }; - - const handleAddServer = (e: React.MouseEvent, value: string) => { - dispatch(addNtpServer(value)); - setInputValue(''); - }; return ( - - handleKeyDown(e, inputValue)} - /> - - - - - - {errorText && ( - - {errorText} - - )} - - {ntpServers?.map((server) => ( - dispatch(removeNtpServer(server))}> - {server} - - ))} - + ); };