This commit is contained in:
@@ -3,6 +3,7 @@ export * from "./useCallbackPromise.js"
|
||||
export * from "./useCallbackSync.js"
|
||||
export * from "./useContext.js"
|
||||
export * from "./useEffect.js"
|
||||
export * from "./useInput.js"
|
||||
export * from "./useLayoutEffect.js"
|
||||
export * from "./useMemo.js"
|
||||
export * from "./useOnce.js"
|
||||
|
||||
59
packages/effect-fc/src/hooks/Hooks/useInput.ts
Normal file
59
packages/effect-fc/src/hooks/Hooks/useInput.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
import { type Duration, Effect, flow, Option, ParseResult, Ref, Schema, Stream, SubscriptionRef, Types } from "effect"
|
||||
import * as React from "react"
|
||||
import { useCallbackSync } from "./useCallbackSync.js"
|
||||
import { useFork } from "./useFork.js"
|
||||
import { useOnce } from "./useOnce.js"
|
||||
import { useSubscribeRefs } from "./useSubscribeRefs.js"
|
||||
|
||||
|
||||
export namespace useInput {
|
||||
export interface Options<A, R> {
|
||||
readonly ref: SubscriptionRef.SubscriptionRef<A>
|
||||
readonly schema: Schema.Schema<Types.NoInfer<A>, string, R>
|
||||
readonly debounce?: Duration.Duration
|
||||
}
|
||||
|
||||
export interface Result {
|
||||
readonly value: string
|
||||
readonly onChange: React.ChangeEventHandler<HTMLInputElement | HTMLTextAreaElement>
|
||||
readonly error: Option.Option<ParseResult.ParseError>
|
||||
}
|
||||
}
|
||||
|
||||
export const useInput: {
|
||||
<A, R>(options: useInput.Options<A, R>): Effect.Effect<useInput.Result, ParseResult.ParseError, R>
|
||||
} = Effect.fnUntraced(function* <A, R>(options: useInput.Options<A, R>) {
|
||||
const internalRef = yield* useOnce(() => options.ref.pipe(
|
||||
Effect.andThen(Schema.encode(options.schema)),
|
||||
Effect.andThen(SubscriptionRef.make),
|
||||
))
|
||||
const [error, setError] = React.useState(Option.none<ParseResult.ParseError>())
|
||||
|
||||
yield* useFork(() => Effect.all([
|
||||
Stream.runForEach(options.ref, upstreamValue =>
|
||||
Effect.andThen(internalRef, internalValue =>
|
||||
upstreamValue !== internalValue
|
||||
? Effect.andThen(Schema.encode(options.schema)(upstreamValue), v => Ref.set(internalRef, v))
|
||||
: Effect.void
|
||||
)
|
||||
),
|
||||
|
||||
Stream.runForEach(
|
||||
options.debounce ? Stream.debounce(internalRef, options.debounce) : internalRef,
|
||||
flow(
|
||||
Schema.decode(options.schema),
|
||||
Effect.andThen(v => Ref.set(options.ref, v)),
|
||||
Effect.andThen(() => setError(Option.none())),
|
||||
Effect.catchTag("ParseError", e => Effect.sync(() => setError(Option.some(e)))),
|
||||
),
|
||||
),
|
||||
], { concurrency: "unbounded" }), [options.ref, options.schema, options.debounce, internalRef])
|
||||
|
||||
const [value] = yield* useSubscribeRefs(internalRef)
|
||||
const onChange = yield* useCallbackSync((e: React.ChangeEvent<HTMLInputElement>) => Ref.set(
|
||||
internalRef,
|
||||
e.target.value,
|
||||
), [internalRef])
|
||||
|
||||
return { value, onChange, error }
|
||||
})
|
||||
Reference in New Issue
Block a user