-
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 (#3132)
- Loading branch information
1 parent
fe69f9c
commit 18061ba
Showing
12 changed files
with
440 additions
and
28 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
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?.length} | ||
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; |
Oops, something went wrong.