datasync-dynamic-form 1.3.2 → 1.3.4

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);
@@ -389,8 +394,9 @@ export class DsDynamicForm extends Component {
389
394
  }
390
395
  };
391
396
  this._colRendering = (col) => {
397
+ var _a, _b, _c, _d;
392
398
  try {
393
- return (_jsx(ReactstrapCol, { xs: "12", sm: "12", md: "6", lg: "4", id: `${col.Id ? col.Id : ''}`, children: col.Fields.map((field, ii) => {
399
+ return (_jsx(ReactstrapCol, { xs: (_a = this.props.col_xs) !== null && _a !== void 0 ? _a : 12, sm: (_b = this.props.col_sm) !== null && _b !== void 0 ? _b : 12, md: (_c = this.props.col_md) !== null && _c !== void 0 ? _c : 6, lg: (_d = this.props.col_lg) !== null && _d !== void 0 ? _d : 4, id: `${col.Id ? col.Id : ''}`, children: col.Fields.map((field, ii) => {
394
400
  return this._fieldRendering(field, ii);
395
401
  }) }));
396
402
  }
@@ -464,25 +470,28 @@ export class DsDynamicForm extends Component {
464
470
  }
465
471
  };
466
472
  this._cancelButton = () => {
473
+ var _a, _b, _c, _d;
467
474
  let buttonIsHidden = (this.props.read_only || (this.props.buttons_options && this.props.buttons_options.cancel_button_hidden)) ? true : false;
468
475
  let buttonCaption = (this.props.buttons_options && this.props.buttons_options.cancel_button_caption) ? this.props.buttons_options.cancel_button_caption : "Annuler";
469
476
  if (buttonIsHidden)
470
477
  return (_jsx(_Fragment, {}));
471
478
  else {
472
- return (_jsx(ReactstrapCol, { xs: "12", sm: "12", md: "6", lg: "4", children: _jsx(Button, { id: "dsdf_clear_button", block: true, className: "btn-round", color: "danger", outline: true, type: "reset", onClick: () => { this.clearForm(); }, children: buttonCaption }) }));
479
+ return (_jsx(ReactstrapCol, { xs: (_a = this.props.col_xs) !== null && _a !== void 0 ? _a : 12, sm: (_b = this.props.col_sm) !== null && _b !== void 0 ? _b : 12, md: (_c = this.props.col_md) !== null && _c !== void 0 ? _c : 6, lg: (_d = this.props.col_lg) !== null && _d !== void 0 ? _d : 4, children: _jsx(Button, { id: "dsdf_clear_button", block: true, className: "btn-round", color: "danger", outline: true, type: "reset", onClick: () => { this.clearForm(); }, children: buttonCaption }) }));
473
480
  }
474
481
  };
475
482
  this._submitButton = () => {
483
+ var _a, _b, _c, _d;
476
484
  let buttonIsHidden = (this.props.read_only || (this.props.buttons_options && this.props.buttons_options.submit_button_hidden)) ? true : false;
477
485
  let buttonCaption = (this.props.buttons_options && this.props.buttons_options.submit_button_caption) ? this.props.buttons_options.submit_button_caption : "Soumettre";
478
486
  if (buttonIsHidden)
479
487
  return (_jsx(_Fragment, {}));
480
488
  else {
481
- return (_jsx(ReactstrapCol, { xs: "12", sm: "12", md: "6", lg: "4", children: _jsx(Button, { id: "dsdf_submit_button", block: true, className: "btn-round", color: "danger", outline: true, type: "submit", onClick: () => { this.onClickSubmitFormHandler(); }, children: buttonCaption }) }));
489
+ return (_jsx(ReactstrapCol, { xs: (_a = this.props.col_xs) !== null && _a !== void 0 ? _a : 12, sm: (_b = this.props.col_sm) !== null && _b !== void 0 ? _b : 12, md: (_c = this.props.col_md) !== null && _c !== void 0 ? _c : 6, lg: (_d = this.props.col_lg) !== null && _d !== void 0 ? _d : 4, children: _jsx(Button, { id: "dsdf_submit_button", block: true, className: "btn-round", color: "danger", outline: true, type: "submit", onClick: () => { this.onClickSubmitFormHandler(); }, children: buttonCaption }) }));
482
490
  }
483
491
  };
484
492
  this.render = () => {
485
- return (_jsxs(_Fragment, { children: [_jsx(_Fragment, { children: console.log('form->', this.props.form) }), !this.state.form &&
493
+ var _a, _b, _c, _d, _e, _f, _g, _h;
494
+ return (_jsxs(_Fragment, { children: [_jsx(_Fragment, {}), !this.state.form &&
486
495
  _jsx("div", { children: _jsx("h1", { children: "Form loading..." }) }), !(this.state.form &&
487
496
  this.state.form.Pages) &&
488
497
  _jsx("div", { children: _jsx("h1", { children: "Form Missing Pages !!!" }) }), _jsx("form", { children: this.state.form &&
@@ -494,11 +503,11 @@ export class DsDynamicForm extends Component {
494
503
  return (_jsx("div", { children: this._rowRendering(row) }, rowIndex));
495
504
  }) }, pageIndex));
496
505
  }) }), _jsxs(ReactstrapRow, { id: "dsdf_buttons_container", className: "buttons-row", children: [this._cancelButton(), this._submitButton(), this.props.custom_button_caption &&
497
- _jsx(ReactstrapCol, { xs: "12", sm: "12", md: "6", lg: "4", children: _jsx(Button, { id: "dsdf_custom_button", block: true, className: "btn-round", color: "primary", type: "submit", onClick: (e) => { if (this.props.custom_button_handler)
506
+ _jsx(ReactstrapCol, { xs: (_a = this.props.col_xs) !== null && _a !== void 0 ? _a : 12, sm: (_b = this.props.col_sm) !== null && _b !== void 0 ? _b : 12, md: (_c = this.props.col_md) !== null && _c !== void 0 ? _c : 6, lg: (_d = this.props.col_lg) !== null && _d !== void 0 ? _d : 4, children: _jsx(Button, { id: "dsdf_custom_button", block: true, className: "btn-round", color: "primary", type: "submit", onClick: (e) => { if (this.props.custom_button_handler)
498
507
  this.props.custom_button_handler(this.state.form);
499
508
  else
500
509
  console.error("custom_button_handler unset !"); }, children: this.props.custom_button_caption }) }), this.props.custom_button_caption2 &&
501
- _jsx(ReactstrapCol, { xs: "12", sm: "12", md: "6", lg: "4", children: _jsx(Button, { id: "dsdf_custom_button2", block: true, className: "btn-round", color: "primary", type: "submit", onClick: (e) => { if (this.props.custom_button_handler2)
510
+ _jsx(ReactstrapCol, { xs: (_e = this.props.col_xs) !== null && _e !== void 0 ? _e : 12, sm: (_f = this.props.col_sm) !== null && _f !== void 0 ? _f : 12, md: (_g = this.props.col_md) !== null && _g !== void 0 ? _g : 6, lg: (_h = this.props.col_lg) !== null && _h !== void 0 ? _h : 4, children: _jsx(Button, { id: "dsdf_custom_button2", block: true, className: "btn-round", color: "primary", type: "submit", onClick: (e) => { if (this.props.custom_button_handler2)
502
511
  this.props.custom_button_handler2(this.state.form);
503
512
  else
504
513
  console.error("custom_button_handler2 unset !"); }, children: this.props.custom_button_caption2 }) })] })] }));
@@ -538,3 +547,19 @@ export class DsDynamicForm extends Component {
538
547
  }
539
548
  }
540
549
  }
550
+ // ForwardRef wrapper that exposes only the desired methods
551
+ export const DsDynamicFormForwardRef = forwardRef((props, ref) => {
552
+ const innerRef = React.useRef(null);
553
+ useImperativeHandle(ref, () => ({
554
+ clearForm: () => {
555
+ var _a;
556
+ (_a = innerRef.current) === null || _a === void 0 ? void 0 : _a.clearForm();
557
+ },
558
+ onClickSubmitFormHandler: (event) => {
559
+ return innerRef.current
560
+ ? innerRef.current.onClickSubmitFormHandler(event)
561
+ : Promise.resolve(false);
562
+ },
563
+ }));
564
+ return (_jsx(DsDynamicForm, Object.assign({}, props, { ref: innerRef })));
565
+ });
@@ -97,6 +97,10 @@ interface DsDynamicFormProps {
97
97
  custom_button_handler2?: (form: Form) => void;
98
98
  show_hidden?: boolean;
99
99
  active_page?: number;
100
+ col_xs?: number | string;
101
+ col_sm?: number | string;
102
+ col_md?: number | string;
103
+ col_lg?: number | string;
100
104
  }
101
105
  interface DsDynamicFormState {
102
106
  form: Form;
@@ -160,4 +164,9 @@ export declare class DsDynamicForm extends Component<DsDynamicFormProps, DsDynam
160
164
  _submitButton: () => JSX.Element;
161
165
  render: () => JSX.Element;
162
166
  }
167
+ export type DsDynamicFormRef = {
168
+ clearForm: () => void;
169
+ onClickSubmitFormHandler: (event?: React.MouseEvent<HTMLButtonElement>) => Promise<boolean>;
170
+ };
171
+ export declare const DsDynamicFormForwardRef: any;
163
172
  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.4",
5
5
  "main": "dist/DsDynamicForm.js",
6
6
  "module": "dist/DsDynamicForm.js",
7
7
  "types": "dist/types/DsDynamicForm.d.ts",