Form work
Lint / lint (push) Failing after 2s

This commit is contained in:
Julien Valverdé
2025-09-25 02:31:46 +02:00
parent 2df12d7f40
commit a7471c0d49
2 changed files with 25 additions and 8 deletions
+19 -3
View File
@@ -1,4 +1,4 @@
import { Array, Duration, Effect, Equivalence, flow, identity, Option, ParseResult, Pipeable, Schema, Scope, Stream, Subscribable, SubscriptionRef } from "effect"
import { Array, Duration, Effect, Equal, Equivalence, flow, identity, Option, ParseResult, pipe, Pipeable, Schema, Scope, Stream, Subscribable, SubscriptionRef } from "effect"
import type { NoSuchElementException } from "effect/Cause"
import * as React from "react"
import { Hooks } from "./hooks/index.js"
@@ -16,6 +16,8 @@ extends Pipeable.Pipeable {
readonly encodedValueRef: SubscriptionRef.SubscriptionRef<I>,
readonly errorRef: SubscriptionRef.SubscriptionRef<Option.Option<ParseResult.ParseError>>,
readonly canSubmitSubscribable: Subscribable.Subscribable<boolean>
makeFieldIssuesSubscribable<const P extends PropertyPath.Paths<A>>(
path: P
): Subscribable.Subscribable<readonly ParseResult.ArrayFormatterIssue[]>
@@ -25,6 +27,7 @@ extends Pipeable.Pipeable {
class FormImpl<in out A, in out I, out R>
extends Pipeable.Class() implements Form<A, I, R> {
readonly [TypeId]: TypeId = TypeId
readonly canSubmitSubscribable: Subscribable.Subscribable<boolean>
constructor(
readonly schema: Schema.Schema<A, I, R>,
@@ -33,6 +36,18 @@ extends Pipeable.Class() implements Form<A, I, R> {
readonly errorRef: SubscriptionRef.SubscriptionRef<Option.Option<ParseResult.ParseError>>,
) {
super()
this.canSubmitSubscribable = pipe(
<A>([value, error]: readonly [
Option.Option<A>,
Option.Option<ParseResult.ParseError>,
]) => Option.isSome(value) && Option.isNone(error),
filter => SubscribableInternal.make({
get: Effect.map(Effect.all([valueRef, errorRef]), filter),
get changes() { return Stream.map(Stream.zipLatestAll(valueRef.changes, errorRef.changes), filter)},
}),
)
}
makeFieldIssuesSubscribable<const P extends PropertyPath.Paths<A>>(path: P) {
@@ -146,8 +161,9 @@ export const useInput: {
Stream.drop(1),
),
upstreamEncodedValue => internalValueRef.pipe(
upstreamEncodedValue => Effect.whenEffect(
SubscriptionRef.set(internalValueRef, upstreamEncodedValue),
Effect.andThen(internalValueRef, internalValue => !Equal.equals(upstreamEncodedValue, internalValue)),
),
),