-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[RHOAIENG-11530] Add file field advanced settings for connection type…
… fields
- Loading branch information
1 parent
4a0626e
commit 62f95cf
Showing
10 changed files
with
414 additions
and
27 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export const EXTENSION_REGEX = /^\.[a-zA-Z0-9]+(^.[a-zA-Z0-9]+)?$/; | ||
|
||
export const isDuplicateExtension = (index: number, values: string[]): boolean => | ||
index !== 0 && | ||
!!values.slice(0, index).find((val) => values[index].toLowerCase() === val.toLowerCase()); |
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
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
92 changes: 92 additions & 0 deletions
92
frontend/src/pages/connectionTypes/manage/advanced/FileUploadAdvancedPropertiesForm.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 |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import * as React from 'react'; | ||
import { Button, FormGroup } from '@patternfly/react-core'; | ||
import { PlusCircleIcon } from '@patternfly/react-icons'; | ||
import { FileField } from '~/concepts/connectionTypes/types'; | ||
import { | ||
EXTENSION_REGEX, | ||
isDuplicateExtension, | ||
} from '~/concepts/connectionTypes/fields/fieldUtils'; | ||
import { AdvancedFieldProps } from '~/pages/connectionTypes/manage/advanced/types'; | ||
import ExpandableFormSection from '~/components/ExpandableFormSection'; | ||
import FileUploadExtensionRow from '~/pages/connectionTypes/manage/advanced/FileUploadExtensionRow'; | ||
|
||
const FileUploadAdvancedPropertiesForm: React.FC<AdvancedFieldProps<FileField>> = ({ | ||
properties, | ||
onChange, | ||
onValidate, | ||
}) => { | ||
const displayedExtensions = React.useMemo( | ||
() => [...(properties.extensions?.length ? [...properties.extensions] : [''])], | ||
[properties.extensions], | ||
); | ||
const lastTextRef = React.useRef<HTMLInputElement | null>(null); | ||
|
||
React.useEffect(() => { | ||
if (!properties.extensions?.length) { | ||
onValidate(true); | ||
return; | ||
} | ||
const valid = properties.extensions.every( | ||
(extension, i) => | ||
!extension || | ||
(EXTENSION_REGEX.test(extension) && !isDuplicateExtension(i, properties.extensions || [])), | ||
); | ||
onValidate(valid); | ||
// do not run when callback changes | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [properties.extensions]); | ||
|
||
return ( | ||
<ExpandableFormSection | ||
toggleText="Advanced settings" | ||
initExpanded={!!properties.extensions} | ||
data-testid="advanced-settings-toggle" | ||
> | ||
<FormGroup label="Allow specific files" fieldId="file-types" isStack> | ||
{displayedExtensions.map((extension, index) => ( | ||
<FileUploadExtensionRow | ||
key={index} | ||
extension={extension} | ||
isDuplicate={isDuplicateExtension(index, displayedExtensions)} | ||
textRef={index === displayedExtensions.length - 1 ? lastTextRef : undefined} | ||
onChange={(val) => { | ||
onChange({ | ||
...properties, | ||
extensions: [ | ||
...displayedExtensions.slice(0, index), | ||
val, | ||
...displayedExtensions.slice(index + 1), | ||
], | ||
}); | ||
}} | ||
onRemove={() => | ||
onChange({ | ||
...properties, | ||
extensions: [ | ||
...displayedExtensions.slice(0, index), | ||
...displayedExtensions.slice(index + 1), | ||
], | ||
}) | ||
} | ||
allowRemove={displayedExtensions.length > 1 || !!displayedExtensions[0]} | ||
/> | ||
))} | ||
<Button | ||
variant="link" | ||
data-testid="add-variable-button" | ||
isInline | ||
icon={<PlusCircleIcon />} | ||
iconPosition="left" | ||
onClick={() => { | ||
onChange({ ...properties, extensions: [...displayedExtensions, ''] }); | ||
requestAnimationFrame(() => lastTextRef.current?.focus()); | ||
}} | ||
> | ||
Add file type | ||
</Button> | ||
</FormGroup> | ||
</ExpandableFormSection> | ||
); | ||
}; | ||
|
||
export default FileUploadAdvancedPropertiesForm; |
94 changes: 94 additions & 0 deletions
94
frontend/src/pages/connectionTypes/manage/advanced/FileUploadExtensionRow.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 |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import * as React from 'react'; | ||
import { | ||
Button, | ||
FormHelperText, | ||
HelperText, | ||
HelperTextItem, | ||
InputGroup, | ||
InputGroupItem, | ||
TextInput, | ||
} from '@patternfly/react-core'; | ||
import { ExclamationCircleIcon, MinusCircleIcon } from '@patternfly/react-icons'; | ||
import { EXTENSION_REGEX } from '~/concepts/connectionTypes/fields/fieldUtils'; | ||
|
||
type FileUploadExtensionRowProps = { | ||
extension?: string; | ||
isDuplicate?: boolean; | ||
onChange: (newValue: string) => void; | ||
onRemove?: () => void; | ||
allowRemove: boolean; | ||
textRef?: React.RefObject<HTMLInputElement>; | ||
}; | ||
|
||
const FileUploadExtensionRow: React.FC<FileUploadExtensionRowProps> = ({ | ||
extension, | ||
isDuplicate, | ||
onRemove, | ||
allowRemove, | ||
onChange, | ||
textRef, | ||
}) => { | ||
const [isValid, setIsValid] = React.useState<boolean>(!isDuplicate); | ||
|
||
React.useEffect( | ||
() => setIsValid(extension ? !isDuplicate && EXTENSION_REGEX.test(extension) : true), | ||
// only run on entry or if a duplicate, otherwise wait for blur | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
[isDuplicate], | ||
); | ||
|
||
return ( | ||
<> | ||
<InputGroup data-testid="file-upload-extension-row"> | ||
<InputGroupItem isFill> | ||
<TextInput | ||
name="file-extension" | ||
data-testid="file-upload-extension-row-input" | ||
type="text" | ||
aria-label="allowed extension" | ||
ref={textRef} | ||
value={extension || ''} | ||
placeholder={extension || 'Example: .json'} | ||
onChange={(_ev, val) => { | ||
if (!isValid && !isDuplicate && EXTENSION_REGEX.test(val)) { | ||
setIsValid(true); | ||
} | ||
onChange(val); | ||
}} | ||
onBlur={() => { | ||
setIsValid(extension ? !isDuplicate && EXTENSION_REGEX.test(extension) : true); | ||
}} | ||
/> | ||
</InputGroupItem> | ||
<InputGroupItem isPlain> | ||
<Button | ||
variant="plain" | ||
data-testid="file-upload-extension-row-remove" | ||
aria-label="remove extension" | ||
isDisabled={!allowRemove} | ||
onClick={onRemove} | ||
> | ||
<MinusCircleIcon /> | ||
</Button> | ||
</InputGroupItem> | ||
</InputGroup> | ||
{!isValid ? ( | ||
<FormHelperText> | ||
<HelperText> | ||
<HelperTextItem | ||
icon={<ExclamationCircleIcon />} | ||
variant="error" | ||
data-testid="file-upload-extension-row-error" | ||
> | ||
{isDuplicate | ||
? 'Extension has already been specified.' | ||
: `Please enter a valid extension starting with '.'`} | ||
</HelperTextItem> | ||
</HelperText> | ||
</FormHelperText> | ||
) : null} | ||
</> | ||
); | ||
}; | ||
|
||
export default FileUploadExtensionRow; |
Oops, something went wrong.