@rebasepro/formex 0.0.1-canary.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Rebase
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.
package/README.md ADDED
@@ -0,0 +1,165 @@
1
+ # Formex - React Form Library
2
+
3
+ Formex is a lightweight, flexible library designed to simplify form handling within React applications. By leveraging React's powerful context and hooks features, Formex allows for efficient form state management with minimal boilerplate code.
4
+
5
+ ## Features
6
+
7
+ - Lightweight and easy to integrate
8
+ - Supports custom field components
9
+ - Built-in validation handling
10
+ - Provides both field-level and form-level state management
11
+
12
+ ## Installation
13
+
14
+ To install Formex, you can use either npm or yarn:
15
+
16
+ ```sh
17
+ npm install @rebasepro/formex
18
+
19
+ # or if you're using yarn
20
+
21
+ yarn add @rebasepro/formex
22
+ ```
23
+
24
+ ## Quick Start
25
+
26
+ To get started with Formex, you first need to create your form context and form controller using the `useCreateFormex` hook. Then, you can structure your form using the `<Field />` components provided by Formex.
27
+
28
+ ### Step 1: Create your form controller
29
+
30
+ ```jsx
31
+ import React from 'react';
32
+ import { useCreateFormex } from 'formex-library';
33
+
34
+ const MyForm = () => {
35
+ const formController = useCreateFormex({
36
+ initialValues: {
37
+ name: '',
38
+ email: '',
39
+ },
40
+ // Optionally add a validation function
41
+ // validation: values => {
42
+ // const errors = {};
43
+ // if (!values.name) errors.name = 'Name is required';
44
+ // return errors;
45
+ // },
46
+ onSubmit: (values) => {
47
+ console.log('Form Submitted:', values);
48
+ },
49
+ });
50
+
51
+ return (
52
+ <form onSubmit={formController.handleSubmit}>
53
+ {/* Field components go here */}
54
+ </form>
55
+ );
56
+ };
57
+ ```
58
+
59
+ ### Step 2: Use the `<Field />` component
60
+
61
+ ```jsx
62
+ import { Field } from 'formex-library';
63
+
64
+ // Inside your form component
65
+ <Field name="name">
66
+ {({ field }) => (
67
+ <input
68
+ {...field}
69
+ placeholder="Your name"
70
+ />
71
+ )}
72
+ </Field>
73
+
74
+ <Field name="email">
75
+ {({ field }) => (
76
+ <input
77
+ {...field}
78
+ type="email"
79
+ placeholder="Your email"
80
+ />
81
+ )}
82
+ </Field>
83
+
84
+ <button type="submit">Submit</button>
85
+ ```
86
+
87
+ ### Handling Submissions
88
+
89
+ Wrap your form inputs and submit button within a form element and pass the `submitForm` method from your form controller to the form's `onSubmit` event:
90
+
91
+ ```jsx
92
+ <form onSubmit={formController.handleSubmit}>
93
+ {/* Fields and submit button */}
94
+ </form>
95
+ ```
96
+
97
+ ## API Reference
98
+
99
+ ### `useCreateFormex`
100
+
101
+ Hook to create a form controller.
102
+
103
+ **Parameters**
104
+
105
+ - `initialValues`: An object with your form's initial values.
106
+ - `initialErrors` (optional): An object for any initial validation errors.
107
+ - `validation` (optional): A function for validating form data.
108
+ - `validateOnChange` (optional): If `true`, validates fields whenever they change.
109
+ - `onSubmit`: A function that fires when the form is submitted.
110
+
111
+
112
+ ### `<Field />`
113
+
114
+ A component used to render individual form fields.
115
+
116
+ **Props**
117
+
118
+ - `name`: The name of the form field.
119
+ - `as` (optional): The component or HTML tag that should be rendered. Defaults to `"input"`.
120
+ - `children`: A function that returns the field input component. Receives field props as its parameter.
121
+
122
+ **Example**
123
+
124
+ ```jsx
125
+ <Field name="username">
126
+ {({ field }) => <input {...field} />}
127
+ </Field>
128
+ ```
129
+
130
+ ## Customization
131
+
132
+ Formex is designed to be flexible. You can create custom field components, use any validation library, or integrate with UI component libraries.
133
+
134
+ ### Using with UI Libraries
135
+
136
+ ```jsx
137
+ import { Field } from 'formex-library';
138
+ import { TextField } from 'some-ui-library';
139
+
140
+ <Field name="username">
141
+ {({ field }) => (
142
+ <TextField {...field} label="Username" />
143
+ )}
144
+ </Field>
145
+ ```
146
+
147
+ ### Custom Validation
148
+
149
+ Leverage the `validation` function in `useCreateFormex` to integrate any validation logic or library.
150
+
151
+ ```jsx
152
+ const validate = values => {
153
+ const errors = {};
154
+ if (!values.email.includes('@')) {
155
+ errors.email = 'Invalid email';
156
+ }
157
+ return errors;
158
+ };
159
+ ```
160
+
161
+ ## Conclusion
162
+
163
+ Formex provides a simple yet powerful way to manage forms in React applications. It reduces the amount of boilerplate code needed and offers flexibility to work with custom components and validation strategies. Whether you are building simple or complex forms, Formex can help streamline your form management process.
164
+
165
+ For further examples and advanced usage, refer to the Formex documentation or source code.
@@ -0,0 +1,52 @@
1
+ import * as React from "react";
2
+ import { FormexController } from "./types";
3
+ export interface FieldInputProps<Value> {
4
+ /** Value of the field */
5
+ value: Value;
6
+ /** Name of the field */
7
+ name: string;
8
+ /** Multiple select? */
9
+ multiple?: boolean;
10
+ /** Is the field checked? */
11
+ checked?: boolean;
12
+ /** Change event handler */
13
+ onChange: (event: React.SyntheticEvent) => void;
14
+ /** Blur event handler */
15
+ onBlur: (event: React.FocusEvent) => void;
16
+ }
17
+ export interface FormexFieldProps<Value = any, FormValues extends object = any> {
18
+ field: FieldInputProps<Value>;
19
+ form: FormexController<FormValues>;
20
+ }
21
+ export interface FieldConfig<Value, C extends React.ElementType | undefined = undefined> {
22
+ /**
23
+ * Component to render. Can either be a string e.g. 'select', 'input', or 'textarea', or a component.
24
+ */
25
+ as?: C | string | React.ForwardRefExoticComponent<any>;
26
+ /**
27
+ * Children render function <Field name>{props => ...}</Field>)
28
+ */
29
+ children?: ((props: FormexFieldProps<Value>) => React.ReactNode) | React.ReactNode;
30
+ /**
31
+ * Validate a single field value independently
32
+ */
33
+ /**
34
+ * Used for 'select' and related input types.
35
+ */
36
+ multiple?: boolean;
37
+ /**
38
+ * Field name
39
+ */
40
+ name: string;
41
+ /** HTML input type */
42
+ type?: string;
43
+ /** Field value */
44
+ value?: any;
45
+ /** Inner ref */
46
+ innerRef?: (instance: any) => void;
47
+ }
48
+ export type FieldProps<T, C extends React.ElementType | undefined> = {
49
+ as?: C;
50
+ } & (C extends React.ElementType ? (React.ComponentProps<C> & FieldConfig<T, C>) : FieldConfig<T, C>);
51
+ export declare function Field<T, C extends React.ElementType | undefined = undefined>({ validate, name, children, as: is, // `as` is reserved in typescript lol
52
+ className, ...props }: FieldProps<T, C>): any;
@@ -0,0 +1,7 @@
1
+ import React from "react";
2
+ import { FormexController } from "./types";
3
+ export declare const useFormex: <T extends object>() => FormexController<T>;
4
+ export declare const Formex: ({ value, children }: {
5
+ value: FormexController<any>;
6
+ children: React.ReactNode;
7
+ }) => import("react/jsx-runtime").JSX.Element;
@@ -0,0 +1,5 @@
1
+ export * from "./Field";
2
+ export * from "./Formex";
3
+ export * from "./types";
4
+ export * from "./utils";
5
+ export * from "./useCreateFormex";