@gpa-gemstone/react-forms 1.1.78 → 1.1.80

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.
@@ -1,7 +1,7 @@
1
1
  import * as React from 'react';
2
2
  import { Gemstone } from '@gpa-gemstone/application-typings';
3
3
  interface IOption {
4
- Value: string;
4
+ Value: string | number;
5
5
  Label: string;
6
6
  }
7
7
  interface IProps<T> extends Gemstone.TSX.Interfaces.IBaseFormProps<T> {
@@ -16,7 +16,7 @@ interface IProps<T> extends Gemstone.TSX.Interfaces.IBaseFormProps<T> {
16
16
  * @param search - Search string
17
17
  * @returns {[promise: Promise<IOption[]>, callback?: () => void]}
18
18
  */
19
- Search: (search: string) => [promise: Promise<IOption[]>, callback?: () => void];
19
+ Search: (search: string) => [Promise<IOption[]>, () => void];
20
20
  /**
21
21
  * CSS styles to apply to the form group
22
22
  * @type {React.CSSProperties}
@@ -27,10 +27,9 @@ interface IProps<T> extends Gemstone.TSX.Interfaces.IBaseFormProps<T> {
27
27
  * CSS style to apply to the button holding the selected value
28
28
  * @type {React.CSSProperties}
29
29
  * @optional
30
- *
31
30
  */
32
31
  BtnStyle?: React.CSSProperties;
33
- SearchLabel?: string;
32
+ GetLabel?: () => [Promise<string>, () => void];
34
33
  }
35
34
  export default function SearchableSelect<T>(props: IProps<T>): JSX.Element;
36
35
  export {};
@@ -38,57 +38,71 @@ var React = require("react");
38
38
  var StylableSelect_1 = require("./StylableSelect");
39
39
  var gpa_symbols_1 = require("@gpa-gemstone/gpa-symbols");
40
40
  function SearchableSelect(props) {
41
- var _a, _b, _c;
42
- var _d = React.useState((_c = (_b = ((_a = props.SearchLabel) !== null && _a !== void 0 ? _a : props.Record[props.Field])) === null || _b === void 0 ? void 0 : _b.toString()) !== null && _c !== void 0 ? _c : ''), search = _d[0], setSearch = _d[1];
43
- var _e = React.useState([]), results = _e[0], setResults = _e[1];
44
- var _f = React.useState(false), loading = _f[0], setLoading = _f[1];
41
+ var _a = React.useState(props.Record[props.Field].toString()), search = _a[0], setSearch = _a[1];
42
+ var _b = React.useState(props.Record[props.Field].toString()), label = _b[0], setLabel = _b[1];
43
+ var _c = React.useState([]), results = _c[0], setResults = _c[1];
44
+ var _d = React.useState(false), loading = _d[0], setLoading = _d[1];
45
+ React.useEffect(function () {
46
+ if (props.GetLabel === undefined)
47
+ setLabel(props.Record[props.Field].toString());
48
+ else {
49
+ setLoading(true);
50
+ var _a = props.GetLabel(), handle = _a[0], callback = _a[1];
51
+ handle.then(function (lab) {
52
+ setLabel(lab);
53
+ setSearch(lab);
54
+ setLoading(false);
55
+ });
56
+ return callback;
57
+ }
58
+ }, [props.GetLabel]);
45
59
  React.useEffect(function () {
46
60
  setLoading(true);
47
- var _a = props.Search(search), h = _a[0], c = _a[1];
48
- h.then(function (d) {
49
- setResults(d.map(function (o) { return ({ Value: o.Value, Element: React.createElement("p", null, o.Label) }); }));
61
+ var _a = props.Search(search), handle = _a[0], callback = _a[1];
62
+ handle.then(function (d) {
63
+ setResults(d.map(function (o) { return ({ Value: o.Value, Element: o.Label }); }));
50
64
  setLoading(false);
51
65
  });
52
- return c;
66
+ return callback;
53
67
  }, [search]);
54
- React.useEffect(function () {
55
- if (props.SearchLabel == null)
56
- return;
57
- setSearch(props.SearchLabel);
58
- }, [props.SearchLabel]);
68
+ var handleOnBlur = React.useCallback(function () {
69
+ setSearch(label);
70
+ }, [label]);
59
71
  var options = React.useMemo(function () {
60
- var _a, _b, _c, _d;
72
+ var _a;
61
73
  var ops = [];
62
74
  ops.push({
63
- Value: props.Record[props.Field], Element: React.createElement("div", { className: 'input-group' },
64
- React.createElement("input", { type: "text", className: "form-control", value: search, onChange: function (d) { return setSearch(d.target.value); }, onBlur: (((_a = props.AllowCustom) !== null && _a !== void 0 ? _a : false) ? function () {
65
- var _a;
66
- return props.Setter(__assign(__assign({}, props.Record), (_a = {}, _a[props.Field] = search, _a)));
67
- } : function () { var _a; return setSearch((_a = props.SearchLabel) !== null && _a !== void 0 ? _a : props.Record[props.Field].toString()); }) }),
75
+ Value: props.Record[props.Field],
76
+ Element: React.createElement("div", { className: 'input-group' },
77
+ React.createElement("input", { type: "text", className: "form-control", value: search, onChange: function (d) { return setSearch(d.target.value); }, onBlur: handleOnBlur }),
68
78
  loading ?
69
79
  React.createElement("div", { className: "input-group-append" },
70
80
  React.createElement("span", { className: "input-group-text" },
71
81
  React.createElement(gpa_symbols_1.ReactIcons.SpiningIcon, null)))
72
82
  : null)
73
83
  });
74
- if (!((_b = props.AllowCustom) !== null && _b !== void 0 ? _b : false))
75
- ops.push({ Value: 'search-' + props.Record[props.Field], Element: React.createElement("p", null, (_c = props.SearchLabel) !== null && _c !== void 0 ? _c : props.Record[props.Field]) });
76
- if ((_d = props.AllowCustom) !== null && _d !== void 0 ? _d : false)
77
- ops.push({ Value: search, Element: React.createElement("p", null, search) });
84
+ if (!((_a = props.AllowCustom) !== null && _a !== void 0 ? _a : false))
85
+ ops.push({ Value: 'search-' + props.Record[props.Field], Element: label });
86
+ else
87
+ ops.push({ Value: search, Element: search });
78
88
  ops.push.apply(ops, results.filter(function (f) { return f.Value !== search && f.Value !== props.Record[props.Field]; }));
79
89
  return ops;
80
- }, [search, props.Record[props.Field], props.Disabled, loading, props.SearchLabel, results]);
81
- var update = React.useCallback(function (record) {
90
+ }, [search, props.Record[props.Field], results, props.Disabled, loading, label, props.AllowCustom, handleOnBlur]);
91
+ var update = React.useCallback(function (record, selectedOption) {
82
92
  var _a;
83
- var _b, _c;
84
- if (record[props.Field].toString().startsWith('search-')) {
85
- var value = record[props.Field].toString().replace('search-', '');
93
+ var stringVal = record[props.Field].toString();
94
+ var newLabel = stringVal;
95
+ if (!React.isValidElement(selectedOption.Element))
96
+ newLabel = selectedOption.Element;
97
+ setLabel(newLabel);
98
+ if (stringVal.startsWith('search-')) {
99
+ var value = stringVal.replace('search-', '');
86
100
  props.Setter(__assign(__assign({}, record), (_a = {}, _a[props.Field] = value, _a)));
87
- setSearch((_b = props.SearchLabel) !== null && _b !== void 0 ? _b : value);
101
+ setSearch(newLabel !== null && newLabel !== void 0 ? newLabel : value);
88
102
  return;
89
103
  }
90
104
  props.Setter(record);
91
- setSearch((_c = props.SearchLabel) !== null && _c !== void 0 ? _c : record[props.Field].toString());
92
- }, [props.Setter, props.Field, props.SearchLabel]);
105
+ setSearch(newLabel);
106
+ }, [props.Setter, props.Field, label]);
93
107
  return React.createElement(StylableSelect_1.default, { Record: props.Record, Field: props.Field, Setter: update, Label: props.Label, Disabled: props.Disabled, Help: props.Help, Style: props.Style, Options: options, BtnStyle: props.BtnStyle });
94
108
  }
@@ -1,13 +1,45 @@
1
1
  import * as React from 'react';
2
- import { Gemstone } from '@gpa-gemstone/application-typings';
3
2
  export interface IOption {
4
3
  Value: any;
5
- Element: React.ReactElement<any>;
4
+ Element: React.ReactElement<any> | string;
6
5
  }
7
- interface IProps<T> extends Gemstone.TSX.Interfaces.IBaseFormProps<T> {
6
+ interface IProps<T> {
8
7
  /**
9
- * Options for the select dropdown
10
- * @type {{ Value: any, Element: React.ReactElement<any> }[]}
8
+ * Record to be used in form
9
+ * @type {T}
10
+ */
11
+ Record: T;
12
+ /**
13
+ * Field of the record to be edited
14
+ * @type {keyof T}
15
+ */
16
+ Field: keyof T;
17
+ /**
18
+ * Label to display for the form, defaults to the Field prop
19
+ * @type {string}
20
+ * @optional
21
+ */
22
+ Label?: string;
23
+ /**
24
+ * Help message or element to display
25
+ * @type {string | JSX.Element}
26
+ * @optional
27
+ */
28
+ Help?: string | JSX.Element;
29
+ /**
30
+ * Flag to disable the input field
31
+ * @type {boolean}
32
+ * @optional
33
+ */
34
+ Disabled?: boolean;
35
+ /**
36
+ * Setter function to update the Record
37
+ * @param record - Updated Record
38
+ */
39
+ Setter: (record: T, option: IOption) => void;
40
+ /**
41
+ * Options for the select dropdown
42
+ * @type {{ Value: any, Element: React.ReactElement<any> }[]}
11
43
  */
12
44
  Options: IOption[];
13
45
  /**
@@ -98,7 +98,7 @@ function StylableSelect(props) {
98
98
  record[props.Field] = selectedOption.Value;
99
99
  else
100
100
  record[props.Field] = null;
101
- props.Setter(record);
101
+ props.Setter(record, selectedOption);
102
102
  }
103
103
  // Effect for initial setup and event listeners.
104
104
  React.useEffect(function () {
@@ -145,6 +145,6 @@ function StylableSelect(props) {
145
145
  } },
146
146
  React.createElement("table", { className: "table table-hover", style: { margin: 0 }, ref: selectTable },
147
147
  React.createElement("tbody", null, props.Options.map(function (f, i) { return (f.Value === props.Record[props.Field] ? null :
148
- React.createElement("tr", { key: i, onClick: function (evt) { return handleOptionClick(evt, f); } },
148
+ React.createElement("tr", { key: i, onMouseDown: function (evt) { return handleOptionClick(evt, f); } },
149
149
  React.createElement("td", null, f.Element))); })))))));
150
150
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gpa-gemstone/react-forms",
3
- "version": "1.1.78",
3
+ "version": "1.1.80",
4
4
  "description": "React Form modules for gpa webapps",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -45,7 +45,7 @@
45
45
  "typescript": "5.5.3"
46
46
  },
47
47
  "dependencies": {
48
- "@gpa-gemstone/application-typings": "0.0.79",
48
+ "@gpa-gemstone/application-typings": "0.0.80",
49
49
  "@gpa-gemstone/gpa-symbols": "0.0.46",
50
50
  "@gpa-gemstone/helper-functions": "0.0.37",
51
51
  "@types/react": "^17.0.14",