1.3.37 • Published 3 years ago

formwiz-publicweb-controls v1.3.37

Weekly downloads
10
License
ISC
Repository
-
Last release
3 years ago

Dynamic form builder

This is an dynamic form builder empowered FormWiz project.

How to use

  • Install peer dependencies:
npm install --save react react-dom react-redux redux moment
  • This package relies on redux-form so you need to install it (if you haven't):
npm install --save redux-form
  • Install it:
npm install --save formwiz-publicweb-controls
  • Load form options:

    • Since the form builder relies on redux, you can only load its rendered form's options to state and then the form will load them automatically.
    • First you need to register its store inredux's state tree:

      /**
       * rootReducer.js
       */
      
      import { formBuilder } from 'formwiz-publicweb-controls';
      const rootReducer = combineReducers({
        ...
        // Register formwiz-publicweb-controls's store
        formBuilder
        ...
      });
    • Then dispatch formOptionsUpdate action from inside your action for loading form's options:

      /**
       * actions.js
       */
      import { formOptionsUpdate } from 'formwiz-publicweb-controls';
      // Using redux-thunk (learn more at https://github.com/gaearon/redux-thunk)
      export const loadFormOptions = (payload: {
        uuid,
        data,
        currentSection
      }) => {
        return (
          dispatch,
          getState
        ) => {
          const { uuid, data, currentSection } = payload;
          fetch('http://www.my-api.com/my-form-options')
            .then(
              (formOptions) => {
                // Set uuid back to new form options
                formOptions.uuid = uuid; // optional
                // Update form options
                dispatch(formOptionsUpdate(uuid, formOptions));
              }
            )
        };
      };
  • Load default form data:

    /**
     * actions.js
     */
    import { formOptionsUpdate, fillData } from 'formwiz-publicweb-controls';
    // Using redux-thunk (learn more at https://github.com/gaearon/redux-thunk)
    export const loadFormOptions = (payload: {
      uuid,
      data,
      currentSection
    }) => {
      return (
        dispatch,
        getState
      ) => {
        const { uuid, data, currentSection } = payload;
        fetch('http://www.my-api.com/my-form-options')
          .then(
            (formOptions) => {
              // Set uuid back to new form options
              formOptions.uuid = uuid; // optional
              // Assume you already have form data stored in `reduxStore.formDataState`
              const {
                formDataState,
                currentSectionState
              } = getState().application;
    
              // Get current form data state
              const formData = formDataState[formOptions.uuid];
              if (formData) {
                // fill data in current form
                formOptions = fillData(
                  dispatch,
                  formOptions,
                  formData,
                  currentSection,
                  formMeta.isSubmitted
                );
              } else {
                // Activate first section
                formOptions.sections = formOptions.sections.map(
                  (section, index) => ({
                    ...section,
                    isActive:
                      currentSection && currentSectionState
                        ? currentSection.uuid === currentSectionState.uuid
                        : index === 0
                  })
                );
              }
              // Update form options
              dispatch(formOptionsUpdate(uuid, formOptions));
            }
          )
      };
    };
  • Submit form data:

    /**
     * actions.js
     */
    import { formSubmit } from 'formwiz-publicweb-controls';
    export const submit = ({ formUUID, formModel }) =>
      formSubmit(
        formUUID,
        // onSubmitAction
        // Call API to submit form model
        () =>
          fetch('http://www.my-api.com/my-form-data', {
            method: 'POST', // or 'PUT'
            body: JSON.stringify(formModel),
            headers: new Headers({
              'Content-Type': 'application/json'
            })
          })
        // onStartAction?: Function
        undefined,
        // onErrorAction?: Function,
        undefined,
        // onSuccessAction?: Function,
        undefined,
        // onEndAction?: Function,
        undefined,
        // onCatchAction?: Function
      );
  • Import and then render it:

    /**
     * MyFormComponent.js
     */
    import React from 'react';
    import { ReduxDynamicFormBuilder } from 'formwiz-publicweb-controls';
    
    class MyFormComponent extends React.Component {
      ...
      render() {
        const DynamicFormBuilder = ReduxDynamicFormBuilder({
          // Form's UUID
          form: 'MyDynamicForm',
          // Other `redux-form` configurations
          ...
        });
        return (<DynamicFormBuilder />);
      }
    }
  • Advance configurations:

    • Validation:

      /**
        * MyFormComponent.js
        */
      import React from 'react';
      import { ReduxDynamicFormBuilder } from 'formwiz-publicweb-controls';
      
      class MyFormComponent extends React.Component {
        ...
        render() {
          const DynamicFormBuilder = ReduxDynamicFormBuilder({
            // Form's UUID
            form: 'MyDynamicForm',
            // Other `redux-form` configurations
            ...
          });
          return (<DynamicFormBuilder onValidate={(fieldName, errors, form) => {
            // Validating field's name
            console.log(fieldName);
            // Validation's errors
            console.log(errors);
            // Rendered form's ref object
            console.log(form);
          }}/>);
        }
      }
    • Localization

      • Date time:

        • This package use moment as a date parser.
        • You can change its locale via adding moment.locale('<LOCALE>'); anywhere in your app, as long as it's executed before rendering the form.
        • Best practice is to add it in app.js - where you bootstrapped your root React component.
        /**
         * app.js
         */
        import React from 'react';
        import { render } from 'react-dom';
        import moment from 'moment';
        import 'moment/locale/hr';
        
        moment.locale('en');
        const App = props => (
          ...
        );
        ...
        render(
          <App />,
          document.querySelector('#root')
        );
    • Other examples: COMING SOON.

  • API docs:

Contribution Guide

Note: This package is currently receives contribution only from FormWiz team members. We're sorry about that.

  • Clone FormWiz project repo.
  • Run npm install and npm run bootstrap at repo's root folder. This will run lerna's bootstrap commands.
  • Navigate to cloned-repo/src/FormWiz.DynamicFormBuilder in terminal.
  • Run npm start. This will build formwiz-publicweb-controls package, copy it to packages which depends on it in this project and then watch for formwiz-publicweb-controls's changes.
1.3.37

3 years ago

1.3.36

3 years ago

1.3.35

3 years ago

1.3.34

3 years ago

1.3.33

3 years ago

1.3.32

3 years ago

1.3.31

4 years ago

1.3.30

4 years ago

1.3.29

4 years ago

1.3.28

4 years ago

1.3.27

4 years ago

1.3.26

5 years ago

1.3.25

5 years ago

1.3.24

5 years ago

1.3.23

5 years ago

1.3.22

5 years ago

1.3.21

5 years ago

1.3.20

5 years ago

1.3.19

5 years ago

1.3.18

5 years ago

1.3.17

5 years ago

1.3.16

5 years ago

1.3.15

5 years ago

1.3.14

5 years ago

1.3.13

5 years ago

1.3.12

5 years ago

1.3.11

6 years ago

1.3.10

6 years ago

1.3.9

6 years ago

1.3.8

6 years ago

1.3.7

6 years ago

1.3.6

6 years ago

1.3.5

6 years ago

1.3.4

6 years ago

1.3.3

6 years ago

1.3.2

6 years ago

1.3.1

6 years ago

1.2.18

6 years ago

1.2.17

6 years ago

1.2.16

6 years ago

1.2.15

6 years ago

1.2.14

6 years ago

1.2.13

6 years ago

1.2.12

6 years ago

1.2.11

6 years ago

1.2.10

6 years ago

1.2.9

6 years ago

1.2.8

6 years ago

1.2.7

6 years ago

1.2.6

6 years ago

1.2.5

6 years ago

1.2.4

6 years ago

1.2.3

6 years ago

1.2.2

6 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.38

6 years ago

1.1.37

6 years ago

1.1.36

6 years ago

1.1.35

6 years ago

1.1.34

6 years ago

1.1.33

6 years ago

1.1.32

6 years ago

1.1.31

6 years ago

1.1.30

6 years ago

1.1.29

6 years ago

1.1.28

6 years ago

1.1.27

6 years ago

1.1.26

6 years ago

1.1.25

6 years ago

1.1.24

6 years ago

1.1.23

6 years ago

1.1.22

6 years ago

1.1.21

6 years ago

1.1.20

6 years ago

1.1.19

6 years ago

1.1.18

6 years ago

1.1.17

6 years ago

1.1.16

6 years ago

1.1.15

6 years ago

1.1.14

6 years ago

1.1.13

6 years ago

1.1.12

6 years ago

1.1.11

6 years ago

1.1.10

6 years ago

1.1.9

6 years ago

1.1.8

6 years ago

1.1.7

6 years ago

1.1.6

6 years ago

1.1.5

6 years ago

1.1.4

6 years ago

1.1.3

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.0.69

6 years ago

1.0.68

6 years ago

1.0.67

6 years ago

1.0.66

6 years ago

1.0.65

6 years ago

1.0.64

6 years ago

1.0.63

6 years ago

1.0.62

6 years ago

1.0.61

6 years ago

1.0.60

6 years ago

1.0.59

6 years ago

1.0.58

6 years ago

1.0.57

6 years ago

1.0.56

6 years ago

1.0.55

6 years ago

1.0.54

6 years ago

1.0.53

6 years ago

1.0.52

6 years ago

1.0.51

6 years ago

1.0.50

6 years ago

1.0.49

6 years ago

1.0.48

6 years ago

1.0.47

6 years ago

1.0.46

6 years ago

1.0.44

6 years ago

1.0.43

6 years ago

1.0.42

6 years ago

1.0.41

6 years ago

1.0.40

6 years ago

1.0.39

6 years ago

1.0.38

6 years ago

1.0.37

6 years ago

1.0.36

6 years ago

1.0.35

6 years ago

1.0.34

6 years ago

1.0.33

6 years ago

1.0.32

6 years ago

1.0.31

6 years ago

1.0.30

6 years ago

1.0.29

6 years ago

1.0.28

6 years ago

1.0.27

6 years ago

1.0.26

6 years ago

1.0.25

6 years ago

1.0.24

6 years ago

1.0.23

6 years ago

1.0.22

6 years ago

1.0.21

6 years ago

1.0.20

6 years ago

1.0.19

6 years ago

1.0.18

6 years ago

1.0.17

6 years ago

1.0.16

6 years ago

1.0.15

6 years ago

1.0.14

6 years ago

1.0.13

6 years ago

1.0.12

6 years ago

1.0.11

6 years ago

1.0.10

6 years ago

1.0.9

6 years ago

1.0.8

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago