@ssplib/react-components 0.0.96 → 0.0.98

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,11 +1,12 @@
1
1
  /// <reference types="react" />
2
- export default function FetchAutoComplete({ name, url, title, customLoadingText, shouldRefetch, required, defaultValue, xs, sm, md, }: {
2
+ export default function FetchAutoComplete({ name, url, title, customLoadingText, shouldRefetch, required, defaultValue, onChange, xs, sm, md, }: {
3
3
  name: string;
4
4
  url: string;
5
5
  title: string;
6
6
  customLoadingText?: string;
7
7
  defaultValue?: number;
8
8
  required?: boolean;
9
+ onChange?: (id: number | undefined) => void;
9
10
  shouldRefetch?: boolean;
10
11
  xs?: number;
11
12
  sm?: number;
@@ -31,7 +31,7 @@ const react_1 = __importStar(require("react"));
31
31
  const form_1 = require("../../../context/form");
32
32
  const auth_1 = require("../../../context/auth");
33
33
  const lodash_get_1 = __importDefault(require("lodash.get"));
34
- function FetchAutoComplete({ name, url, title, customLoadingText, shouldRefetch = true, required = false, defaultValue, xs = 12, sm, md, }) {
34
+ function FetchAutoComplete({ name, url, title, customLoadingText, shouldRefetch = true, required = false, defaultValue, onChange = () => { }, xs = 12, sm, md, }) {
35
35
  const context = (0, react_1.useContext)(form_1.FormContext);
36
36
  const [loading, setLoading] = (0, react_1.useState)(true);
37
37
  const [list, setList] = (0, react_1.useState)([]);
@@ -89,6 +89,10 @@ function FetchAutoComplete({ name, url, title, customLoadingText, shouldRefetch
89
89
  }
90
90
  });
91
91
  }
92
+ function handleAutoCompleteChange(value) {
93
+ context === null || context === void 0 ? void 0 : context.formSetValue(name, value ? value.id : '');
94
+ onChange(value ? value.id : -1);
95
+ }
92
96
  if (defaultValue && list.length <= 0 && !dValue)
93
97
  return (react_1.default.createElement(material_1.Grid, Object.assign({ item: true }, { xs, sm, md }),
94
98
  react_1.default.createElement(material_1.TextField, { size: 'small', fullWidth: true, placeholder: loadingText, disabled: true })));
@@ -100,7 +104,7 @@ function FetchAutoComplete({ name, url, title, customLoadingText, shouldRefetch
100
104
  return 'Este campo é obrigatório';
101
105
  },
102
106
  }), { hidden: true })),
103
- react_1.default.createElement(material_1.Autocomplete, { loading: loading, loadingText: loadingText, options: list, defaultValue: dValue, isOptionEqualToValue: (op, value) => op.id === value.id, onChange: (e, v) => context === null || context === void 0 ? void 0 : context.formSetValue(name, v ? v.id : ''), renderInput: (params) => {
107
+ react_1.default.createElement(material_1.Autocomplete, { loading: loading, loadingText: loadingText, options: list, defaultValue: dValue, isOptionEqualToValue: (op, value) => op.id === value.id, onChange: (e, v) => handleAutoCompleteChange(v), renderInput: (params) => {
104
108
  var _a;
105
109
  return (react_1.default.createElement(material_1.TextField, Object.assign({}, params, { size: 'small', fullWidth: true, placeholder: title, onFocus: onFocus, required: true, error: (0, lodash_get_1.default)(context === null || context === void 0 ? void 0 : context.errors, name) ? true : false, helperText: (_a = (0, lodash_get_1.default)(context === null || context === void 0 ? void 0 : context.errors, name)) === null || _a === void 0 ? void 0 : _a.message })));
106
110
  }, size: 'small', fullWidth: true })));
@@ -1,12 +1,14 @@
1
1
  /// <reference types="react" />
2
- export declare function ToggleVisibility({ invert, ...props }: {
2
+ export declare function ToggleVisibility({ invert, hasCheckValue, ...props }: {
3
3
  switchId: string;
4
4
  unregisterNameList: string[];
5
5
  invert?: boolean;
6
+ hasCheckValue: boolean;
6
7
  }): JSX.Element;
7
- export declare function SwitchWatch({ invert, ...props }: {
8
+ export declare function SwitchWatch({ invert, checkValue, ...props }: {
8
9
  children: JSX.Element | JSX.Element[];
9
10
  switchId: string;
10
11
  unregisterNameList: string[];
11
12
  invert?: boolean;
13
+ checkValue?: string | number;
12
14
  }): JSX.Element;
@@ -39,11 +39,11 @@ const react_1 = __importStar(require("react"));
39
39
  const form_1 = require("../../../context/form");
40
40
  // Coloque esse componente dentro de um bloco que é retirado com o valor do input
41
41
  function ToggleVisibility(_a) {
42
- var { invert = false } = _a, props = __rest(_a, ["invert"]);
42
+ var { invert = false, hasCheckValue = false } = _a, props = __rest(_a, ["invert", "hasCheckValue"]);
43
43
  const context = (0, react_1.useContext)(form_1.FormContext);
44
44
  (0, react_1.useEffect)(() => {
45
45
  return () => {
46
- if (context.formWatch(props.switchId) === invert) {
46
+ if (hasCheckValue ? invert : context.formWatch(props.switchId) === invert) {
47
47
  props.unregisterNameList.forEach((x) => context.formUnregister(x));
48
48
  }
49
49
  };
@@ -52,10 +52,10 @@ function ToggleVisibility(_a) {
52
52
  }
53
53
  exports.ToggleVisibility = ToggleVisibility;
54
54
  function SwitchWatch(_a) {
55
- var { invert = false } = _a, props = __rest(_a, ["invert"]);
55
+ var { invert = false, checkValue } = _a, props = __rest(_a, ["invert", "checkValue"]);
56
56
  const context = (0, react_1.useContext)(form_1.FormContext);
57
- return (react_1.default.createElement(react_1.default.Fragment, null, (context === null || context === void 0 ? void 0 : context.formWatch(props.switchId)) !== invert && (react_1.default.createElement(react_1.default.Fragment, null,
58
- react_1.default.createElement(ToggleVisibility, { switchId: props.switchId, unregisterNameList: props.unregisterNameList, invert: invert }),
57
+ return (react_1.default.createElement(react_1.default.Fragment, null, (checkValue ? ((context === null || context === void 0 ? void 0 : context.formWatch(props.switchId)) === checkValue) !== invert : (context === null || context === void 0 ? void 0 : context.formWatch(props.switchId)) !== invert) && (react_1.default.createElement(react_1.default.Fragment, null,
58
+ react_1.default.createElement(ToggleVisibility, { switchId: props.switchId, unregisterNameList: props.unregisterNameList, hasCheckValue: !!checkValue, invert: checkValue ? ((context === null || context === void 0 ? void 0 : context.formWatch(props.switchId)) === checkValue) !== invert : invert }),
59
59
  props.children))));
60
60
  }
61
61
  exports.SwitchWatch = SwitchWatch;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ssplib/react-components",
3
- "version": "0.0.96",
3
+ "version": "0.0.98",
4
4
  "description": "SSP React Components",
5
5
  "main": "index.js",
6
6
  "author": "Pedro Henrique <sr.hudrick@gmail.com>",