Skip to content

Commit

Permalink
Support signals 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jnicklas committed Dec 29, 2023
1 parent 2c32194 commit 73d6f31
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .changeset/cold-otters-care.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"signal-form": minor
---

Added support for signals 2.0
3 changes: 3 additions & 0 deletions lib/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { createContext, useContext } from "react";
import type { ReadonlySignal } from "@preact/signals-react";
import type { ValidationError, ValidationResult } from "~/utils/validate";
import type { AnyObjectSchema, InferType } from "yup";
import { useSignals } from "@preact/signals-react/runtime";

export type Touched = Record<string, boolean>;
export type FieldsContext = {
Expand Down Expand Up @@ -30,6 +31,7 @@ export const FieldsContext = createContext<FieldsContext | undefined>(
);

export function useFormContext<S extends AnyObjectSchema = any>() {
useSignals();
let context = useContext(FormContext) as FormContext<S>;
if (context) {
return context;
Expand All @@ -39,6 +41,7 @@ export function useFormContext<S extends AnyObjectSchema = any>() {
}

export function useFieldsContext() {
useSignals();
let context = useContext(FieldsContext);
if (context) {
return context;
Expand Down
2 changes: 2 additions & 0 deletions lib/fields-array-for.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { FieldsForProps } from "./fields-for";
import { useFieldsArray } from "~/use-fields-array";
import { FieldsContext, useFieldsContext } from "~/context";
import type { ReadonlySignal } from "@preact/signals-react";
import { useSignals } from "@preact/signals-react/runtime";

export function FieldsArrayFor({
name,
Expand Down Expand Up @@ -55,6 +56,7 @@ let FieldsArrayRowContext = createContext<FieldsArrayRow | undefined>(
);

export function useFieldsArrayRow() {
useSignals();
let context = useContext(FieldsArrayRowContext);
if (context) {
return context;
Expand Down
3 changes: 3 additions & 0 deletions lib/use-field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { signal as createSignal, computed } from "@preact/signals-react";
import type { ReadonlySignal } from "@preact/signals-react";
import { FormContext, FieldsContext } from "./context";
import type { ValidationError } from "~/utils/validate";
import { useSignals } from "@preact/signals-react/runtime";

export type Field<T> = {
name: string;
Expand All @@ -22,6 +23,8 @@ export function useField<T>(
fieldName: string,
options: UseFieldOptions = {}
): Field<T> {
useSignals();

const formContext = useContext(FormContext);
const fieldContext = useContext(FieldsContext);

Expand Down
34 changes: 17 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"css-check": "ts-node --esm ./scripts/css-check.ts"
},
"dependencies": {
"@preact/signals-react": "^1.1.0",
"@preact/signals-react": "^2.0.0",
"@remix-run/node": ">=1.19.0",
"@remix-run/react": ">=1.19.0",
"react": "^18.0.0",
Expand Down

0 comments on commit 73d6f31

Please sign in to comment.