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

feat(dashboard): Submit forms on Cmd + Enter #9623

Merged
merged 6 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { Form } from "../../common/form"
import { InlineTip } from "../../common/inline-tip"
import { Skeleton } from "../../common/skeleton"
import { RouteDrawer, useRouteModal } from "../../modals"
import { KeyboundForm } from "../../utilities/keybound-form"

type MetaDataSubmitHook<TRes> = (
params: { metadata?: Record<string, any> | null },
Expand Down Expand Up @@ -125,7 +126,7 @@ const InnerForm = <TRes,>({

return (
<RouteDrawer.Form form={form}>
<form
<KeyboundForm
onSubmit={handleSubmit}
className="flex flex-1 flex-col overflow-hidden"
>
Expand Down Expand Up @@ -277,7 +278,7 @@ const InnerForm = <TRes,>({
</Button>
</div>
</RouteDrawer.Footer>
</form>
</KeyboundForm>
</RouteDrawer.Form>
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
} from "react"
import { useTranslation } from "react-i18next"

import { genericForwardRef } from "../../common/generic-forward-ref"
import { genericForwardRef } from "../../utilities/generic-forward-ref"

type ComboboxOption = {
value: string
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./keybound-form"
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import React from "react"

/**
* A form that can only be submitted when using the meta or control key.
*/
export const KeyboundForm = React.forwardRef<
HTMLFormElement,
React.FormHTMLAttributes<HTMLFormElement>
>(({ onSubmit, onKeyDown, ...rest }, ref) => {
const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault()
onSubmit?.(event)
}

const handleKeyDown = (event: React.KeyboardEvent<HTMLFormElement>) => {
if (event.key === "Enter") {
event.preventDefault()

if (event.metaKey || event.ctrlKey) {
handleSubmit(event)
}
}
}

return (
<form
{...rest}
onSubmit={handleSubmit}
onKeyDown={onKeyDown ?? handleKeyDown}
ref={ref}
/>
)
})

KeyboundForm.displayName = "KeyboundForm"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./visually-hidden"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { PropsWithChildren } from "react"

export const VisuallyHidden = ({ children }: PropsWithChildren) => {
return <span className="sr-only">{children}</span>
}
6 changes: 6 additions & 0 deletions packages/admin/dashboard/src/i18n/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1877,6 +1877,7 @@
},
"edit": {
"header": "Edit Campaign",
"description": "Edit the details of the campaign.",
"successToast": "Campaign '{{name}}' was successfully updated."
},
"configuration": {
Expand All @@ -1888,6 +1889,8 @@
}
},
"create": {
"title": "Create Campaign",
"description": "Create a promotional campaign.",
"hint": "Create a promotional campaign.",
"header": "Create Campaign",
"successToast": "Campaign '{{name}}' was successfully created."
Expand Down Expand Up @@ -2301,9 +2304,12 @@
},
"edit": {
"header": "Edit API Key",
"description": "Edit the API key's title.",
"successToast": "API key {{title}} was successfully updated."
},
"salesChannels": {
"title": "Add Sales Channels",
"description": "Add the sales channels that the API key should be limited to.",
"successToast_one": "{{count}} sales channel was successfully added to the API key.",
"successToast_other": "{{count}} sales channels were successfully added to the API key.",
"alreadyAddedTooltip": "The sales channel has already been added to the API key.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
RouteFocusModal,
useRouteModal,
} from "../../../../../components/modals"
import { KeyboundForm } from "../../../../../components/utilities/keybound-form"
import { useCreateApiKey } from "../../../../../hooks/api/api-keys"
import { ApiKeyType } from "../../../common/constants"

Expand Down Expand Up @@ -97,36 +98,29 @@ export const ApiKeyCreateForm = ({ keyType }: ApiKeyCreateFormProps) => {
return (
<Fragment>
<RouteFocusModal.Form form={form}>
<form
<KeyboundForm
className="flex h-full flex-col overflow-hidden"
onSubmit={handleSubmit}
>
<RouteFocusModal.Header>
<div className="flex items-center justify-end gap-x-2">
<RouteFocusModal.Close asChild>
<Button size="small" variant="secondary">
{t("actions.cancel")}
</Button>
</RouteFocusModal.Close>
<Button size="small" type="submit" isLoading={isPending}>
{t("actions.save")}
</Button>
</div>
</RouteFocusModal.Header>
<RouteFocusModal.Header />
<RouteFocusModal.Body className="flex flex-1 flex-col overflow-hidden">
<div className="flex flex-1 flex-col items-center overflow-y-auto">
<div className="flex w-full max-w-[720px] flex-col gap-y-8 px-2 py-16">
<div>
<Heading>
{keyType === ApiKeyType.PUBLISHABLE
? t("apiKeyManagement.create.createPublishableHeader")
: t("apiKeyManagement.create.createSecretHeader")}
</Heading>
<Text size="small" className="text-ui-fg-subtle">
{keyType === ApiKeyType.PUBLISHABLE
? t("apiKeyManagement.create.createPublishableHint")
: t("apiKeyManagement.create.createSecretHint")}
</Text>
<RouteFocusModal.Title asChild>
<Heading>
{keyType === ApiKeyType.PUBLISHABLE
? t("apiKeyManagement.create.createPublishableHeader")
: t("apiKeyManagement.create.createSecretHeader")}
</Heading>
</RouteFocusModal.Title>
<RouteFocusModal.Description asChild>
<Text size="small" className="text-ui-fg-subtle">
{keyType === ApiKeyType.PUBLISHABLE
? t("apiKeyManagement.create.createPublishableHint")
: t("apiKeyManagement.create.createSecretHint")}
</Text>
</RouteFocusModal.Description>
</div>
<div className="grid grid-cols-2 gap-4">
<Form.Field
Expand All @@ -148,7 +142,19 @@ export const ApiKeyCreateForm = ({ keyType }: ApiKeyCreateFormProps) => {
</div>
</div>
</RouteFocusModal.Body>
</form>
<RouteFocusModal.Footer>
<div className="flex items-center justify-end gap-x-2">
<RouteFocusModal.Close asChild>
<Button size="small" variant="secondary">
{t("actions.cancel")}
</Button>
</RouteFocusModal.Close>
<Button size="small" type="submit" isLoading={isPending}>
{t("actions.save")}
</Button>
</div>
</RouteFocusModal.Footer>
</KeyboundForm>
</RouteFocusModal.Form>
<Prompt variant="confirmation" open={!!createdKey}>
<Prompt.Content className="w-fit max-w-[42.5%]">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Heading } from "@medusajs/ui"
import { useTranslation } from "react-i18next"
import { useParams } from "react-router-dom"
import { RouteDrawer } from "../../../components/modals"
import { VisuallyHidden } from "../../../components/utilities/visually-hidden"
import { useApiKey } from "../../../hooks/api/api-keys"
import { EditApiKeyForm } from "./components/edit-api-key-form"

Expand All @@ -18,7 +19,14 @@ export const ApiKeyManagementEdit = () => {
return (
<RouteDrawer>
<RouteDrawer.Header>
<Heading>{t("apiKeyManagement.edit.header")}</Heading>
<RouteDrawer.Title asChild>
<Heading>{t("apiKeyManagement.edit.header")}</Heading>
</RouteDrawer.Title>
<RouteDrawer.Description asChild>
<VisuallyHidden>
{t("apiKeyManagement.edit.description")}
</VisuallyHidden>
</RouteDrawer.Description>
</RouteDrawer.Header>
{!isLoading && !!api_key && <EditApiKeyForm apiKey={api_key} />}
</RouteDrawer>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as zod from "zod"
import { ApiKeyDTO } from "@medusajs/types"
import { Form } from "../../../../../components/common/form"
import { RouteDrawer, useRouteModal } from "../../../../../components/modals"
import { KeyboundForm } from "../../../../../components/utilities/keybound-form"
import { useUpdateApiKey } from "../../../../../hooks/api/api-keys"

type EditApiKeyFormProps = {
Expand Down Expand Up @@ -48,7 +49,7 @@ export const EditApiKeyForm = ({ apiKey }: EditApiKeyFormProps) => {

return (
<RouteDrawer.Form form={form}>
<form onSubmit={handleSubmit} className="flex flex-1 flex-col">
<KeyboundForm onSubmit={handleSubmit} className="flex flex-1 flex-col">
<RouteDrawer.Body>
<div className="flex flex-col gap-y-4">
<Form.Field
Expand Down Expand Up @@ -80,7 +81,7 @@ export const EditApiKeyForm = ({ apiKey }: EditApiKeyFormProps) => {
</Button>
</div>
</RouteDrawer.Footer>
</form>
</KeyboundForm>
</RouteDrawer.Form>
)
}
Loading
Loading