@luomus/laji-form 15.1.80 → 15.1.81

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.
@@ -12,6 +12,7 @@ export default class InputWithDefaultValueButtonWidget extends React.Component<a
12
12
  resultKey: PropTypes.Validator<string>;
13
13
  cache: PropTypes.Requireable<boolean>;
14
14
  }>>;
15
+ disableButtonAfterUse: PropTypes.Requireable<boolean>;
15
16
  onClick: PropTypes.Requireable<(...args: any[]) => any>;
16
17
  }>>>;
17
18
  }>>>;
@@ -21,11 +22,15 @@ export default class InputWithDefaultValueButtonWidget extends React.Component<a
21
22
  value: PropTypes.Requireable<string>;
22
23
  };
23
24
  constructor(props: any);
25
+ fetching: boolean;
26
+ disabled: boolean;
24
27
  state: {
25
28
  fetching: boolean;
29
+ disabled: boolean;
26
30
  };
27
31
  render(): JSX.Element;
28
32
  onClick: () => any;
33
+ changeValue: (value: any) => void;
29
34
  }
30
35
  import * as React from "react";
31
36
  import * as PropTypes from "prop-types";
@@ -41,26 +41,31 @@ const PropTypes = __importStar(require("prop-types"));
41
41
  const BaseInputTemplate_1 = __importDefault(require("../templates/BaseInputTemplate"));
42
42
  const ReactContext_1 = __importDefault(require("../../ReactContext"));
43
43
  const utils_1 = require("../../utils");
44
+ const react_spinner_1 = __importDefault(require("react-spinner"));
44
45
  class InputWithDefaultValueButtonWidget extends React.Component {
45
46
  constructor(props) {
46
47
  super(props);
47
48
  this.onClick = () => {
49
+ if (this.fetching || this.disabled) {
50
+ return;
51
+ }
48
52
  const { contextFieldForDefaultValue, apiQueryForDefaultValue, onClick } = (0, utils_1.getUiOptions)(this.props);
49
53
  if (contextFieldForDefaultValue) {
50
54
  const uiSchemaContext = this.props.formContext.uiSchemaContext || {};
51
55
  const defaultValue = uiSchemaContext[contextFieldForDefaultValue];
52
- this.props.onChange(defaultValue);
56
+ this.changeValue(defaultValue);
53
57
  }
54
58
  else if (apiQueryForDefaultValue) {
55
59
  const { path, query = {}, resultKey, cache = false } = apiQueryForDefaultValue;
56
60
  const apiClient = this.props.formContext.apiClient;
61
+ this.fetching = true;
57
62
  this.setState({ fetching: true });
58
- return apiClient.get(path, { query }, cache).then(result => {
59
- if (result[resultKey]) {
60
- this.props.onChange(result[resultKey]);
61
- }
62
- this.setState({ fetching: false });
63
+ return apiClient.get(path, { query: Object.assign({ value: this.props.value }, query) }, cache).then(result => {
64
+ result = resultKey ? result === null || result === void 0 ? void 0 : result[resultKey] : result;
65
+ this.changeValue(result);
63
66
  }).catch(() => {
67
+ }).finally(() => {
68
+ this.fetching = false;
64
69
  this.setState({ fetching: false });
65
70
  });
66
71
  }
@@ -68,7 +73,17 @@ class InputWithDefaultValueButtonWidget extends React.Component {
68
73
  onClick();
69
74
  }
70
75
  };
71
- this.state = { fetching: false };
76
+ this.changeValue = (value) => {
77
+ const { disableButtonAfterUse = false } = (0, utils_1.getUiOptions)(this.props);
78
+ this.props.onChange(value);
79
+ if (disableButtonAfterUse) {
80
+ this.disabled = true;
81
+ this.setState({ disabled: true });
82
+ }
83
+ };
84
+ this.fetching = false;
85
+ this.disabled = false;
86
+ this.state = { fetching: false, disabled: false };
72
87
  }
73
88
  render() {
74
89
  const { InputGroup, Button } = this.context.theme;
@@ -77,7 +92,9 @@ class InputWithDefaultValueButtonWidget extends React.Component {
77
92
  return (React.createElement(InputGroup, null,
78
93
  React.createElement(BaseInputTemplate_1.default, Object.assign({}, this.props)),
79
94
  React.createElement(InputGroup.Button, { className: "input-group-button" },
80
- React.createElement(Button, { onClick: this.onClick, disabled: disabled || readonly || this.state.fetching, variant: buttonVariant }, buttonLabel))));
95
+ React.createElement(Button, { onClick: this.onClick, disabled: disabled || readonly || this.state.fetching || this.state.disabled, variant: buttonVariant },
96
+ buttonLabel,
97
+ this.state.fetching && React.createElement(react_spinner_1.default, null)))));
81
98
  }
82
99
  }
83
100
  InputWithDefaultValueButtonWidget.contextType = ReactContext_1.default;
@@ -93,6 +110,7 @@ InputWithDefaultValueButtonWidget.propTypes = {
93
110
  resultKey: PropTypes.string.isRequired,
94
111
  cache: PropTypes.bool
95
112
  }),
113
+ disableButtonAfterUse: PropTypes.bool,
96
114
  onClick: PropTypes.func
97
115
  }).isRequired
98
116
  }).isRequired,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@luomus/laji-form",
3
- "version": "15.1.80",
3
+ "version": "15.1.81",
4
4
  "description": "React module capable of building dynamic forms from Laji form json schemas",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",