1.0.32 • Published 9 months ago

craft-ux v1.0.32

Weekly downloads
-
License
-
Repository
-
Last release
9 months ago

craft-ux

craft-ux is a dynamic form-building library that provides components to create and manage form workflows efficiently. Built for React and powered by Redux, craft-ux allows you to build highly customizable and scalable forms, ideal for use cases in fintech, loan applications, and beyond.

Features

  • Dynamic Forms: Easily create dynamic forms using JSON configurations.
  • Repeatable Sections: Support for repeatable sections, allowing you to add multiple blocks of fields like "Shareholder" or "Applicant" entries.
  • Conditional Logic: Implement dynamic field visibility and validation rules based on form inputs.
  • Redux Integration: Leverage Redux Toolkit for centralized form state management, making it easy to integrate with larger applications.
  • Field Dependencies: Define dependencies between fields for scenarios like dropdown value changes based on other fields.
  • Custom Stepper: Seamlessly incorporate custom steppers for multi-stage forms to improve user navigation.
  • External Form Submission: Trigger form submission externally using a reference to the DynamicForm component.

Installation

Install the package using npm or yarn:

npm install craft-ux

or

yarn add craft-ux

Usage

Here's a basic example of how to use DynamicForm from craft-ux:

import React from 'react';
import { DynamicForm } from 'craft-ux';

const formDefinition = {
  fields: [
    { name: 'business_name', type: 'text', label: 'Business Name', required: true },
    { name: 'applicant_name', type: 'text', label: 'Applicant Name', required: true },
    { name: 'loan_type', type: 'dropdown', label: 'Loan Type', options: [
      { label: 'Home Loan', value: 'home' },
      { label: 'Car Loan', value: 'car' },
    ]},
  ],
};

const App = () => {
  return (
    <DynamicForm formDefinition={formDefinition} />
  );
};

export default App;

Advanced Usage

Handling Form Submission

You can use the exported handleDynamicFormSubmit to handle form submissions outside of the DynamicForm component:

import React from 'react';
import { DynamicForm, handleDynamicFormSubmit } from 'craft-ux';
import { useDispatch } from 'react-redux';

const App = () => {
  const dispatch = useDispatch();

  const onSubmit = () => {
    dispatch(handleDynamicFormSubmit()).then((data) => {
      // Handle form submission data here
      console.log(data);
    });
  };

  return (
    <>
      <DynamicForm formDefinition={formDefinition} />
      <button onClick={onSubmit}>Submit</button>
    </>
  );
};

export default App;

External Form Submission with Ref

You can also trigger form submission externally using a reference to the DynamicForm component:

import React, { useRef, useState } from 'react';
import { Provider, DynamicForm, AxiosProvider } from 'craft-ux';

const formJson = {
  "sections": [
    {
      "title": "Pokemon Information",
      "fields": [
        {
          "name": "primary.pokemon.name",
          "label": "Pokemon Name",
          "fieldType": "text",
          "validation": {
            "required": true
          }
        },
        {
          "name": "primary.pokemon.type",
          "label": "Pokemon Type",
          "fieldType": "dropdown",
          "source": {
            "api": "https://pokeapi.co/api/v2/pokemon?offset=20&limit=20",
            "labelKey": "name",
            "valueKey": "name"
          },
          "validation": {
            "required": true
          }
        }
      ]
    }
  ]
};

const App = () => {
  const dynamicFormRef = useRef(null);
  const [submittedData, setSubmittedData] = useState(null);

  const handleFormSubmitSuccess = (data) => {
    console.log('Form submitted successfully with data:', data);
    setSubmittedData(data);
  };

  const triggerFormSubmit = () => {
    if (dynamicFormRef.current) {
      dynamicFormRef.current.submitFormExternally();
    }
  };

  return (
    <Provider>
      <div>
        <h1>Using Craft UX Library</h1>
        <AxiosProvider axiosInstance={axiosInstance}>
          <DynamicForm
            ref={dynamicFormRef}
            componentName="TestForm"
            formJson={formJson}
            onSubmitSuccess={handleFormSubmitSuccess}
          />
        </AxiosProvider>
        <button onClick={triggerFormSubmit}>Submit Form Externally</button>
        {submittedData && <pre>{JSON.stringify(submittedData, null, 2)}</pre>}
      </div>
    </Provider>
  );
};

export default App;

Dynamic Sections

The craft-ux library allows for adding new sections dynamically using repeatable fields. For example, adding multiple "Co-Applicants" or "Guarantors" can be easily managed through the repeatable configuration.

{
  "name": "co_applicant",
  "type": "section",
  "repeatable": true,
  "fields": [
    { "name": "name", "type": "text", "label": "Co-Applicant Name" },
    { "name": "relationship", "type": "dropdown", "label": "Relationship", "options": [
      { "label": "Spouse", "value": "spouse" },
      { "label": "Sibling", "value": "sibling" }
    ]}
  ]
}

API Reference

  • DynamicForm: The core form component that takes formDefinition or formJson as a prop.
  • handleDynamicFormSubmit: Redux action to handle form submission.
  • fetchDynamicFormData: Redux asyncThunk to load form data dynamically.
  • submitFormExternally: Method to trigger form submission from outside the component using a reference.

Props for DynamicForm

  • formDefinition or formJson: JSON object defining the form fields and their configurations.
  • onSubmit (optional): Callback to handle form submission when a submit button is clicked.
  • onSubmitSuccess (optional): Callback to handle successful form submission.

Contributing

Contributions are welcome! Feel free to open issues or submit pull requests.

License

This project is licensed under the MIT License.


Feel free to modify the examples to suit your needs, and let us know how craft-ux helps in building dynamic forms for your projects!

1.0.32

9 months ago

1.0.31

9 months ago

1.0.30

10 months ago

1.0.29

11 months ago

1.0.28

11 months ago

1.0.27

11 months ago

1.0.26

11 months ago

1.0.25

12 months ago

1.0.24

12 months ago

1.0.22

1 year ago

1.0.23

12 months ago

1.0.19

1 year ago

1.0.18

1 year ago

1.0.17

1 year ago

1.0.16

1 year ago

1.0.21

1 year ago

1.0.20

1 year ago

1.0.15

1 year ago

1.0.14

1 year ago

1.0.13

1 year ago

1.0.12

1 year ago

1.0.11

1 year ago

1.0.10

1 year ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago