@@ -1,3 +1,4 @@
|
||||
import type { StandardSchemaV1 } from "@standard-schema/spec"
|
||||
import { Array, type Cause, Chunk, type Duration, Effect, Equal, Function, identity, Option, Pipeable, Predicate, type Scope, Stream, SubscriptionRef } from "effect"
|
||||
import type * as React from "react"
|
||||
import * as Component from "./Component.js"
|
||||
@@ -8,11 +9,6 @@ import * as View from "./View.js"
|
||||
export const FormTypeId: unique symbol = Symbol.for("@effect-fc/Form/Form")
|
||||
export type FormTypeId = typeof FormTypeId
|
||||
|
||||
export interface FormIssue {
|
||||
readonly path: readonly PropertyKey[]
|
||||
readonly message: string
|
||||
}
|
||||
|
||||
export interface Form<out P extends readonly PropertyKey[], in out A, in out I = A, in out ER = never, in out EW = never>
|
||||
extends Pipeable.Pipeable {
|
||||
readonly [FormTypeId]: FormTypeId
|
||||
@@ -20,8 +16,8 @@ extends Pipeable.Pipeable {
|
||||
readonly path: P
|
||||
readonly value: View.View<Option.Option<A>, ER, never>
|
||||
readonly encodedValue: Lens.Lens<I, ER, EW, never, never>
|
||||
readonly issues: View.View<readonly FormIssue[], never, never>
|
||||
readonly isValidating: View.View<boolean, never, never>
|
||||
readonly issues: View.View<readonly StandardSchemaV1.Issue[], never, never>
|
||||
readonly isValidating: View.View<boolean, ER, never>
|
||||
readonly canCommit: View.View<boolean, never, never>
|
||||
readonly isCommitting: View.View<boolean, never, never>
|
||||
}
|
||||
@@ -34,7 +30,7 @@ extends Pipeable.Class implements Form<P, A, I, ER, EW> {
|
||||
readonly path: P,
|
||||
readonly value: View.View<Option.Option<A>, ER, never>,
|
||||
readonly encodedValue: Lens.Lens<I, ER, EW, never, never>,
|
||||
readonly issues: View.View<readonly FormIssue[], never, never>,
|
||||
readonly issues: View.View<readonly StandardSchemaV1.Issue[], never, never>,
|
||||
readonly isValidating: View.View<boolean, never, never>,
|
||||
readonly canCommit: View.View<boolean, never, never>,
|
||||
readonly isCommitting: View.View<boolean, never, never>,
|
||||
@@ -43,16 +39,17 @@ extends Pipeable.Class implements Form<P, A, I, ER, EW> {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export const isForm = (u: unknown): u is Form<readonly PropertyKey[], unknown, unknown> => Predicate.hasProperty(u, FormTypeId)
|
||||
|
||||
|
||||
const filterIssuesByPath = (
|
||||
issues: readonly FormIssue[],
|
||||
issues: readonly StandardSchemaV1.Issue[],
|
||||
path: readonly PropertyKey[],
|
||||
): readonly FormIssue[] => Array.filter(issues, issue =>
|
||||
issue.path.length >= path.length && Array.every(path, (p, i) => p === issue.path[i])
|
||||
)
|
||||
): readonly StandardSchemaV1.Issue[] => Array.filter(issues, issue => {
|
||||
const issuePath = issue.path
|
||||
if (!issuePath) return false
|
||||
return issuePath.length >= path.length && Array.every(path, (p, i) => p === issuePath[i])
|
||||
})
|
||||
|
||||
export const focusObjectOn: {
|
||||
<P extends readonly PropertyKey[], A extends object, I extends object, ER, EW, K extends keyof A & keyof I>(
|
||||
@@ -66,7 +63,7 @@ export const focusObjectOn: {
|
||||
self: Form<P, A, I, ER, EW>,
|
||||
key: K,
|
||||
): Form<readonly [...P, K], A[K], I[K], ER, EW> => {
|
||||
const form = self as unknown as FormImpl<P, A, I, ER, EW>
|
||||
const form = self as FormImpl<P, A, I, ER, EW>
|
||||
const path = [...form.path, key] as const
|
||||
|
||||
return new FormImpl(
|
||||
@@ -92,12 +89,12 @@ export const focusArrayAt: {
|
||||
self: Form<P, A, I, ER, EW>,
|
||||
index: number,
|
||||
): Form<readonly [...P, number], A[number], I[number], ER | Cause.NoSuchElementError, EW | Cause.NoSuchElementError> => {
|
||||
const form = self as unknown as FormImpl<P, A, I, ER, EW>
|
||||
const form = self as FormImpl<P, A, I, ER, EW>
|
||||
const path = [...form.path, index] as const
|
||||
|
||||
return new FormImpl(
|
||||
path,
|
||||
View.mapOptionEffect(form.value, values => Effect.fromOption(Array.get(values, index))),
|
||||
View.mapOptionEffect(form.value, value => Effect.fromOption(Array.get(value, index))),
|
||||
Lens.focusArrayAt(form.encodedValue, index),
|
||||
View.map(form.issues, issues => filterIssuesByPath(issues, path)),
|
||||
form.isValidating,
|
||||
@@ -118,12 +115,12 @@ export const focusTupleAt: {
|
||||
self: Form<P, A, I, ER, EW>,
|
||||
index: K,
|
||||
): Form<readonly [...P, K], A[K], I[K], ER, EW> => {
|
||||
const form = self as unknown as FormImpl<P, A, I, ER, EW>
|
||||
const form = self as FormImpl<P, A, I, ER, EW>
|
||||
const path = [...form.path, index] as const
|
||||
|
||||
return new FormImpl(
|
||||
path,
|
||||
View.mapOption(form.value, values => values[index]),
|
||||
View.mapOption(form.value, Array.getUnsafe(index)),
|
||||
Lens.focusTupleAt(form.encodedValue, index),
|
||||
View.map(form.issues, issues => filterIssuesByPath(issues, path)),
|
||||
form.isValidating,
|
||||
@@ -144,12 +141,12 @@ export const focusChunkAt: {
|
||||
self: Form<P, Chunk.Chunk<A>, Chunk.Chunk<I>, ER, EW>,
|
||||
index: number,
|
||||
): Form<readonly [...P, number], A, I, ER | Cause.NoSuchElementError, EW> => {
|
||||
const form = self as unknown as FormImpl<P, Chunk.Chunk<A>, Chunk.Chunk<I>, ER, EW>
|
||||
const form = self as FormImpl<P, Chunk.Chunk<A>, Chunk.Chunk<I>, ER, EW>
|
||||
const path = [...form.path, index] as const
|
||||
|
||||
return new FormImpl(
|
||||
path,
|
||||
View.mapOptionEffect(form.value, values => Effect.fromOption(Chunk.get(values, index))),
|
||||
View.mapOptionEffect(form.value, value => Effect.fromOption(Chunk.get(value, index))),
|
||||
Lens.focusChunkAt(form.encodedValue, index),
|
||||
View.map(form.issues, issues => filterIssuesByPath(issues, path)),
|
||||
form.isValidating,
|
||||
@@ -183,11 +180,9 @@ export const useInput = Effect.fnUntraced(function* <P extends readonly Property
|
||||
yield* Effect.forkScoped(Effect.all([
|
||||
Stream.runForEach(
|
||||
Stream.drop(form.encodedValue.changes, 1),
|
||||
upstreamEncodedValue => Effect.flatMap(
|
||||
Lens.get(internalValueLens),
|
||||
internalValue => !Equal.equals(upstreamEncodedValue, internalValue)
|
||||
? Lens.set(internalValueLens, upstreamEncodedValue)
|
||||
: Effect.succeed(undefined),
|
||||
upstreamEncodedValue => Effect.when(
|
||||
Lens.set(internalValueLens, upstreamEncodedValue),
|
||||
Effect.map(Lens.get(internalValueLens), internalValue => !Equal.equals(upstreamEncodedValue, internalValue)),
|
||||
),
|
||||
),
|
||||
|
||||
@@ -242,11 +237,8 @@ export const useOptionalInput = Effect.fnUntraced(function* <P extends readonly
|
||||
Stream.runForEach(
|
||||
Stream.drop(field.encodedValue.changes, 1),
|
||||
|
||||
upstreamEncodedValue => Effect.flatMap(
|
||||
Effect.all([Lens.get(enabledLens), Lens.get(internalValueLens)]),
|
||||
([enabled, internalValue]) => Equal.equals(upstreamEncodedValue, enabled ? Option.some(internalValue) : Option.none())
|
||||
? Effect.succeed(undefined)
|
||||
: Option.match(upstreamEncodedValue, {
|
||||
upstreamEncodedValue => Effect.when(
|
||||
Option.match(upstreamEncodedValue, {
|
||||
onSome: v => Effect.andThen(
|
||||
Lens.set(enabledLens, true),
|
||||
Lens.set(internalValueLens, v),
|
||||
@@ -256,6 +248,11 @@ export const useOptionalInput = Effect.fnUntraced(function* <P extends readonly
|
||||
Lens.set(internalValueLens, options.defaultValue),
|
||||
),
|
||||
}),
|
||||
|
||||
Effect.map(
|
||||
Effect.all([Lens.get(enabledLens), Lens.get(internalValueLens)]),
|
||||
([enabled, internalValue]) => !Equal.equals(upstreamEncodedValue, enabled ? Option.some(internalValue) : Option.none()),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
|
||||
Reference in New Issue
Block a user