Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make pressing enter submit instance resize modal form #2586

Merged
merged 3 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions app/pages/project/instances/instance/InstancePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Copyright Oxide Computer Company
*/
import { filesize } from 'filesize'
import { useMemo, useState } from 'react'
import { useId, useMemo, useState } from 'react'
import { useForm } from 'react-hook-form'
import { Link, useNavigate, type LoaderFunctionArgs } from 'react-router-dom'

Expand Down Expand Up @@ -314,13 +314,14 @@ export function ResizeInstanceModal({
form.watch('ncpus') !== instance.ncpus || form.watch('memory') !== instance.memory / GiB
const isDisabled = !form.formState.isValid || !canResize || !willChange

const onAction = form.handleSubmit(({ ncpus, memory }) => {
const onSubmit = form.handleSubmit(({ ncpus, memory }) => {
instanceUpdate.mutate({
path: { instance: instance.name },
query: { project },
body: { ncpus, memory: memory * GiB, bootDisk: instance.bootDiskId },
})
})
const formId = useId()

return (
<Modal title="Resize instance" isOpen onDismiss={onDismiss}>
Expand All @@ -340,7 +341,7 @@ export function ResizeInstanceModal({
}
/>
)}
<form autoComplete="off" className="space-y-4">
<form id={formId} autoComplete="off" className="space-y-4" onSubmit={onSubmit}>
<NumberField
required
label="vCPUs"
Expand Down Expand Up @@ -383,8 +384,8 @@ export function ResizeInstanceModal({
</Modal.Section>
</Modal.Body>
<Modal.Footer
formId={formId}
onDismiss={onDismiss}
onAction={onAction}
actionText="Resize"
actionLoading={instanceUpdate.isPending}
disabled={isDisabled}
Expand Down
30 changes: 20 additions & 10 deletions app/ui/lib/Modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import * as Dialog from '@radix-ui/react-dialog'
import { animated, useTransition } from '@react-spring/web'
import cn from 'classnames'
import React, { forwardRef, useId } from 'react'
import type { MergeExclusive } from 'type-fest'

import { Close12Icon } from '@oxide/design-system/icons/react'

Expand Down Expand Up @@ -125,6 +126,21 @@ Modal.Body = classed.div`py-2 overflow-y-auto`

Modal.Section = classed.div`p-4 space-y-4 border-b border-secondary text-secondary last-of-type:border-none text-sans-md`

/**
* `formId` and `onAction` are mutually exclusive. If there is a form associated,
* the button becomes a submit button for that form, and the action is assumed to
* be hooked up in the form's `onSubmit`.
*/
type FooterProps = {
children?: React.ReactNode
onDismiss: () => void
actionType?: 'primary' | 'danger'
actionText: React.ReactNode
actionLoading?: boolean
cancelText?: string
disabled?: boolean
} & MergeExclusive<{ formId: string }, { onAction: () => void }>

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yoooo MergeExclusive is my new best friend

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤯

Modal.Footer = ({
children,
onDismiss,
Expand All @@ -134,23 +150,17 @@ Modal.Footer = ({
actionLoading,
cancelText,
disabled = false,
}: {
children?: React.ReactNode
onDismiss: () => void
onAction: () => void
actionType?: 'primary' | 'danger'
actionText: React.ReactNode
actionLoading?: boolean
cancelText?: string
disabled?: boolean
}) => (
formId,
}: FooterProps) => (
<footer className="flex items-center justify-between border-t px-3 py-3 border-secondary">
<div className="mr-4">{children}</div>
<div className="space-x-2">
<Button variant="secondary" size="sm" onClick={onDismiss}>
{cancelText || 'Cancel'}
</Button>
<Button
type={formId ? 'submit' : 'button'}
form={formId}
david-crespo marked this conversation as resolved.
Show resolved Hide resolved
size="sm"
variant={actionType}
onClick={onAction}
Expand Down
Loading