@ttoss/forms 0.22.1 → 0.22.3

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/README.md CHANGED
@@ -9,6 +9,8 @@ pnpm i @ttoss/forms @ttoss/react-i18n @ttoss/ui @emotion/react
9
9
  pnpm i --save-dev @ttoss/i18n-cli
10
10
  ```
11
11
 
12
+ Check the [@ttoss/react-i18n](https://ttoss.dev/docs/modules/packages/react-i18n/) docs to see how to configure the i18n.
13
+
12
14
  ## Quickstart
13
15
 
14
16
  ```tsx
@@ -154,7 +156,7 @@ const RenderForm = () => {
154
156
  };
155
157
  ```
156
158
 
157
- ## Yup Validation
159
+ ### Yup Validation
158
160
 
159
161
  You can also use yup and all of API from react-hook-form importing `import { yup, useForm } from @ttoss/forms`
160
162
 
@@ -222,3 +224,111 @@ const ComponentForm = () => {
222
224
  // ...
223
225
  };
224
226
  ```
227
+
228
+ ## @ttoss/forms/multistep-form
229
+
230
+ The `@ttoss/forms/multistep-form` module from the **@ttoss/forms** library provides an efficient and flexible way to create multistep forms in React. This component is ideal for scenarios where filling out a lengthy form needs to be divided into several smaller steps, improving user experience. With support for integrated validations and style customizations, this tool offers everything you need to implement robust multistep forms in your React applications.
231
+
232
+ ### How to Use
233
+
234
+ To use the `MultistepForm`, you first need to define the steps of the form, each with its own fields, validations, and messages. Here's a basic example of assembling a multistep form:
235
+
236
+ ```tsx
237
+ import * as React from 'react';
238
+ import { FormFieldInput, yup } from '@ttoss/forms';
239
+ import { MultistepForm } from '@ttoss/forms/multistep-form';
240
+
241
+ // Define your steps
242
+ const steps = [
243
+ {
244
+ label: 'Step 1',
245
+ question: 'What is your name?',
246
+ fields: <FormFieldInput name="name" label="Name" />,
247
+ schema: yup.object({
248
+ name: yup.string().required('Name is required'),
249
+ }),
250
+ },
251
+ {
252
+ label: 'Step 2',
253
+ question: 'How old are you?',
254
+ fields: <FormFieldInput type="number" name="age" label="Age" />,
255
+ defaultValues: {
256
+ age: 18,
257
+ },
258
+ schema: yup.object({
259
+ age: yup
260
+ .number()
261
+ .min(18, 'Min required age is 18')
262
+ .required('Age is required'),
263
+ }),
264
+ },
265
+ // Add more steps as needed
266
+ ];
267
+
268
+ const MyMultistepForm = () => {
269
+ return (
270
+ <MultistepForm
271
+ // ...other props
272
+ steps={steps}
273
+ // submit the full form on submit
274
+ onSubmit={(data) => console.log(data)}
275
+ />
276
+ );
277
+ };
278
+ ```
279
+
280
+ #### Props
281
+
282
+ The `MultistepForm` component accepts the following props:
283
+
284
+ - `steps`: An array of objects representing each step of the form.
285
+ - `onSubmit`: A function that is called when the form is completely filled and submitted.
286
+ - `footer`: An string with the text to show on form's footer.
287
+ - `header`: [Header Props](#header-props)
288
+
289
+ Each step can have the following properties:
290
+
291
+ - `label`: The label of the step (used for navigation).
292
+ - `question`: The question or instruction presented to the user at this step.
293
+ - `fields`: The form fields for this step.
294
+ - `schema`: A `yup` schema for validating the fields at this step.
295
+ - `defaultValues`: An optional object with default values to this step.
296
+
297
+ #### Header-Props
298
+
299
+ - **For Logo Header (`MultistepFormHeaderLogoProps`):**
300
+
301
+ - `variant`: Set to `'logo'`.
302
+ - `src`: The source URL for the logo image.
303
+ - `onClose`: A function to handle the close button click event.
304
+
305
+ - **For Titled Header (`MultistepFormTitledProps`):**
306
+ - `variant`: Set to `'titled'`.
307
+ - `title`: The title text.
308
+ - `leftIcon` and `rightIcon`: Icon types for left and right icons.
309
+ - `onLeftIconClick` and `onRightIconClick`: Functions to handle clicks on left and right icons.
310
+
311
+ #### Customizing Headers
312
+
313
+ 1. **Logo Header:**
314
+
315
+ ```tsx
316
+ const logoHeaderProps = {
317
+ variant: 'logo',
318
+ src: 'path-to-your-logo-image',
319
+ onClose: () => console.log('Close button clicked'),
320
+ };
321
+ ```
322
+
323
+ 2. **Titled Header:**
324
+
325
+ ```tsx
326
+ const titledHeaderProps = {
327
+ variant: 'titled',
328
+ title: 'Your Title',
329
+ leftIcon: 'icon-type',
330
+ rightIcon: 'icon-type',
331
+ onLeftIconClick: () => console.log('Left icon clicked'),
332
+ onRightIconClick: () => console.log('Right icon clicked'),
333
+ };
334
+ ```
@@ -0,0 +1,64 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import * as React from 'react';
3
+ import * as yup from 'yup';
4
+ import { IconType } from '@ttoss/react-icons';
5
+
6
+ type MultistepFlowMessageVariant = 'image-text' | 'heading-and-subheading';
7
+ type MultistepFlowMessageBase = {
8
+ variant: MultistepFlowMessageVariant;
9
+ };
10
+
11
+ type MultistepFlowMessageImageTextProps = MultistepFlowMessageBase & {
12
+ variant: 'image-text';
13
+ src: string;
14
+ description: string | React.ReactNode;
15
+ };
16
+
17
+ type MultistepFlowMessageProps = MultistepFlowMessageImageTextProps;
18
+
19
+ type MultistepFormStepperProps = {
20
+ flowMessage: MultistepFlowMessageProps;
21
+ onSubmit: (data: unknown) => void;
22
+ question: string;
23
+ isLastStep: boolean;
24
+ fields: React.ReactNode | React.ReactNode[];
25
+ schema?: yup.ObjectSchema<any>;
26
+ defaultValues?: any;
27
+ submitLabel: string;
28
+ stepNumber: number;
29
+ };
30
+
31
+ type MultistepHeaderTitledProps = {
32
+ variant: 'titled';
33
+ title: string;
34
+ leftIcon: IconType;
35
+ rightIcon: IconType;
36
+ onLeftIconClick: () => void;
37
+ onRightIconClick: () => void;
38
+ };
39
+ type MultistepHeaderLogoProps = {
40
+ variant: 'logo';
41
+ src: string;
42
+ onClose?: () => void;
43
+ };
44
+ type MultistepHeaderProps = MultistepHeaderLogoProps | MultistepHeaderTitledProps;
45
+
46
+ type MultistepStep = {
47
+ question: string;
48
+ flowMessage: MultistepFlowMessageProps;
49
+ label: string;
50
+ fields: React.ReactNode | React.ReactNode[];
51
+ schema?: MultistepFormStepperProps['schema'];
52
+ defaultValues?: MultistepFormStepperProps['defaultValues'];
53
+ };
54
+ type MultistepFormProps<FormValues = unknown> = {
55
+ header: MultistepHeaderProps;
56
+ steps: MultistepStep[];
57
+ footer?: string;
58
+ onSubmit: (data: FormValues) => void;
59
+ nextStepButtonLabel?: string;
60
+ submitButtonLabel?: string;
61
+ };
62
+ declare const MultistepForm: ({ nextStepButtonLabel, submitButtonLabel, ...props }: MultistepFormProps) => react_jsx_runtime.JSX.Element;
63
+
64
+ export { MultistepForm, type MultistepFormProps };
@@ -0,0 +1,64 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import * as React from 'react';
3
+ import * as yup from 'yup';
4
+ import { IconType } from '@ttoss/react-icons';
5
+
6
+ type MultistepFlowMessageVariant = 'image-text' | 'heading-and-subheading';
7
+ type MultistepFlowMessageBase = {
8
+ variant: MultistepFlowMessageVariant;
9
+ };
10
+
11
+ type MultistepFlowMessageImageTextProps = MultistepFlowMessageBase & {
12
+ variant: 'image-text';
13
+ src: string;
14
+ description: string | React.ReactNode;
15
+ };
16
+
17
+ type MultistepFlowMessageProps = MultistepFlowMessageImageTextProps;
18
+
19
+ type MultistepFormStepperProps = {
20
+ flowMessage: MultistepFlowMessageProps;
21
+ onSubmit: (data: unknown) => void;
22
+ question: string;
23
+ isLastStep: boolean;
24
+ fields: React.ReactNode | React.ReactNode[];
25
+ schema?: yup.ObjectSchema<any>;
26
+ defaultValues?: any;
27
+ submitLabel: string;
28
+ stepNumber: number;
29
+ };
30
+
31
+ type MultistepHeaderTitledProps = {
32
+ variant: 'titled';
33
+ title: string;
34
+ leftIcon: IconType;
35
+ rightIcon: IconType;
36
+ onLeftIconClick: () => void;
37
+ onRightIconClick: () => void;
38
+ };
39
+ type MultistepHeaderLogoProps = {
40
+ variant: 'logo';
41
+ src: string;
42
+ onClose?: () => void;
43
+ };
44
+ type MultistepHeaderProps = MultistepHeaderLogoProps | MultistepHeaderTitledProps;
45
+
46
+ type MultistepStep = {
47
+ question: string;
48
+ flowMessage: MultistepFlowMessageProps;
49
+ label: string;
50
+ fields: React.ReactNode | React.ReactNode[];
51
+ schema?: MultistepFormStepperProps['schema'];
52
+ defaultValues?: MultistepFormStepperProps['defaultValues'];
53
+ };
54
+ type MultistepFormProps<FormValues = unknown> = {
55
+ header: MultistepHeaderProps;
56
+ steps: MultistepStep[];
57
+ footer?: string;
58
+ onSubmit: (data: FormValues) => void;
59
+ nextStepButtonLabel?: string;
60
+ submitButtonLabel?: string;
61
+ };
62
+ declare const MultistepForm: ({ nextStepButtonLabel, submitButtonLabel, ...props }: MultistepFormProps) => react_jsx_runtime.JSX.Element;
63
+
64
+ export { MultistepForm, type MultistepFormProps };