@nativetail/ui 0.2.2 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nativetail/ui",
3
- "version": "0.2.2",
3
+ "version": "0.2.4",
4
4
  "description": "",
5
5
  "main": "src/index.ts",
6
6
  "scripts": {},
@@ -29,7 +29,6 @@
29
29
  "@hookform/resolvers": "^3.6.0",
30
30
  "@nativetail/core": "^0.0.6",
31
31
  "@shopify/flash-list": "^1.7.1",
32
- "@sinclair/typebox": "0.32.34",
33
32
  "countries-list": "^3.1.0",
34
33
  "expo-blur": "^13.0.2",
35
34
  "expo-linear-gradient": "~13.0.2",
@@ -1,9 +1,8 @@
1
1
  import { zodResolver } from "@hookform/resolvers/zod";
2
- import { typeboxResolver } from "@hookform/resolvers/typebox";
3
2
 
4
3
  import { cn, TextInputProps, View } from "@nativetail/core";
5
4
  import React, { ComponentPropsWithoutRef } from "react";
6
- import { Control, DefaultValues, FieldValues, useForm } from "react-hook-form";
5
+ import { Control, DefaultValues, useForm } from "react-hook-form";
7
6
  import { z } from "zod";
8
7
  import { Button } from "../button";
9
8
  import {
@@ -16,16 +15,8 @@ import {
16
15
  PinInputProps,
17
16
  } from "../input";
18
17
  import { MultiSelect, MultiSelectProps, Select, SelectProps } from "../select";
19
- import {
20
- Type,
21
- TSchema,
22
- Static,
23
- JavaScriptTypeBuilder,
24
- TObject,
25
- TProperties,
26
- } from "@sinclair/typebox";
27
18
 
28
- type ZodFormBuilderProps<
19
+ export type FormBuilderProps<
29
20
  T extends z.ZodObject<z.ZodRawShape>,
30
21
  IValues = z.infer<T>,
31
22
  > = {
@@ -44,46 +35,13 @@ type ZodFormBuilderProps<
44
35
  containerClassname?: string;
45
36
  submitButtonProps?: ComponentPropsWithoutRef<typeof Button>;
46
37
  onSubmit?: (values: IValues, reset?: () => void) => void;
47
- onError?: (values: Partial<Record<keyof T, any>>) => void;
48
- isSubmitting?: boolean;
49
- defaultValues?: DefaultValues<IValues>;
50
- inputContainerClassname?: string;
51
- };
52
- type TypeboxFormBuilderProps<T extends TSchema, IValues = Static<T>> = {
53
- schema: TSchema;
54
- inputs?: Partial<
55
- Record<
56
- keyof IValues,
57
- {
58
- render?: (props: {
59
- control: Control<IValues, any>;
60
- name: string;
61
- }) => React.ReactElement;
62
- } & InputType
63
- >
64
- >;
65
- containerClassname?: string;
66
- submitButtonProps?: ComponentPropsWithoutRef<typeof Button>;
67
- onSubmit?: (values: IValues, reset?: () => void) => void;
68
- onError?: (values: Partial<Record<string, any>>) => void;
38
+ onError?: (values: Partial<Record<keyof IValues, any>>) => void;
69
39
  isSubmitting?: boolean;
70
40
  defaultValues?: DefaultValues<IValues>;
71
41
  inputContainerClassname?: string;
72
42
  };
73
43
 
74
- export type FormBuilderProps<T extends z.ZodObject<z.ZodRawShape> | TSchema> =
75
- T extends z.ZodObject<z.ZodRawShape>
76
- ? ZodFormBuilderProps<T>
77
- : T extends TSchema
78
- ? TypeboxFormBuilderProps<T>
79
- : never;
80
- const isTypebox = (shape: any): shape is TObject<TProperties> => {
81
- return shape instanceof JavaScriptTypeBuilder;
82
- };
83
- const isZod = (shape: any): shape is z.ZodObject<z.ZodRawShape> => {
84
- return shape instanceof z.ZodObject;
85
- };
86
- export function FormBuilder<T extends z.ZodObject<z.ZodRawShape> | TSchema>({
44
+ export function FormBuilder<T extends z.ZodObject<z.ZodRawShape>>({
87
45
  schema,
88
46
  inputs,
89
47
  containerClassname,
@@ -97,19 +55,9 @@ export function FormBuilder<T extends z.ZodObject<z.ZodRawShape> | TSchema>({
97
55
  const shape = schema.shape;
98
56
  const keys = Object.keys(shape);
99
57
 
100
- type SchemaType = typeof schema;
101
- type FormSchemaType =
102
- SchemaType extends z.ZodObject<z.ZodRawShape>
103
- ? z.infer<SchemaType>
104
- : SchemaType extends TSchema
105
- ? Static<SchemaType>
106
- : any;
58
+ type FormSchemaType = z.infer<T>;
107
59
  const form = useForm<FormSchemaType>({
108
- resolver: isTypebox(schema)
109
- ? typeboxResolver(schema)
110
- : isZod(schema)
111
- ? zodResolver(schema)
112
- : undefined,
60
+ resolver: zodResolver(schema),
113
61
  defaultValues: defaultValues,
114
62
  });
115
63
  return (
@@ -138,9 +86,12 @@ export function FormBuilder<T extends z.ZodObject<z.ZodRawShape> | TSchema>({
138
86
  className="w-full"
139
87
  children={"Submit"}
140
88
  {...submitButtonProps}
141
- onPress={form.handleSubmit((data) => {
142
- onSubmit?.(data, form.reset);
143
- }, onError)}
89
+ onPress={form.handleSubmit(
90
+ (data) => {
91
+ onSubmit?.(data, form.reset);
92
+ },
93
+ (error) => onError?.(error)
94
+ )}
144
95
  isLoading={isSubmitting}
145
96
  />
146
97
  </View>