diff --git a/app/components/form/fields/ListboxField.tsx b/app/components/form/fields/ListboxField.tsx index ee14434ed..965a92081 100644 --- a/app/components/form/fields/ListboxField.tsx +++ b/app/components/form/fields/ListboxField.tsx @@ -35,8 +35,6 @@ export type ListboxFieldProps< onChange?: (value: string | null | undefined) => void isLoading?: boolean noItemsPlaceholder?: string - isClearable?: boolean - onClear?: () => void } export function ListboxField< @@ -56,8 +54,6 @@ export function ListboxField< onChange, isLoading, noItemsPlaceholder, - isClearable, - onClear, }: ListboxFieldProps) { // TODO: recreate this logic // validate: (v) => (required && !v ? `${name} is required` : undefined), @@ -86,11 +82,6 @@ export function ListboxField< buttonRef={field.ref} /> - {isClearable && ( - - )} ) } diff --git a/app/components/form/fields/useItemsList.ts b/app/components/form/fields/useItemsList.ts index d5ea5f70c..5370b0d27 100644 --- a/app/components/form/fields/useItemsList.ts +++ b/app/components/form/fields/useItemsList.ts @@ -11,16 +11,23 @@ import { useMemo } from 'react' import { useApiQuery } from '~/api' import { useVpcSelector } from '~/hooks' +/** + * Special value indicating no router. Must convert `undefined` to this when + * populating form, and must convert this to `undefined` in onSubmit. + */ +export const NO_ROUTER = '||no router||' + export const useCustomRouterItems = () => { const vpcSelector = useVpcSelector() - const routers = useApiQuery('vpcRouterList', { query: { ...vpcSelector } }) + const { data, isLoading } = useApiQuery('vpcRouterList', { query: { ...vpcSelector } }) + const routerItems = useMemo(() => { - return ( - routers?.data?.items - .filter((item) => item.kind === 'custom') - .map((router) => ({ value: router.id, label: router.name })) || [] - ) - }, [routers]) + const items = (data?.items || []) + .filter((item) => item.kind === 'custom') + .map((router) => ({ value: router.id, label: router.name })) + + return [{ value: NO_ROUTER, label: 'None' }, ...items] + }, [data]) - return { isLoading: routers.isLoading, items: routerItems } + return { isLoading, items: routerItems } } diff --git a/app/forms/subnet-create.tsx b/app/forms/subnet-create.tsx index 6bc5d2933..c58dc6e74 100644 --- a/app/forms/subnet-create.tsx +++ b/app/forms/subnet-create.tsx @@ -13,7 +13,7 @@ import { DescriptionField } from '~/components/form/fields/DescriptionField' import { ListboxField } from '~/components/form/fields/ListboxField' import { NameField } from '~/components/form/fields/NameField' import { TextField } from '~/components/form/fields/TextField' -import { useCustomRouterItems } from '~/components/form/fields/useItemsList' +import { NO_ROUTER, useCustomRouterItems } from '~/components/form/fields/useItemsList' import { SideModalForm } from '~/components/form/SideModalForm' import { useForm, useVpcSelector } from '~/hooks' import { FormDivider } from '~/ui/lib/Divider' @@ -21,7 +21,7 @@ import { pb } from '~/util/path-builder' const defaultValues: VpcSubnetCreate = { name: '', - customRouter: undefined, + customRouter: NO_ROUTER, description: '', ipv4Block: '', } @@ -49,7 +49,15 @@ export function CreateSubnetForm() { formType="create" resourceName="subnet" onDismiss={onDismiss} - onSubmit={(body) => createSubnet.mutate({ query: vpcSelector, body })} + onSubmit={(body) => + createSubnet.mutate({ + query: vpcSelector, + body: { + ...body, + customRouter: body.customRouter === NO_ROUTER ? undefined : body.customRouter, + }, + }) + } loading={createSubnet.isPending} submitError={createSubnet.error} > @@ -63,6 +71,7 @@ export function CreateSubnetForm() { isLoading={isLoading} items={items} control={form.control} + required /> diff --git a/app/forms/subnet-edit.tsx b/app/forms/subnet-edit.tsx index ab6b706a5..2f515cca7 100644 --- a/app/forms/subnet-edit.tsx +++ b/app/forms/subnet-edit.tsx @@ -18,7 +18,7 @@ import { import { DescriptionField } from '~/components/form/fields/DescriptionField' import { ListboxField } from '~/components/form/fields/ListboxField' import { NameField } from '~/components/form/fields/NameField' -import { useCustomRouterItems } from '~/components/form/fields/useItemsList' +import { NO_ROUTER, useCustomRouterItems } from '~/components/form/fields/useItemsList' import { SideModalForm } from '~/components/form/SideModalForm' import { getVpcSubnetSelector, useForm, useVpcSubnetSelector } from '~/hooks' import { FormDivider } from '~/ui/lib/Divider' @@ -55,17 +55,11 @@ export function EditSubnetForm() { const defaultValues: VpcSubnetUpdate = { name: subnet.name, description: subnet.description, - customRouter: subnet.customRouterId, + customRouter: subnet.customRouterId || NO_ROUTER, } const form = useForm({ defaultValues }) const { isLoading, items } = useCustomRouterItems() - const customRouterValue = form.watch('customRouter') - const clearCustomRouter = () => { - // Because this is `undefined`, the dropdown isn't behaving correctly; it should revert to the placeholder - // It works when the value is '', but that's not a valid value for this field and it trips zod's validator - form.setValue('customRouter', undefined) - } return ( )