Skip to content

Commit

Permalink
Modify MultiSelection to include creatable options
Browse files Browse the repository at this point in the history
  • Loading branch information
ashley-o0o committed Aug 28, 2024
1 parent c6d0fd5 commit 7a1c960
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 343 deletions.
8 changes: 8 additions & 0 deletions frontend/src/__mocks__/mockConnectionType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
ConnectionTypeField,
} from '~/concepts/connectionTypes/types';
import { toConnectionTypeConfigMap } from '~/concepts/connectionTypes/utils';
import { GroupsConfig } from '~/pages/groupSettings/groupTypes';

Check failure on line 7 in frontend/src/__mocks__/mockConnectionType.ts

View workflow job for this annotation

GitHub Actions / Tests (18.x)

'GroupsConfig' is defined but never used

type MockConnectionTypeConfigMap = {
name?: string;
Expand Down Expand Up @@ -542,3 +543,10 @@ const mockFields: ConnectionTypeField[] = [
},
},
];

export const categoryOptions = [
{ id: 'object-storage', name: 'Object storage', selected: false },
{ id: 'database', name: 'Database', selected: false },
{ id: 'model-registry', name: 'Model registry', selected: false },
{ id: 'uri', name: 'URI', selected: false },
];
29 changes: 26 additions & 3 deletions frontend/src/components/MultiSelection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ type MultiSelectionProps = {
selectionRequired?: boolean;
noSelectedOptionsMessage?: string;
toggleTestId?: string;
isCreatable?: boolean;
isCreateOptionOnTop?: boolean;
createOptionMessage?: string;
};

export const MultiSelection: React.FC<MultiSelectionProps> = ({
Expand All @@ -56,6 +59,9 @@ export const MultiSelection: React.FC<MultiSelectionProps> = ({
toggleTestId,
selectionRequired,
noSelectedOptionsMessage = 'One or more options must be selected',
isCreatable = false,
isCreateOptionOnTop = false,

Check failure on line 63 in frontend/src/components/MultiSelection.tsx

View workflow job for this annotation

GitHub Actions / Tests (18.x)

'isCreateOptionOnTop' is assigned a value but never used
createOptionMessage = 'Create "{value}"',
}) => {
const [isOpen, setIsOpen] = React.useState(false);
const [inputValue, setInputValue] = React.useState<string>('');
Expand Down Expand Up @@ -84,6 +90,7 @@ export const MultiSelection: React.FC<MultiSelectionProps> = ({
setInputValue('');
}
};

const groupOptions = selectGroups.reduce<SelectionOptions[]>((acc, g) => {
acc.push(...g.values);
return acc;
Expand Down Expand Up @@ -156,9 +163,8 @@ export const MultiSelection: React.FC<MultiSelectionProps> = ({
case 'Enter':
if (isOpen && focusedItem) {
onSelect(focusedItem);
}
if (!isOpen) {
setIsOpen(true);
} else if (isCreatable && inputValue.trim()) {
createOption(inputValue);
}
break;
case 'Tab':
Expand All @@ -178,9 +184,11 @@ export const MultiSelection: React.FC<MultiSelectionProps> = ({
setOpen(!isOpen);
setTimeout(() => textInputRef.current?.focus(), 100);
};

const onTextInputChange = (_event: React.FormEvent<HTMLInputElement>, valueOfInput: string) => {
setInputValue(valueOfInput);
};

const onSelect = (menuItem?: SelectionOptions) => {
if (menuItem) {
setValue(
Expand All @@ -192,6 +200,12 @@ export const MultiSelection: React.FC<MultiSelectionProps> = ({
textInputRef.current?.focus();
};

const createOption = (newOption: string) => {
const newSelection = { id: newOption, name: newOption, selected: true };
setValue([...allOptions, newSelection]);
setInputValue('');
};

const noSelectedItems = allOptions.filter((option) => option.selected).length === 0;

const toggle = (toggleRef: React.Ref<MenuToggleElement>) => (
Expand Down Expand Up @@ -267,6 +281,15 @@ export const MultiSelection: React.FC<MultiSelectionProps> = ({
onOpenChange={() => setOpen(false)}
toggle={toggle}
>
{isCreatable && inputValue && !allOptions.some(option => option.name.toLowerCase() === inputValue.toLowerCase()) && (

Check failure on line 284 in frontend/src/components/MultiSelection.tsx

View workflow job for this annotation

GitHub Actions / Tests (18.x)

Replace `·inputValue·&&·!allOptions.some(option` with `⏎··········inputValue·&&⏎··········!allOptions.some((option)`
<SelectOption

Check failure on line 285 in frontend/src/components/MultiSelection.tsx

View workflow job for this annotation

GitHub Actions / Tests (18.x)

Insert `··`
key="create-option"

Check failure on line 286 in frontend/src/components/MultiSelection.tsx

View workflow job for this annotation

GitHub Actions / Tests (18.x)

Insert `··`
value={inputValue}

Check failure on line 287 in frontend/src/components/MultiSelection.tsx

View workflow job for this annotation

GitHub Actions / Tests (18.x)

Insert `··`
onClick={() => createOption(inputValue)}

Check failure on line 288 in frontend/src/components/MultiSelection.tsx

View workflow job for this annotation

GitHub Actions / Tests (18.x)

Replace `············` with `··············`
>

Check failure on line 289 in frontend/src/components/MultiSelection.tsx

View workflow job for this annotation

GitHub Actions / Tests (18.x)

Insert `··`
{createOptionMessage.replace('{value}', inputValue)}

Check failure on line 290 in frontend/src/components/MultiSelection.tsx

View workflow job for this annotation

GitHub Actions / Tests (18.x)

Insert `··`
</SelectOption>

Check failure on line 291 in frontend/src/components/MultiSelection.tsx

View workflow job for this annotation

GitHub Actions / Tests (18.x)

Insert `··`
)}
{visibleOptions.length === 0 && inputValue ? (
<SelectList isAriaMultiselectable>
<SelectOption isDisabled>No results found</SelectOption>
Expand Down
Loading

0 comments on commit 7a1c960

Please sign in to comment.