Skip to content

Commit

Permalink
fix multi-select that allows custom values bug
Browse files Browse the repository at this point in the history
  • Loading branch information
brahimmouhamou committed Apr 11, 2024
1 parent e2a45e5 commit f59bed3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
27 changes: 26 additions & 1 deletion src/components/SearchSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,31 @@ const SearchSelect = ({
session: { theme: vaultTheme },
} = useSession();

const findMatchingOption = (val: string) =>
options.find((option) => option.value === val);

const createNewOptionType = (val: string): OptionType => ({
label: val,
value: val,
});

const findOrCreateOption = (val: string): OptionType =>
findMatchingOption(val) ?? createNewOptionType(val);

// If the value is not in the options list, add it as a custom option
// It handles both single and multi-select cases
const addCustomOptionIfNeeded = (
updatedValues: OptionType | OptionType[] | undefined,
value: string | string[] | readonly string[] | undefined
): OptionType | OptionType[] | undefined => {
if (!value || !updatedValues || !Array.isArray(updatedValues))
return updatedValues;

return typeof value === 'string'
? findOrCreateOption(value)
: value.map((v) => findOrCreateOption(v));
};

useEffect(() => {
if (!value) return;

Expand All @@ -71,7 +96,7 @@ const SearchSelect = ({

if (isCreatable) {
option = rest.isMulti
? ([{ label: value, value }] as OptionType[])
? addCustomOptionIfNeeded(option, value)
: ({ label: value, value } as OptionType);
}

Expand Down
4 changes: 2 additions & 2 deletions stories/Vault.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ const Template: Story<Props> = (args) => (
trigger={<button className="p-2 border rounded shadow">Open modal</button>}
{...args}
showConsumer={true}
// unifyBaseUrl="https://localhost:3050"
token="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyZWRpcmVjdF91cmkiOiJodHRwczovL3ZhdWx0LmFwaWRlY2suY29tL29hdXRoL2NhbGxiYWNrIiwiY29uc3VtZXJfbWV0YWRhdGEiOnsiYWNjb3VudF9uYW1lIjoiQXBpZGVjayBTYW5kYm94ICIsInVzZXJfbmFtZSI6Ikpha2UiLCJ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