-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds the kernel append input. New arguments can be added by pressing the "Add" button or hitting Enter after the argument. The kernel arguments linked to a selected OpenSCAP profile are rendered in a category marked as "Required by OpenSCAP" and are read only.
- Loading branch information
Showing
2 changed files
with
135 additions
and
3 deletions.
There are no files selected for viewing
130 changes: 127 additions & 3 deletions
130
src/Components/CreateImageWizard/steps/Kernel/components/KernelArguments.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,133 @@ | ||
import React from 'react'; | ||
import React, { useState } from 'react'; | ||
|
||
import { FormGroup } from '@patternfly/react-core'; | ||
import { | ||
Button, | ||
Chip, | ||
ChipGroup, | ||
FormGroup, | ||
HelperText, | ||
HelperTextItem, | ||
TextInputGroup, | ||
TextInputGroupMain, | ||
TextInputGroupUtilities, | ||
} from '@patternfly/react-core'; | ||
import { PlusCircleIcon, TimesIcon } from '@patternfly/react-icons'; | ||
|
||
import { useAppDispatch, useAppSelector } from '../../../../../store/hooks'; | ||
import { | ||
addKernelArg, | ||
removeKernelArg, | ||
selectKernel, | ||
} from '../../../../../store/wizardSlice'; | ||
import { isKernelArgumentValid } from '../../../validators'; | ||
|
||
const KernelArguments = () => { | ||
return <FormGroup isRequired={false} label="Append"></FormGroup>; | ||
const dispatch = useAppDispatch(); | ||
const kernelAppend = useAppSelector(selectKernel).append; | ||
|
||
const [inputValue, setInputValue] = useState(''); | ||
const [errorText, setErrorText] = useState(''); | ||
|
||
const onTextInputChange = ( | ||
_event: React.FormEvent<HTMLInputElement>, | ||
value: string | ||
) => { | ||
setInputValue(value); | ||
}; | ||
|
||
const handleKeyDown = (e: React.KeyboardEvent, value: string) => { | ||
if (e.key === 'Enter') { | ||
e.preventDefault(); | ||
|
||
if ( | ||
isKernelArgumentValid(value) && | ||
!kernelAppend.some((arg) => arg.name === value) | ||
) { | ||
dispatch(addKernelArg({ name: value, isRequiredByOpenSCAP: false })); | ||
setInputValue(''); | ||
setErrorText(''); | ||
} | ||
|
||
if (kernelAppend.some((arg) => arg.name === value)) { | ||
setErrorText(`Kernel argument already exists.`); | ||
} | ||
|
||
if (!isKernelArgumentValid(value)) { | ||
setErrorText('Invalid format.'); | ||
} | ||
} | ||
}; | ||
|
||
const handleAddItem = (e: React.MouseEvent, value: string) => { | ||
dispatch(addKernelArg({ name: value, isRequiredByOpenSCAP: false })); | ||
setInputValue(''); | ||
}; | ||
return ( | ||
<FormGroup isRequired={false} label="Append"> | ||
<TextInputGroup> | ||
<TextInputGroupMain | ||
placeholder="Add kernel argument" | ||
onChange={onTextInputChange} | ||
value={inputValue} | ||
onKeyDown={(e) => handleKeyDown(e, inputValue)} | ||
/> | ||
<TextInputGroupUtilities> | ||
<Button | ||
variant="plain" | ||
onClick={(e) => handleAddItem(e, inputValue)} | ||
isDisabled={!inputValue} | ||
aria-label="Add kernel argument" | ||
> | ||
<PlusCircleIcon className="pf-v5-u-primary-color-100" /> | ||
</Button> | ||
<Button | ||
variant="plain" | ||
onClick={() => setInputValue('')} | ||
isDisabled={!inputValue} | ||
aria-label="Clear input" | ||
> | ||
<TimesIcon /> | ||
</Button> | ||
</TextInputGroupUtilities> | ||
</TextInputGroup> | ||
{errorText && ( | ||
<HelperText> | ||
<HelperTextItem variant={'error'}>{errorText}</HelperTextItem> | ||
</HelperText> | ||
)} | ||
{kernelAppend.some((arg) => arg.isRequiredByOpenSCAP) && ( | ||
<ChipGroup | ||
categoryName="Required by OpenSCAP" | ||
numChips={20} | ||
className="pf-v5-u-mt-sm pf-v5-u-w-100" | ||
> | ||
{kernelAppend | ||
.filter((arg) => arg.isRequiredByOpenSCAP) | ||
.map((arg) => ( | ||
<Chip | ||
key={arg.name} | ||
onClick={() => dispatch(removeKernelArg(arg.name))} | ||
isReadOnly | ||
> | ||
{arg.name} | ||
</Chip> | ||
))} | ||
</ChipGroup> | ||
)} | ||
<ChipGroup numChips={20} className="pf-v5-u-mt-sm pf-v5-u-w-100"> | ||
{kernelAppend | ||
.filter((arg) => !arg.isRequiredByOpenSCAP) | ||
.map((arg) => ( | ||
<Chip | ||
key={arg.name} | ||
onClick={() => dispatch(removeKernelArg(arg.name))} | ||
> | ||
{arg.name} | ||
</Chip> | ||
))} | ||
</ChipGroup> | ||
</FormGroup> | ||
); | ||
}; | ||
|
||
export default KernelArguments; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters