@ttoss/forms 0.3.6 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,85 @@
1
+ "use strict";
2
+
3
+ // tsup.inject.js
4
+ import * as React from "react";
5
+
6
+ // src/Form.tsx
7
+ import * as React2 from "react";
8
+ import { Box } from "@ttoss/ui";
9
+ import { FormProvider } from "react-hook-form";
10
+ var Form = ({
11
+ children,
12
+ onSubmit,
13
+ sx,
14
+ ...formMethods
15
+ }) => {
16
+ return /* @__PURE__ */ React2.createElement(FormProvider, {
17
+ ...formMethods
18
+ }, /* @__PURE__ */ React2.createElement(Box, {
19
+ as: "form",
20
+ variant: "forms.form",
21
+ onSubmit: formMethods.handleSubmit((data) => onSubmit(data)),
22
+ sx
23
+ }, children));
24
+ };
25
+
26
+ // src/FormFieldInput.tsx
27
+ import { Box as Box2, Input, Label } from "@ttoss/ui";
28
+
29
+ // src/ErrorMessage.tsx
30
+ import { useFormContext } from "react-hook-form";
31
+ import { ErrorMessage as HookFormErrorMessage } from "@hookform/error-message";
32
+ import { Text } from "@ttoss/ui";
33
+ var ErrorMessage = ({
34
+ name
35
+ }) => {
36
+ const {
37
+ formState: { errors }
38
+ } = useFormContext();
39
+ return /* @__PURE__ */ React.createElement(HookFormErrorMessage, {
40
+ errors,
41
+ name,
42
+ as: /* @__PURE__ */ React.createElement(Text, {
43
+ variant: "text.error",
44
+ role: "alert"
45
+ })
46
+ });
47
+ };
48
+
49
+ // src/FormFieldInput.tsx
50
+ import { useController } from "react-hook-form";
51
+ var FormFieldInput = ({
52
+ label,
53
+ name
54
+ }) => {
55
+ const {
56
+ field: { onChange, onBlur, value, ref }
57
+ } = useController({
58
+ name,
59
+ defaultValue: ""
60
+ });
61
+ const id = `form-field-input-${name}`;
62
+ console.log({ name });
63
+ return /* @__PURE__ */ React.createElement(Box2, null, label && /* @__PURE__ */ React.createElement(Label, {
64
+ htmlFor: id
65
+ }, label), /* @__PURE__ */ React.createElement(Input, {
66
+ ref,
67
+ onChange,
68
+ onBlur,
69
+ value,
70
+ name,
71
+ id
72
+ }), /* @__PURE__ */ React.createElement(ErrorMessage, {
73
+ name
74
+ }));
75
+ };
76
+
77
+ // src/FormField.tsx
78
+ var FormField = () => {
79
+ return null;
80
+ };
81
+ FormField.Input = FormFieldInput;
82
+ export {
83
+ Form,
84
+ FormField
85
+ };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,22 @@
1
- export * from './Form';
2
- export { default as Form } from './Form';
3
- export * from './S3';
4
- export { default as S3 } from './S3';
5
- export * as masks from './masks/masks';
1
+ import * as react_hook_form from 'react-hook-form';
2
+ import { FieldValues } from 'react-hook-form';
3
+ import * as React from 'react';
4
+ import { BoxProps } from '@ttoss/ui';
5
+
6
+ declare const Form: <TFieldValues extends FieldValues = FieldValues>({ children, onSubmit, sx, ...formMethods }: {
7
+ children?: React.ReactNode;
8
+ onSubmit: (data: TFieldValues) => Promise<void> | void;
9
+ sx?: BoxProps['sx'];
10
+ } & {
11
+ children: React.ReactNode | React.ReactNode[];
12
+ } & react_hook_form.UseFormReturn<TFieldValues, any>) => JSX.Element;
13
+
14
+ declare const FormField: {
15
+ (): null;
16
+ Input: <TFieldValues extends react_hook_form.FieldValues = react_hook_form.FieldValues>({ label, name, }: {
17
+ label?: string | undefined;
18
+ name: react_hook_form.Path<TFieldValues>;
19
+ }) => JSX.Element;
20
+ };
21
+
22
+ export { Form, FormField };
package/dist/index.js CHANGED
@@ -1,8 +1,114 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod));
21
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
1
22
 
2
- 'use strict'
23
+ // src/index.ts
24
+ var src_exports = {};
25
+ __export(src_exports, {
26
+ Form: () => Form,
27
+ FormField: () => FormField
28
+ });
29
+ module.exports = __toCommonJS(src_exports);
3
30
 
4
- if (process.env.NODE_ENV === 'production') {
5
- module.exports = require('./forms.cjs.production.min.js')
6
- } else {
7
- module.exports = require('./forms.cjs.development.js')
8
- }
31
+ // tsup.inject.js
32
+ var React = __toESM(require("react"));
33
+
34
+ // src/Form.tsx
35
+ var React2 = __toESM(require("react"));
36
+ var import_ui = require("@ttoss/ui");
37
+ var import_react_hook_form = require("react-hook-form");
38
+ var Form = ({
39
+ children,
40
+ onSubmit,
41
+ sx,
42
+ ...formMethods
43
+ }) => {
44
+ return /* @__PURE__ */ React2.createElement(import_react_hook_form.FormProvider, {
45
+ ...formMethods
46
+ }, /* @__PURE__ */ React2.createElement(import_ui.Box, {
47
+ as: "form",
48
+ variant: "forms.form",
49
+ onSubmit: formMethods.handleSubmit((data) => onSubmit(data)),
50
+ sx
51
+ }, children));
52
+ };
53
+
54
+ // src/FormFieldInput.tsx
55
+ var import_ui3 = require("@ttoss/ui");
56
+
57
+ // src/ErrorMessage.tsx
58
+ var import_react_hook_form2 = require("react-hook-form");
59
+ var import_error_message = require("@hookform/error-message");
60
+ var import_ui2 = require("@ttoss/ui");
61
+ var ErrorMessage = ({
62
+ name
63
+ }) => {
64
+ const {
65
+ formState: { errors }
66
+ } = (0, import_react_hook_form2.useFormContext)();
67
+ return /* @__PURE__ */ React.createElement(import_error_message.ErrorMessage, {
68
+ errors,
69
+ name,
70
+ as: /* @__PURE__ */ React.createElement(import_ui2.Text, {
71
+ variant: "text.error",
72
+ role: "alert"
73
+ })
74
+ });
75
+ };
76
+
77
+ // src/FormFieldInput.tsx
78
+ var import_react_hook_form3 = require("react-hook-form");
79
+ var FormFieldInput = ({
80
+ label,
81
+ name
82
+ }) => {
83
+ const {
84
+ field: { onChange, onBlur, value, ref }
85
+ } = (0, import_react_hook_form3.useController)({
86
+ name,
87
+ defaultValue: ""
88
+ });
89
+ const id = `form-field-input-${name}`;
90
+ console.log({ name });
91
+ return /* @__PURE__ */ React.createElement(import_ui3.Box, null, label && /* @__PURE__ */ React.createElement(import_ui3.Label, {
92
+ htmlFor: id
93
+ }, label), /* @__PURE__ */ React.createElement(import_ui3.Input, {
94
+ ref,
95
+ onChange,
96
+ onBlur,
97
+ value,
98
+ name,
99
+ id
100
+ }), /* @__PURE__ */ React.createElement(ErrorMessage, {
101
+ name
102
+ }));
103
+ };
104
+
105
+ // src/FormField.tsx
106
+ var FormField = () => {
107
+ return null;
108
+ };
109
+ FormField.Input = FormFieldInput;
110
+ // Annotate the CommonJS export names for ESM import in node:
111
+ 0 && (module.exports = {
112
+ Form,
113
+ FormField
114
+ });
package/package.json CHANGED
@@ -1,91 +1,45 @@
1
1
  {
2
- "version": "0.3.6",
3
- "license": "MIT",
2
+ "name": "@ttoss/forms",
3
+ "version": "0.5.0",
4
+ "license": "UNLICENSED",
5
+ "author": "ttoss",
6
+ "contributors": [
7
+ "Pedro Arantes <pedro@arantespp.com> (https://arantespp.com/contact)"
8
+ ],
4
9
  "main": "dist/index.js",
5
- "typings": "dist/index.d.ts",
10
+ "module": "dist/esm/index.js",
6
11
  "files": [
7
12
  "dist",
8
13
  "src"
9
14
  ],
10
- "engines": {
11
- "node": ">=10"
12
- },
13
15
  "scripts": {
14
- "start": "tsdx watch",
15
- "build": "tsdx build",
16
- "test": "tsdx test --passWithNoTests",
17
- "lint": "tsdx lint",
18
- "prepare": "tsdx build",
19
- "size": "size-limit",
20
- "analyze": "size-limit --why",
21
- "storybook": "start-storybook -p 6006",
22
- "build-storybook": "build-storybook"
23
- },
24
- "peerDependencies": {
25
- "react-hook-form": "^6.13.1",
26
- "theme-ui": "^0.3.5",
27
- "yup": "^0.32.8"
28
- },
29
- "husky": {
30
- "hooks": {
31
- "pre-commit": "tsdx lint"
32
- }
16
+ "build": "tsup",
17
+ "test": "jest"
33
18
  },
34
- "prettier": {
35
- "printWidth": 80,
36
- "semi": true,
37
- "singleQuote": true,
38
- "trailingComma": "es5"
19
+ "typings": "dist/index.d.ts",
20
+ "dependencies": {
21
+ "@hookform/error-message": "^2.0.0"
39
22
  },
40
- "np": {
41
- "anyBranch": false,
42
- "branch": "main"
23
+ "peerDependencies": {
24
+ "@ttoss/ui": "^1.19.1",
25
+ "react": "^18.2.0",
26
+ "react-hook-form": "^7.36.1"
43
27
  },
44
- "name": "@ttoss/forms",
45
- "author": "Pedro Arantes <arantespp@gmail.com> (https://twitter.com/arantespp)",
46
- "module": "dist/forms.esm.js",
47
- "size-limit": [
48
- {
49
- "path": "dist/forms.cjs.production.min.js",
50
- "limit": "10 KB"
51
- },
52
- {
53
- "path": "dist/forms.esm.js",
54
- "limit": "10 KB"
55
- }
56
- ],
57
28
  "devDependencies": {
58
- "@babel/core": "^7.12.10",
59
- "@hookform/resolvers": "^1.3.0",
60
- "@size-limit/preset-small-lib": "^4.9.1",
61
- "@storybook/addon-essentials": "^6.1.11",
62
- "@storybook/addon-info": "^5.3.21",
63
- "@storybook/addon-links": "^6.1.11",
64
- "@storybook/addons": "^6.1.11",
65
- "@storybook/react": "^6.1.11",
66
- "@types/lodash": "^4.14.166",
67
- "@types/react": "^17.0.0",
68
- "@types/react-dom": "^17.0.0",
69
- "@types/theme-ui": "^0.3.7",
70
- "babel-loader": "^8.2.2",
71
- "eslint-plugin-react-hooks": "^4.2.0",
72
- "husky": "^4.3.6",
73
- "react": "^17.0.1",
74
- "react-dom": "^17.0.1",
75
- "react-hook-form": "^6.13.1",
76
- "react-is": "^17.0.1",
77
- "size-limit": "^4.9.1",
78
- "theme-ui": "^0.3.5",
79
- "tsdx": "^0.14.1",
80
- "tslib": "^2.0.3",
81
- "typescript": "^4.1.3",
82
- "yup": "^0.32.8"
29
+ "@hookform/resolvers": "^2.9.8",
30
+ "@ttoss/config": "^1.18.0",
31
+ "@ttoss/test-utils": "^1.16.7",
32
+ "@ttoss/ui": "^1.20.0",
33
+ "@types/jest": "^29.0.3",
34
+ "jest": "^29.0.3",
35
+ "react": "^18.2.0",
36
+ "react-hook-form": "^7.36.1",
37
+ "yup": "^0.32.11"
83
38
  },
84
- "dependencies": {
85
- "@hookform/error-message": "^0.0.5",
86
- "date-fns": "^2.16.1",
87
- "imask": "^6.0.5",
88
- "lodash": "^4.17.20",
89
- "lodash-es": "^4.17.20"
39
+ "engines": {
40
+ "node": ">=16"
41
+ },
42
+ "publishConfig": {
43
+ "access": "public"
90
44
  }
91
45
  }
@@ -0,0 +1,21 @@
1
+ import { FieldName, FieldValues, useFormContext } from 'react-hook-form';
2
+ import { ErrorMessage as HookFormErrorMessage } from '@hookform/error-message';
3
+ import { Text } from '@ttoss/ui';
4
+
5
+ export const ErrorMessage = <TFieldValues extends FieldValues = FieldValues>({
6
+ name,
7
+ }: {
8
+ name: FieldName<TFieldValues>;
9
+ }) => {
10
+ const {
11
+ formState: { errors },
12
+ } = useFormContext<TFieldValues>();
13
+
14
+ return (
15
+ <HookFormErrorMessage
16
+ errors={errors}
17
+ name={name as any}
18
+ as={<Text variant="text.error" role="alert" />}
19
+ />
20
+ );
21
+ };
package/src/Form.tsx CHANGED
@@ -1,95 +1,27 @@
1
1
  import * as React from 'react';
2
+ import { Box, BoxProps } from '@ttoss/ui';
3
+ import { FieldValues, FormProvider, FormProviderProps } from 'react-hook-form';
2
4
 
3
- import { ErrorMessage as HookFormErrorMessage } from '@hookform/error-message';
4
- import get from 'lodash/get';
5
- import { FormProvider, useFormContext, UseFormMethods } from 'react-hook-form';
6
- import { Box, BoxProps, Input, InputProps, Label, Text } from 'theme-ui';
7
-
8
- const Form = <T,>({
5
+ export const Form = <TFieldValues extends FieldValues = FieldValues>({
9
6
  children,
10
- methods,
11
- ...boxProps
7
+ onSubmit,
8
+ sx,
9
+ ...formMethods
12
10
  }: {
13
11
  children?: React.ReactNode;
14
- methods: UseFormMethods<T>;
15
- onSubmit?: ((data: React.FormEvent<HTMLDivElement>) => void) | undefined;
16
- } & BoxProps) => {
12
+ onSubmit: (data: TFieldValues) => Promise<void> | void;
13
+ sx?: BoxProps['sx'];
14
+ } & FormProviderProps<TFieldValues>) => {
17
15
  return (
18
- <FormProvider {...methods}>
19
- <Box as="form" variant="forms.form" {...boxProps}>
16
+ <FormProvider {...formMethods}>
17
+ <Box
18
+ as="form"
19
+ variant="forms.form"
20
+ onSubmit={formMethods.handleSubmit((data) => onSubmit(data))}
21
+ sx={sx}
22
+ >
20
23
  {children}
21
24
  </Box>
22
25
  </FormProvider>
23
26
  );
24
27
  };
25
-
26
- export const ErrorMessage = ({ name }: { name: string }) => {
27
- const { errors } = useFormContext();
28
- return (
29
- <HookFormErrorMessage
30
- errors={errors}
31
- name={name}
32
- render={({ message, messages }) => {
33
- if (messages) {
34
- return (
35
- <>
36
- {Object.entries(messages).map(([type, message]) => (
37
- <Text variant="text.error" role="alert" key={type}>
38
- {message}
39
- </Text>
40
- ))}
41
- </>
42
- );
43
- }
44
- return (
45
- <Text variant="text.error" role="alert">
46
- {message}
47
- </Text>
48
- );
49
- }}
50
- />
51
- );
52
- };
53
-
54
- const Field = ({
55
- name,
56
- children,
57
- label,
58
- ref: inputRef,
59
- ...inputProps
60
- }: {
61
- name: string;
62
- children?: React.ReactNode;
63
- label?: string;
64
- } & InputProps) => {
65
- const { errors, register } = useFormContext();
66
- const error = get(errors, name);
67
- const commonProps = {
68
- name,
69
- key: name,
70
- id: name,
71
- 'aria-invalid': (error ? 'true' : 'false') as any,
72
- };
73
- return (
74
- <Box variant="forms.field">
75
- {label && <Label htmlFor={name}>{label}</Label>}
76
- {children ? (
77
- React.Children.map(children, child => {
78
- return React.createElement((child as any).type, {
79
- ref: register(),
80
- ...commonProps,
81
- ...(child as any).props,
82
- });
83
- })
84
- ) : (
85
- <Input ref={register()} {...commonProps} {...inputProps} />
86
- )}
87
- <ErrorMessage name={name} />
88
- </Box>
89
- );
90
- };
91
-
92
- Form.ErrorMessage = ErrorMessage;
93
- Form.Field = Field;
94
-
95
- export default Form;
@@ -0,0 +1,10 @@
1
+ import { FormFieldInput } from './FormFieldInput';
2
+
3
+ const FormField = () => {
4
+ // to be implemented
5
+ return null;
6
+ };
7
+
8
+ FormField.Input = FormFieldInput;
9
+
10
+ export { FormField };
File without changes
@@ -0,0 +1,70 @@
1
+ import * as yup from 'yup';
2
+ import { Button } from '@ttoss/ui';
3
+ import { Form } from './Form';
4
+ import { FormFieldInput } from './FormFieldInput';
5
+ import { render, screen, userEvent } from '@ttoss/test-utils';
6
+ import { useForm } from 'react-hook-form';
7
+ import { yupResolver } from '@hookform/resolvers/yup';
8
+
9
+ test('call onSubmit with correct data', async () => {
10
+ const user = userEvent.setup({ delay: null });
11
+
12
+ const onSubmit = jest.fn();
13
+
14
+ const RenderForm = () => {
15
+ const formMethods = useForm();
16
+
17
+ return (
18
+ <Form {...formMethods} onSubmit={onSubmit}>
19
+ <FormFieldInput name="input1" label="Input 1" />
20
+ <FormFieldInput name="input2" label="Input 2" />
21
+ <Button type="submit">Submit</Button>
22
+ </Form>
23
+ );
24
+ };
25
+
26
+ render(<RenderForm />);
27
+
28
+ await user.type(screen.getByLabelText('Input 1'), 'input1');
29
+
30
+ await user.type(screen.getByLabelText('Input 2'), 'input2');
31
+
32
+ await user.click(screen.getByText('Submit'));
33
+
34
+ expect(onSubmit).toHaveBeenCalledWith({ input1: 'input1', input2: 'input2' });
35
+ });
36
+
37
+ test('should display error messages', async () => {
38
+ const user = userEvent.setup({ delay: null });
39
+
40
+ const onSubmit = jest.fn();
41
+
42
+ const schema = yup.object({
43
+ firstName: yup.string().required('First name is required'),
44
+ age: yup.number().required('Age is required'),
45
+ });
46
+
47
+ const RenderForm = () => {
48
+ const formMethods = useForm({
49
+ defaultValues: {
50
+ age: 0,
51
+ },
52
+ mode: 'all',
53
+ resolver: yupResolver(schema),
54
+ });
55
+
56
+ return (
57
+ <Form {...formMethods} onSubmit={onSubmit}>
58
+ <FormFieldInput name="firstName" label="First Name" />
59
+ <FormFieldInput name="age" label="Age" />
60
+ <Button type="submit">Submit</Button>
61
+ </Form>
62
+ );
63
+ };
64
+
65
+ render(<RenderForm />);
66
+
67
+ await user.click(screen.getByText('Submit'));
68
+
69
+ expect(await screen.findByText('First name is required')).toBeInTheDocument();
70
+ });
@@ -0,0 +1,35 @@
1
+ import { Box, Input, Label } from '@ttoss/ui';
2
+ import { ErrorMessage } from './ErrorMessage';
3
+ import { FieldValues, Path, useController } from 'react-hook-form';
4
+
5
+ export const FormFieldInput = <TFieldValues extends FieldValues = FieldValues>({
6
+ label,
7
+ name,
8
+ }: {
9
+ label?: string;
10
+ name: Path<TFieldValues>;
11
+ }) => {
12
+ const {
13
+ field: { onChange, onBlur, value, ref },
14
+ } = useController<any>({
15
+ name,
16
+ defaultValue: '',
17
+ });
18
+
19
+ const id = `form-field-input-${name}`;
20
+
21
+ return (
22
+ <Box>
23
+ {label && <Label htmlFor={id}>{label}</Label>}
24
+ <Input
25
+ ref={ref}
26
+ onChange={onChange}
27
+ onBlur={onBlur}
28
+ value={value}
29
+ name={name}
30
+ id={id}
31
+ />
32
+ <ErrorMessage name={name} />
33
+ </Box>
34
+ );
35
+ };
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export { Form } from './Form';
2
+ export { FormField } from './FormField';
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2020 TTOSS, [Pedro Arantes](https://twitter.com/arantespp)
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.