diff --git a/packages/effect-fc/src/Form.ts b/packages/effect-fc/src/Form.ts index a019282..cebe80a 100644 --- a/packages/effect-fc/src/Form.ts +++ b/packages/effect-fc/src/Form.ts @@ -70,8 +70,7 @@ export const make: { export namespace useInput { - export interface Options> { - readonly path: P + export interface Options { readonly debounce?: Duration.DurationInput } @@ -83,21 +82,23 @@ export namespace useInput { } export const useInput: { - >( + >( self: Form, - options: useInput.Options, P>, + path: P, + options?: useInput.Options, ): Effect.Effect>, NoSuchElementException, R> -} = Effect.fnUntraced(function* >( +} = Effect.fnUntraced(function* >( self: Form, - options: useInput.Options, P>, + path: P, + options?: useInput.Options, ) { const [internalValueRef, issuesSubscribable] = yield* Hooks.useMemo(() => Effect.all([ self.encodedValueRef.pipe( - Effect.andThen(PropertyPath.get(options.path)), + Effect.andThen(PropertyPath.get(path)), Effect.andThen(SubscriptionRef.make>), ), - Effect.succeed(self.makeFieldIssuesSubscribable(options.path)), - ]), [self, ...options.path]) + Effect.succeed(self.makeFieldIssuesSubscribable(path)), + ]), [self, ...path]) const [value, setValue] = yield* Hooks.useRefState(internalValueRef) const [issues] = yield* Hooks.useSubscribe(issuesSubscribable) @@ -105,12 +106,12 @@ export const useInput: { yield* Hooks.useFork(() => Stream.runForEach( internalValueRef.changes.pipe( Stream.changesWith(Equivalence.strict()), - options.debounce ? Stream.debounce(options.debounce) : identity, + options?.debounce ? Stream.debounce(options.debounce) : identity, Stream.drop(1), ), internalValue => self.encodedValueRef.pipe( - Effect.andThen(encodedValue => PropertyPath.immutableSet(encodedValue, options.path, internalValue)), + Effect.andThen(encodedValue => PropertyPath.immutableSet(encodedValue, path, internalValue)), Effect.tap(encodedValue => SubscriptionRef.set(self.encodedValueRef, encodedValue)), Effect.andThen(flow( Schema.decode(self.schema), @@ -119,7 +120,7 @@ export const useInput: { Effect.catchTag("ParseError", e => SubscriptionRef.set(self.errorRef, Option.some(e))) )), ), - ), [internalValueRef, self, ...options.path]) + ), [internalValueRef, self, ...path]) return { value, setValue, issues } }) diff --git a/packages/example/src/routes/form.tsx b/packages/example/src/routes/form.tsx index f4bc4e0..d2307b3 100644 --- a/packages/example/src/routes/form.tsx +++ b/packages/example/src/routes/form.tsx @@ -20,8 +20,8 @@ class RegisterForm extends Effect.Service()("RegisterForm", { class RegisterPage extends Component.makeUntraced(function* RegisterPage() { const form = yield* RegisterForm - const emailInput = yield* Form.useInput(form, { path: ["email"] }) - const passwordInput = yield* Form.useInput(form, { path: ["password"] }) + const emailInput = yield* Form.useInput(form, ["email"]) + const passwordInput = yield* Form.useInput(form, ["password"]) yield* useFork(() => Stream.runForEach(form.valueRef.changes, Console.log), [])