-
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: properly handle form submits in settings flow
- Loading branch information
1 parent
f2294c1
commit 4bb066e
Showing
19 changed files
with
610 additions
and
346 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,64 @@ | ||
// Copyright © 2024 Ory Corp | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { PropsWithChildren } from "react" | ||
import { useComponents } from "../../context/component" | ||
import { OryForm } from "./form" | ||
import { UiNode } from "@ory/client-fetch" | ||
import { | ||
ComponentPropsWithoutRef, | ||
FormEventHandler, | ||
PropsWithChildren, | ||
} from "react" | ||
import { useFormContext } from "react-hook-form" | ||
import { useComponents } from "../../context/component" | ||
import { OryFormProvider } from "./form-provider" | ||
import { useOryFormSubmit } from "./useOryFormSubmit" | ||
import { useOryFlow } from "../../context" | ||
|
||
export type OryFormSectionProps = PropsWithChildren<{ | ||
nodes?: UiNode[] | ||
}> | ||
type OryFormProps = Omit< | ||
ComponentPropsWithoutRef<"form">, | ||
"action" | "method" | "onSubmit" | ||
> | ||
|
||
export function OryFormSection({ children, nodes }: OryFormSectionProps) { | ||
const { Card } = useComponents() | ||
export type OryFormSectionProps = PropsWithChildren< | ||
OryFormProps & { | ||
nodes?: UiNode[] | ||
} | ||
> | ||
|
||
export type OryCardSettingsSectionProps = PropsWithChildren & { | ||
action: string | ||
method: string | ||
onSubmit: FormEventHandler<HTMLFormElement> | ||
} | ||
|
||
export function OryFormSection({ | ||
children, | ||
nodes, | ||
...rest | ||
}: OryFormSectionProps) { | ||
return ( | ||
<OryFormProvider nodes={nodes}> | ||
<OryForm> | ||
<Card.SettingsSection>{children}</Card.SettingsSection> | ||
</OryForm> | ||
<OryFormSectionInner {...rest}>{children}</OryFormSectionInner> | ||
</OryFormProvider> | ||
) | ||
} | ||
|
||
function OryFormSectionInner({ | ||
children, | ||
...rest | ||
}: PropsWithChildren<OryFormProps>) { | ||
const { Card } = useComponents() | ||
const flowContainer = useOryFlow() | ||
const onSubmit = useOryFormSubmit() | ||
const methods = useFormContext() | ||
|
||
return ( | ||
<Card.SettingsSection | ||
action={flowContainer.flow.ui.action} | ||
method={flowContainer.flow.ui.method} | ||
onSubmit={(e) => void methods.handleSubmit(onSubmit)(e)} | ||
{...rest} | ||
> | ||
{children} | ||
</Card.SettingsSection> | ||
) | ||
} |
Oops, something went wrong.