Skip to content

Commit

Permalink
throw when used outside of context
Browse files Browse the repository at this point in the history
  • Loading branch information
huntabyte committed Dec 16, 2024
1 parent 1596851 commit 482f7a5
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion packages/formsnap/src/lib/formsnap.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import { fromStore } from "svelte/store";
import type { FormPath, InputConstraint, InputConstraints } from "sveltekit-superforms";
import type { FormPathArrays, TaintedFields, ValidationErrors } from "sveltekit-superforms/client";
import { getContext, setContext } from "svelte";
import { getContext, hasContext, setContext } from "svelte";
import { extractErrorArray } from "./internal/utils/errors.js";
import { getValueAtPath } from "./internal/utils/path.js";
import {
Expand Down Expand Up @@ -544,6 +544,11 @@ export function useFormControl(props: UseFormControlProps = {}) {
* @returns The props to spread onto the element acting as the form control.
*/
export function controlProps() {
if (!hasContext(FORM_CONTROL_CTX)) {
throw new Error(
"No parent <Control> component found. `controlProps` must be used within a descendant of the <Control> component."
);
}
return useFormControl().props;
}

Expand All @@ -562,6 +567,12 @@ export function controlProps() {
* @returns The props to spread onto the element acting as the form control.
*/
export function labelProps() {
if (!hasContext(FORM_CONTROL_CTX)) {
throw new Error(
"No parent <Control> component found. `labelProps` must be used within a descendant of the <Control> component."
);
}

return useFormControl().labelProps;
}

Expand Down

0 comments on commit 482f7a5

Please sign in to comment.