datasync-dynamic-form 1.3.2 → 1.3.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.
@@ -8,7 +8,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  });
9
9
  };
10
10
  import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
11
- import React, { Component } from "react";
11
+ import React, { Component, useImperativeHandle, forwardRef } from "react";
12
12
  import Multiselect from 'multiselect-react-dropdown';
13
13
  import { CTimeStamp, Randomize, SaveDataTierToDatasync, WScrollTo } from "datasync-core";
14
14
  import { DsBlob } from "datasync-blob";
@@ -16,13 +16,10 @@ import { DsPdf } from "datasync-pdf";
16
16
  // reactstrap components
17
17
  import { Button, Label, FormGroup, Input, InputGroupText, InputGroup, Row as ReactstrapRow, Col as ReactstrapCol, } from "reactstrap";
18
18
  const debugging = false;
19
- const local = true;
20
- const production = !local;
21
19
  const globals = {
22
20
  parameters: {
23
21
  debugging: debugging,
24
22
  local: true,
25
- production: false,
26
23
  use_navigate: false,
27
24
  save_failed_alert: false,
28
25
  form_version: "1.0",
@@ -104,8 +101,6 @@ export class DsDynamicForm extends Component {
104
101
  let nextFieldError = this.state.fieldError || {};
105
102
  nextFieldError[pFieldObject.name] = value;
106
103
  this.setState({ fieldError: nextFieldError });
107
- //Scroll form to first erroneous field
108
- WScrollTo(pFieldObject.name);
109
104
  }
110
105
  catch (e) {
111
106
  console.error(`Error caught on ${e}`);
@@ -198,17 +193,21 @@ export class DsDynamicForm extends Component {
198
193
  event.preventDefault();
199
194
  //Force all fields check
200
195
  let canSubmit = true;
196
+ let firstErrFieldName = null;
201
197
  let iPage = 0;
202
- while ((iPage < this.state.form.Pages.length)
203
- && (canSubmit)) {
198
+ while (iPage < this.state.form.Pages.length) {
204
199
  let iRow = 0;
205
- while ((iRow < this.state.form.Pages[iPage].Rows.length)
206
- && (canSubmit)) {
200
+ while (iRow < this.state.form.Pages[iPage].Rows.length) {
207
201
  let iCol = 0;
208
- while ((iCol < this.state.form.Pages[iPage].Rows[iRow].Cols.length) && canSubmit) {
202
+ while (iCol < this.state.form.Pages[iPage].Rows[iRow].Cols.length) {
209
203
  let ii = 0;
210
- while ((ii < this.state.form.Pages[iPage].Rows[iRow].Cols[iCol].Fields.length)
211
- && (canSubmit && (canSubmit = this.checkValidation(this.state.form.Pages[iPage].Rows[iRow].Cols[iCol].Fields[ii])))) {
204
+ while (ii < this.state.form.Pages[iPage].Rows[iRow].Cols[iCol].Fields.length) {
205
+ const field = this.state.form.Pages[iPage].Rows[iRow].Cols[iCol].Fields[ii];
206
+ const isValid = this.checkValidation(field);
207
+ if (!isValid && firstErrFieldName === null) {
208
+ firstErrFieldName = field.name;
209
+ }
210
+ canSubmit = canSubmit && isValid;
212
211
  ii++;
213
212
  }
214
213
  iCol++;
@@ -218,6 +217,12 @@ export class DsDynamicForm extends Component {
218
217
  iPage++;
219
218
  }
220
219
  if (!canSubmit) {
220
+ // Scroll to the first field with error only on submit
221
+ if (firstErrFieldName) {
222
+ if (globals.parameters.debugging)
223
+ console.log("WScrollTo invoked :-)");
224
+ WScrollTo(firstErrFieldName);
225
+ }
221
226
  let err_message = "Le formulaire comporte des erreurs !";
222
227
  if (this.props.onFormFailed)
223
228
  this.props.onFormFailed(err_message);
@@ -482,7 +487,7 @@ export class DsDynamicForm extends Component {
482
487
  }
483
488
  };
484
489
  this.render = () => {
485
- return (_jsxs(_Fragment, { children: [_jsx(_Fragment, { children: console.log('form->', this.props.form) }), !this.state.form &&
490
+ return (_jsxs(_Fragment, { children: [_jsx(_Fragment, {}), !this.state.form &&
486
491
  _jsx("div", { children: _jsx("h1", { children: "Form loading..." }) }), !(this.state.form &&
487
492
  this.state.form.Pages) &&
488
493
  _jsx("div", { children: _jsx("h1", { children: "Form Missing Pages !!!" }) }), _jsx("form", { children: this.state.form &&
@@ -538,3 +543,19 @@ export class DsDynamicForm extends Component {
538
543
  }
539
544
  }
540
545
  }
546
+ // ForwardRef wrapper that exposes only the desired methods
547
+ export const DsDynamicFormForwardRef = forwardRef((props, ref) => {
548
+ const innerRef = React.useRef(null);
549
+ useImperativeHandle(ref, () => ({
550
+ clearForm: () => {
551
+ var _a;
552
+ (_a = innerRef.current) === null || _a === void 0 ? void 0 : _a.clearForm();
553
+ },
554
+ onClickSubmitFormHandler: (event) => {
555
+ return innerRef.current
556
+ ? innerRef.current.onClickSubmitFormHandler(event)
557
+ : Promise.resolve(false);
558
+ },
559
+ }));
560
+ return (_jsx(DsDynamicForm, Object.assign({}, props, { ref: innerRef })));
561
+ });
@@ -160,4 +160,9 @@ export declare class DsDynamicForm extends Component<DsDynamicFormProps, DsDynam
160
160
  _submitButton: () => JSX.Element;
161
161
  render: () => JSX.Element;
162
162
  }
163
+ export type DsDynamicFormRef = {
164
+ clearForm: () => void;
165
+ onClickSubmitFormHandler: (event?: React.MouseEvent<HTMLButtonElement>) => Promise<boolean>;
166
+ };
167
+ export declare const DsDynamicFormForwardRef: any;
163
168
  export {};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "datasync-dynamic-form",
3
3
  "description": "Datasync Dynamic Form component",
4
- "version": "1.3.2",
4
+ "version": "1.3.3",
5
5
  "main": "dist/DsDynamicForm.js",
6
6
  "module": "dist/DsDynamicForm.js",
7
7
  "types": "dist/types/DsDynamicForm.d.ts",