@ssplib/react-components 0.0.95 → 0.0.97

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,11 +31,12 @@ 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)([]);
38
38
  const [loadingText, setLoadingText] = (0, react_1.useState)('Carregando...');
39
+ const [dValue, setDValue] = (0, react_1.useState)(null);
39
40
  const { user } = (0, react_1.useContext)(auth_1.AuthContext);
40
41
  (0, react_1.useEffect)(() => {
41
42
  if (defaultValue) {
@@ -47,9 +48,16 @@ function FetchAutoComplete({ name, url, title, customLoadingText, shouldRefetch
47
48
  }).then((res) => {
48
49
  if (res.ok) {
49
50
  res.json().then((j) => {
50
- setList(j.body.data);
51
- setLoading(false);
52
- context === null || context === void 0 ? void 0 : context.formSetValue(name, j.body.data[defaultValue].id);
51
+ let value = j.body.data.filter((x) => x.id === defaultValue);
52
+ if (value.length > 0) {
53
+ setList(j.body.data);
54
+ setLoading(false);
55
+ context === null || context === void 0 ? void 0 : context.formSetValue(name, defaultValue);
56
+ setDValue(value[0]);
57
+ }
58
+ else {
59
+ setLoadingText('Erro ao carregar dados. Valor inválido');
60
+ }
53
61
  });
54
62
  }
55
63
  else {
@@ -81,7 +89,11 @@ function FetchAutoComplete({ name, url, title, customLoadingText, shouldRefetch
81
89
  }
82
90
  });
83
91
  }
84
- if (defaultValue && list.length <= 0)
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
+ }
96
+ if (defaultValue && list.length <= 0 && !dValue)
85
97
  return (react_1.default.createElement(material_1.Grid, Object.assign({ item: true }, { xs, sm, md }),
86
98
  react_1.default.createElement(material_1.TextField, { size: 'small', fullWidth: true, placeholder: loadingText, disabled: true })));
87
99
  return (react_1.default.createElement(material_1.Grid, Object.assign({ item: true }, { xs, sm, md }),
@@ -92,7 +104,7 @@ function FetchAutoComplete({ name, url, title, customLoadingText, shouldRefetch
92
104
  return 'Este campo é obrigatório';
93
105
  },
94
106
  }), { hidden: true })),
95
- react_1.default.createElement(material_1.Autocomplete, { loading: loading, loadingText: loadingText, options: list, defaultValue: defaultValue ? list[defaultValue] : undefined, 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) => {
96
108
  var _a;
97
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 })));
98
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,13 @@ 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
+ console.log('INVER:', hasCheckValue, invert);
47
+ console.log('EX:', hasCheckValue ? invert : context.formWatch(props.switchId) === invert);
48
+ if (hasCheckValue ? invert : context.formWatch(props.switchId) === invert) {
47
49
  props.unregisterNameList.forEach((x) => context.formUnregister(x));
48
50
  }
49
51
  };
@@ -52,10 +54,12 @@ function ToggleVisibility(_a) {
52
54
  }
53
55
  exports.ToggleVisibility = ToggleVisibility;
54
56
  function SwitchWatch(_a) {
55
- var { invert = false } = _a, props = __rest(_a, ["invert"]);
57
+ var { invert = false, checkValue } = _a, props = __rest(_a, ["invert", "checkValue"]);
56
58
  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 }),
59
- props.children))));
59
+ return (react_1.default.createElement(react_1.default.Fragment, null,
60
+ react_1.default.createElement("h2", null, JSON.stringify(((context === null || context === void 0 ? void 0 : context.formWatch(props.switchId)) === checkValue) === invert)),
61
+ (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,
62
+ 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 }),
63
+ props.children))));
60
64
  }
61
65
  exports.SwitchWatch = SwitchWatch;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ssplib/react-components",
3
- "version": "0.0.95",
3
+ "version": "0.0.97",
4
4
  "description": "SSP React Components",
5
5
  "main": "index.js",
6
6
  "author": "Pedro Henrique <sr.hudrick@gmail.com>",