0.1.3 #4
@@ -8,30 +8,33 @@ import * as React from "react"
|
|||||||
export type TextFieldInputProps<A, R> = Omit<useInput.Options<A, R>, "schema"> & Omit<TextField.RootProps, "ref">
|
export type TextFieldInputProps<A, R> = Omit<useInput.Options<A, R>, "schema"> & Omit<TextField.RootProps, "ref">
|
||||||
export type TextFieldOptionalInputProps<A, R> = Omit<useOptionalInput.Options<A, R>, "schema"> & Omit<TextField.RootProps, "ref">
|
export type TextFieldOptionalInputProps<A, R> = Omit<useOptionalInput.Options<A, R>, "schema"> & Omit<TextField.RootProps, "ref">
|
||||||
|
|
||||||
export const TextFieldInput: {
|
export const TextFieldInput = <A, R, O extends boolean = false>(options: {
|
||||||
<A, R>(options: {
|
readonly optional?: O
|
||||||
readonly optional: true
|
|
||||||
readonly schema: Schema.Schema<A, string, R>
|
|
||||||
}): Component.Component<TextFieldOptionalInputProps<A, R>, React.JSX.Element, ParseResult.ParseError, R>
|
|
||||||
<A, R>(options: {
|
|
||||||
readonly optional?: false
|
|
||||||
readonly schema: Schema.Schema<A, string, R>
|
|
||||||
}): Component.Component<TextFieldInputProps<A, R>, React.JSX.Element, ParseResult.ParseError, R>
|
|
||||||
} = <A, R>(options: {
|
|
||||||
readonly optional?: boolean
|
|
||||||
readonly schema: Schema.Schema<A, string, R>
|
readonly schema: Schema.Schema<A, string, R>
|
||||||
}) => Component.makeUntraced(function* TextFieldInput(props: TextFieldOptionalInputProps<A, R> | TextFieldInputProps<A, R>) {
|
}) => Component.makeUntraced(function* TextFieldInput(props: O extends true
|
||||||
const input = options.optional
|
? TextFieldOptionalInputProps<A, R>
|
||||||
// eslint-disable-next-line react-hooks/rules-of-hooks
|
: TextFieldInputProps<A, R>
|
||||||
? yield* useOptionalInput({
|
) {
|
||||||
schema: options.schema,
|
const input: (
|
||||||
...props as TextFieldOptionalInputProps<A, R>,
|
| { readonly optional: true } & useOptionalInput.Result
|
||||||
})
|
| { readonly optional: false } & useInput.Result
|
||||||
// eslint-disable-next-line react-hooks/rules-of-hooks
|
) = options.optional
|
||||||
: yield* useInput({
|
? {
|
||||||
schema: options.schema,
|
optional: true,
|
||||||
...props as TextFieldInputProps<A, R>,
|
// eslint-disable-next-line react-hooks/rules-of-hooks
|
||||||
})
|
...yield* useOptionalInput({
|
||||||
|
schema: options.schema,
|
||||||
|
...props as TextFieldOptionalInputProps<A, R>,
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
: {
|
||||||
|
optional: false,
|
||||||
|
// eslint-disable-next-line react-hooks/rules-of-hooks
|
||||||
|
...yield* useInput({
|
||||||
|
schema: options.schema,
|
||||||
|
...props as TextFieldInputProps<A, R>,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
const issues = React.useMemo(() => Option.map(
|
const issues = React.useMemo(() => Option.map(
|
||||||
input.error,
|
input.error,
|
||||||
@@ -49,15 +52,16 @@ export const TextFieldInput: {
|
|||||||
}
|
}
|
||||||
|
|
||||||
<Flex direction="row" align="center" gap="1">
|
<Flex direction="row" align="center" gap="1">
|
||||||
{options.optional &&
|
{input.optional &&
|
||||||
<Switch
|
<Switch
|
||||||
|
checked={!input.disabled}
|
||||||
|
onCheckedChange={checked => input.setDisabled(!checked)}
|
||||||
/>
|
/>
|
||||||
}
|
}
|
||||||
|
|
||||||
<TextField.Root
|
<TextField.Root
|
||||||
{...input}
|
{...input}
|
||||||
{...Struct.omit(props, "ref")}
|
{...Struct.omit(props as TextFieldOptionalInputProps<A, R> | TextFieldInputProps<A, R>, "ref")}
|
||||||
/>
|
/>
|
||||||
</Flex>
|
</Flex>
|
||||||
</Flex>
|
</Flex>
|
||||||
|
|||||||
Reference in New Issue
Block a user