0.1.3 (#4)
Co-authored-by: Julien Valverdé <julien.valverde@mailo.com> Reviewed-on: https://gitea:3000/Thilawyn/effect-fc/pulls/4
This commit was merged in pull request #4.
This commit is contained in:
40
packages/example/src/lib/input/TextAreaInput.tsx
Normal file
40
packages/example/src/lib/input/TextAreaInput.tsx
Normal file
@@ -0,0 +1,40 @@
|
||||
import { Callout, Flex, TextArea, TextAreaProps } from "@radix-ui/themes"
|
||||
import { Array, Equivalence, Option, ParseResult, Schema, Struct } from "effect"
|
||||
import { Component } from "effect-fc"
|
||||
import { useInput } from "effect-fc/hooks"
|
||||
import * as React from "react"
|
||||
|
||||
|
||||
export type TextAreaInputProps<A, R> = Omit<useInput.Options<A, R>, "schema" | "equivalence"> & Omit<TextAreaProps, "ref">
|
||||
|
||||
export const TextAreaInput = <A, R>(options: {
|
||||
readonly schema: Schema.Schema<A, string, R>
|
||||
readonly equivalence?: Equivalence.Equivalence<A>
|
||||
}): Component.Component<
|
||||
TextAreaInputProps<A, R>,
|
||||
React.JSX.Element,
|
||||
ParseResult.ParseError,
|
||||
R
|
||||
> => Component.makeUntraced(function* TextFieldInput(props) {
|
||||
const input = yield* useInput({ ...options, ...props })
|
||||
const issue = React.useMemo(() => input.error.pipe(
|
||||
Option.map(ParseResult.ArrayFormatter.formatErrorSync),
|
||||
Option.flatMap(Array.head),
|
||||
), [input.error])
|
||||
|
||||
return (
|
||||
<Flex direction="column" gap="1">
|
||||
<TextArea
|
||||
value={input.value}
|
||||
onChange={e => input.setValue(e.target.value)}
|
||||
{...Struct.omit(props, "ref")}
|
||||
/>
|
||||
|
||||
{Option.isSome(issue) &&
|
||||
<Callout.Root color="red" role="alert">
|
||||
<Callout.Text>{issue.value.message}</Callout.Text>
|
||||
</Callout.Root>
|
||||
}
|
||||
</Flex>
|
||||
)
|
||||
})
|
||||
70
packages/example/src/lib/input/TextFieldInput.tsx
Normal file
70
packages/example/src/lib/input/TextFieldInput.tsx
Normal file
@@ -0,0 +1,70 @@
|
||||
import { Callout, Checkbox, Flex, TextField } from "@radix-ui/themes"
|
||||
import { Array, Equivalence, Option, ParseResult, Schema, Struct } from "effect"
|
||||
import { Component, Memo } from "effect-fc"
|
||||
import { useInput, useOptionalInput } from "effect-fc/hooks"
|
||||
import * as React from "react"
|
||||
|
||||
|
||||
export type TextFieldInputProps<A, R> = (
|
||||
& Omit<useInput.Options<A, R>, "schema" | "equivalence">
|
||||
& Omit<TextField.RootProps, "ref">
|
||||
)
|
||||
export type TextFieldOptionalInputProps<A, R> = (
|
||||
& Omit<useOptionalInput.Options<A, R>, "schema" | "equivalence">
|
||||
& Omit<TextField.RootProps, "ref" | "defaultValue">
|
||||
)
|
||||
|
||||
export const TextFieldInput = <A, R, O extends boolean = false>(options: {
|
||||
readonly optional?: O
|
||||
readonly schema: Schema.Schema<A, string, R>
|
||||
readonly equivalence?: Equivalence.Equivalence<A>
|
||||
}) => Component.makeUntraced(function* TextFieldInput(props: O extends true
|
||||
? TextFieldOptionalInputProps<A, R>
|
||||
: TextFieldInputProps<A, R>
|
||||
) {
|
||||
const input: (
|
||||
| { readonly optional: true } & useOptionalInput.Result
|
||||
| { readonly optional: false } & useInput.Result
|
||||
) = options.optional
|
||||
? {
|
||||
optional: true,
|
||||
// eslint-disable-next-line react-hooks/rules-of-hooks
|
||||
...yield* useOptionalInput({ ...options, ...props as TextFieldOptionalInputProps<A, R> }),
|
||||
}
|
||||
: {
|
||||
optional: false,
|
||||
// eslint-disable-next-line react-hooks/rules-of-hooks
|
||||
...yield* useInput({ ...options, ...props as TextFieldInputProps<A, R> }),
|
||||
}
|
||||
|
||||
const issue = React.useMemo(() => input.error.pipe(
|
||||
Option.map(ParseResult.ArrayFormatter.formatErrorSync),
|
||||
Option.flatMap(Array.head),
|
||||
), [input.error])
|
||||
|
||||
return (
|
||||
<Flex direction="column" gap="1">
|
||||
<Flex direction="row" align="center" gap="1">
|
||||
{input.optional &&
|
||||
<Checkbox
|
||||
checked={input.enabled}
|
||||
onCheckedChange={checked => input.setEnabled(checked !== "indeterminate" && checked)}
|
||||
/>
|
||||
}
|
||||
|
||||
<TextField.Root
|
||||
value={input.value}
|
||||
onChange={e => input.setValue(e.target.value)}
|
||||
disabled={input.optional ? !input.enabled : undefined}
|
||||
{...Struct.omit(props as TextFieldOptionalInputProps<A, R> | TextFieldInputProps<A, R>, "ref", "defaultValue")}
|
||||
/>
|
||||
</Flex>
|
||||
|
||||
{(!(input.optional && !input.enabled) && Option.isSome(issue)) &&
|
||||
<Callout.Root color="red" role="alert">
|
||||
<Callout.Text>{issue.value.message}</Callout.Text>
|
||||
</Callout.Root>
|
||||
}
|
||||
</Flex>
|
||||
)
|
||||
}).pipe(Memo.memo)
|
||||
38
packages/example/src/lib/schema/datetime.ts
Normal file
38
packages/example/src/lib/schema/datetime.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { DateTime, Effect, Option, ParseResult, Schema } from "effect"
|
||||
|
||||
|
||||
export class DateTimeUtcFromZoned extends Schema.transformOrFail(
|
||||
Schema.DateTimeZonedFromSelf,
|
||||
Schema.DateTimeUtcFromSelf,
|
||||
{
|
||||
strict: true,
|
||||
encode: DateTime.setZoneCurrent,
|
||||
decode: i => ParseResult.succeed(DateTime.toUtc(i)),
|
||||
},
|
||||
) {}
|
||||
|
||||
export class DateTimeZonedFromUtc extends Schema.transformOrFail(
|
||||
Schema.DateTimeUtcFromSelf,
|
||||
Schema.DateTimeZonedFromSelf,
|
||||
{
|
||||
strict: true,
|
||||
encode: a => ParseResult.succeed(DateTime.toUtc(a)),
|
||||
decode: DateTime.setZoneCurrent,
|
||||
},
|
||||
) {}
|
||||
|
||||
export class DateTimeUtcFromZonedInput extends Schema.transformOrFail(
|
||||
Schema.String,
|
||||
DateTimeUtcFromZoned,
|
||||
{
|
||||
strict: true,
|
||||
encode: a => ParseResult.succeed(DateTime.formatIsoZoned(a).slice(0, 16)),
|
||||
decode: (i, _, ast) => Effect.flatMap(
|
||||
DateTime.CurrentTimeZone,
|
||||
timeZone => Option.match(DateTime.makeZoned(i, { timeZone, adjustForTimeZone: true }), {
|
||||
onSome: ParseResult.succeed,
|
||||
onNone: () => ParseResult.fail(new ParseResult.Type(ast, i, `Unable to decode ${JSON.stringify(i)} into a DateTime.Zoned`)),
|
||||
}),
|
||||
),
|
||||
},
|
||||
) {}
|
||||
2
packages/example/src/lib/schema/index.ts
Normal file
2
packages/example/src/lib/schema/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from "./datetime"
|
||||
export * from "./json"
|
||||
6
packages/example/src/lib/schema/json.ts
Normal file
6
packages/example/src/lib/schema/json.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import type { Schema } from "effect"
|
||||
import type { JsonValue } from "type-fest"
|
||||
|
||||
|
||||
export const assertEncodedJsonifiable = <S extends Schema.Schema<A, I, R>, A, I extends JsonValue, R>(schema: S & Schema.Schema<A, I, R>): S => schema
|
||||
export const assertTypeJsonifiable = <S extends Schema.Schema<A, I, R>, A extends JsonValue, I, R>(schema: S & Schema.Schema<A, I, R>): S => schema
|
||||
Reference in New Issue
Block a user