@gustavo-valsechi/client 1.4.118 → 1.4.120

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.
@@ -50,7 +50,7 @@ function InputSelect(props) {
50
50
  const register = (props.register || ((name) => ({})))(props.name || "");
51
51
  const inputValue = (_a = props.watch) == null ? void 0 : _a.call(props, props.name);
52
52
  const value = ((_b = import_lodash.default.find(props.options, (option) => Object.keys(option)[0] === inputValue)) == null ? void 0 : _b[inputValue || ""]) || "";
53
- const addOptionsTarget = (0, import_react.useCallback)(() => {
53
+ (0, import_react.useEffect)(() => {
54
54
  var _a2;
55
55
  if (!(inputRef == null ? void 0 : inputRef.current) || !(containerRef == null ? void 0 : containerRef.current)) return;
56
56
  target.add({
@@ -67,9 +67,6 @@ function InputSelect(props) {
67
67
  });
68
68
  return () => target.remove(inputRef);
69
69
  }, [containerRef, inputRef]);
70
- (0, import_react.useEffect)(() => {
71
- addOptionsTarget();
72
- }, [addOptionsTarget]);
73
70
  const onBlur = (event) => {
74
71
  setFocus(false);
75
72
  setFilter("");
@@ -79,10 +76,6 @@ function InputSelect(props) {
79
76
  setFocus(true);
80
77
  if (register.onFocus) register.onFocus(event);
81
78
  };
82
- const onClick = (event) => {
83
- if (register.onClick) register.onClick(event);
84
- addOptionsTarget();
85
- };
86
79
  const onChange = (event) => {
87
80
  const value2 = event.target.value || "";
88
81
  event.target.value = props.mask ? props.mask(value2) : value2;
@@ -114,7 +107,6 @@ function InputSelect(props) {
114
107
  onChange,
115
108
  onFocus,
116
109
  onBlur,
117
- onClick,
118
110
  value: focus ? filter : "",
119
111
  placeholder: import_lodash.default.isString(value) ? value : (value == null ? void 0 : value.textContent) || ""
120
112
  }
@@ -1,6 +1,6 @@
1
1
  "use client";
2
2
  import { jsx, jsxs } from "react/jsx-runtime";
3
- import { useState, useRef, useEffect, useCallback } from "react";
3
+ import { useState, useRef, useEffect } from "react";
4
4
  import { Container } from "./styles";
5
5
  import { InputLabel } from "../label";
6
6
  import { InputError } from "../error";
@@ -17,7 +17,7 @@ function InputSelect(props) {
17
17
  const register = (props.register || ((name) => ({})))(props.name || "");
18
18
  const inputValue = (_a = props.watch) == null ? void 0 : _a.call(props, props.name);
19
19
  const value = ((_b = _.find(props.options, (option) => Object.keys(option)[0] === inputValue)) == null ? void 0 : _b[inputValue || ""]) || "";
20
- const addOptionsTarget = useCallback(() => {
20
+ useEffect(() => {
21
21
  var _a2;
22
22
  if (!(inputRef == null ? void 0 : inputRef.current) || !(containerRef == null ? void 0 : containerRef.current)) return;
23
23
  target.add({
@@ -34,9 +34,6 @@ function InputSelect(props) {
34
34
  });
35
35
  return () => target.remove(inputRef);
36
36
  }, [containerRef, inputRef]);
37
- useEffect(() => {
38
- addOptionsTarget();
39
- }, [addOptionsTarget]);
40
37
  const onBlur = (event) => {
41
38
  setFocus(false);
42
39
  setFilter("");
@@ -46,10 +43,6 @@ function InputSelect(props) {
46
43
  setFocus(true);
47
44
  if (register.onFocus) register.onFocus(event);
48
45
  };
49
- const onClick = (event) => {
50
- if (register.onClick) register.onClick(event);
51
- addOptionsTarget();
52
- };
53
46
  const onChange = (event) => {
54
47
  const value2 = event.target.value || "";
55
48
  event.target.value = props.mask ? props.mask(value2) : value2;
@@ -81,7 +74,6 @@ function InputSelect(props) {
81
74
  onChange,
82
75
  onFocus,
83
76
  onBlur,
84
- onClick,
85
77
  value: focus ? filter : "",
86
78
  placeholder: _.isString(value) ? value : (value == null ? void 0 : value.textContent) || ""
87
79
  }
@@ -44,7 +44,7 @@ function InputSelectOption(props) {
44
44
  (0, import_react.useEffect)(() => {
45
45
  var _a;
46
46
  (_a = props.setValue) == null ? void 0 : _a.call(props, props.name, value);
47
- target.remove(props.inputRef);
47
+ target.close(props.inputRef);
48
48
  }, [value, props.inputRef]);
49
49
  (0, import_react.useEffect)(() => {
50
50
  const optionsElement = document.getElementById("input-options");
@@ -11,7 +11,7 @@ function InputSelectOption(props) {
11
11
  useEffect(() => {
12
12
  var _a;
13
13
  (_a = props.setValue) == null ? void 0 : _a.call(props, props.name, value);
14
- target.remove(props.inputRef);
14
+ target.close(props.inputRef);
15
15
  }, [value, props.inputRef]);
16
16
  useEffect(() => {
17
17
  const optionsElement = document.getElementById("input-options");
@@ -50,22 +50,25 @@ const TargetProviderContainer = ({ children }) => {
50
50
  if (isFocusable) return target.ref.current;
51
51
  return inputRef.current[index];
52
52
  };
53
- const close = (element, index) => {
53
+ const close = (target, index) => {
54
+ const element = containerRef.current[index];
54
55
  element.style.opacity = "0";
55
56
  element.style.zIndex = "-1";
56
57
  insideMemory.splice(import_lodash.default.findIndex(insideMemory, (i) => i === index), 1);
57
58
  };
58
- const open = (element, coords) => {
59
+ const open = (target, index, coords) => {
60
+ const element = containerRef.current[index];
59
61
  element.style.top = `${coords.top}px`;
60
62
  element.style.left = `${coords.left}px`;
61
63
  element.style.opacity = "1";
62
64
  element.style.zIndex = "10";
65
+ elementFocusable(target, index).focus();
63
66
  };
64
67
  (0, import_react.useEffect)(() => {
65
68
  const handleScroll = () => {
66
69
  import_lodash.default.forEach(containerRef.current, (element, index) => {
67
70
  if (import_lodash.default.some(insideMemory, (i) => i === index)) return;
68
- close(element, index);
71
+ close(targets[index], index);
69
72
  });
70
73
  };
71
74
  window.addEventListener("scroll", handleScroll, { passive: true });
@@ -98,31 +101,29 @@ const TargetProviderContainer = ({ children }) => {
98
101
  });
99
102
  observer.disconnect();
100
103
  };
101
- }, []);
104
+ }, [targets]);
102
105
  (0, import_react.useEffect)(() => {
103
106
  import_lodash.default.forEach(targets, (target, index) => {
104
107
  var _a;
105
108
  const element = (_a = target.ref) == null ? void 0 : _a.current;
106
109
  if (!(element == null ? void 0 : element.addEventListener)) return;
107
110
  const focusable = elementFocusable(target, index);
108
- const targetElement = import_lodash.default.find(containerRef.current, (data, i) => i === index);
111
+ const targetElement = containerRef.current[index];
109
112
  element.addEventListener("click", () => {
110
- if (!targetElement) return;
111
113
  const styles = window.getComputedStyle(targetElement);
112
114
  if (styles.opacity === "1") {
113
- close(targetElement, index);
115
+ close(target, index);
114
116
  return;
115
117
  }
116
118
  const coords = getCoords(element);
117
- open(targetElement, coords);
118
- focusable.focus();
119
+ open(target, index, coords);
119
120
  });
120
121
  element.addEventListener("resize", () => {
121
- import_lodash.default.forEach(containerRef.current, (elementRef) => close(elementRef, index));
122
+ import_lodash.default.forEach(containerRef.current, (elementRef) => close(target, index));
122
123
  });
123
124
  focusable.addEventListener("blur", () => {
124
125
  if (import_lodash.default.some(insideMemory, (i) => i === index)) return;
125
- close(containerRef.current[index], index);
126
+ close(target, index);
126
127
  });
127
128
  });
128
129
  }, [targets, inputRef, containerRef]);
@@ -150,18 +151,22 @@ const TargetProviderContainer = ({ children }) => {
150
151
  };
151
152
  const remove = (ref) => {
152
153
  const index = import_lodash.default.findIndex(memory, (data) => data.ref.current === ref.current);
153
- console.log(import_lodash.default.find(memory, (data) => data.ref.current === ref.current));
154
154
  insideMemory.splice(import_lodash.default.findIndex(insideMemory, (i) => i === index), 1);
155
- memory.slice(index, 1);
156
- console.log("memory", memory);
155
+ memory.splice(index, 1);
157
156
  setTargets(memory);
158
157
  };
158
+ const closeTarget = (ref) => {
159
+ const index = import_lodash.default.findIndex(memory, (data) => data.ref.current === ref.current);
160
+ insideMemory.splice(import_lodash.default.findIndex(insideMemory, (i) => i === index), 1);
161
+ elementFocusable(targets[index], index).blur();
162
+ };
159
163
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
160
164
  TargetContext.Provider,
161
165
  {
162
166
  value: {
163
167
  add,
164
- remove
168
+ remove,
169
+ close: closeTarget
165
170
  },
166
171
  children: [
167
172
  import_lodash.default.map(
@@ -16,22 +16,25 @@ const TargetProviderContainer = ({ children }) => {
16
16
  if (isFocusable) return target.ref.current;
17
17
  return inputRef.current[index];
18
18
  };
19
- const close = (element, index) => {
19
+ const close = (target, index) => {
20
+ const element = containerRef.current[index];
20
21
  element.style.opacity = "0";
21
22
  element.style.zIndex = "-1";
22
23
  insideMemory.splice(_.findIndex(insideMemory, (i) => i === index), 1);
23
24
  };
24
- const open = (element, coords) => {
25
+ const open = (target, index, coords) => {
26
+ const element = containerRef.current[index];
25
27
  element.style.top = `${coords.top}px`;
26
28
  element.style.left = `${coords.left}px`;
27
29
  element.style.opacity = "1";
28
30
  element.style.zIndex = "10";
31
+ elementFocusable(target, index).focus();
29
32
  };
30
33
  useEffect(() => {
31
34
  const handleScroll = () => {
32
35
  _.forEach(containerRef.current, (element, index) => {
33
36
  if (_.some(insideMemory, (i) => i === index)) return;
34
- close(element, index);
37
+ close(targets[index], index);
35
38
  });
36
39
  };
37
40
  window.addEventListener("scroll", handleScroll, { passive: true });
@@ -64,31 +67,29 @@ const TargetProviderContainer = ({ children }) => {
64
67
  });
65
68
  observer.disconnect();
66
69
  };
67
- }, []);
70
+ }, [targets]);
68
71
  useEffect(() => {
69
72
  _.forEach(targets, (target, index) => {
70
73
  var _a;
71
74
  const element = (_a = target.ref) == null ? void 0 : _a.current;
72
75
  if (!(element == null ? void 0 : element.addEventListener)) return;
73
76
  const focusable = elementFocusable(target, index);
74
- const targetElement = _.find(containerRef.current, (data, i) => i === index);
77
+ const targetElement = containerRef.current[index];
75
78
  element.addEventListener("click", () => {
76
- if (!targetElement) return;
77
79
  const styles = window.getComputedStyle(targetElement);
78
80
  if (styles.opacity === "1") {
79
- close(targetElement, index);
81
+ close(target, index);
80
82
  return;
81
83
  }
82
84
  const coords = getCoords(element);
83
- open(targetElement, coords);
84
- focusable.focus();
85
+ open(target, index, coords);
85
86
  });
86
87
  element.addEventListener("resize", () => {
87
- _.forEach(containerRef.current, (elementRef) => close(elementRef, index));
88
+ _.forEach(containerRef.current, (elementRef) => close(target, index));
88
89
  });
89
90
  focusable.addEventListener("blur", () => {
90
91
  if (_.some(insideMemory, (i) => i === index)) return;
91
- close(containerRef.current[index], index);
92
+ close(target, index);
92
93
  });
93
94
  });
94
95
  }, [targets, inputRef, containerRef]);
@@ -116,18 +117,22 @@ const TargetProviderContainer = ({ children }) => {
116
117
  };
117
118
  const remove = (ref) => {
118
119
  const index = _.findIndex(memory, (data) => data.ref.current === ref.current);
119
- console.log(_.find(memory, (data) => data.ref.current === ref.current));
120
120
  insideMemory.splice(_.findIndex(insideMemory, (i) => i === index), 1);
121
- memory.slice(index, 1);
122
- console.log("memory", memory);
121
+ memory.splice(index, 1);
123
122
  setTargets(memory);
124
123
  };
124
+ const closeTarget = (ref) => {
125
+ const index = _.findIndex(memory, (data) => data.ref.current === ref.current);
126
+ insideMemory.splice(_.findIndex(insideMemory, (i) => i === index), 1);
127
+ elementFocusable(targets[index], index).blur();
128
+ };
125
129
  return /* @__PURE__ */ jsxs(
126
130
  TargetContext.Provider,
127
131
  {
128
132
  value: {
129
133
  add,
130
- remove
134
+ remove,
135
+ close: closeTarget
131
136
  },
132
137
  children: [
133
138
  _.map(
@@ -13,4 +13,5 @@ export interface ITargetConfigCoords {
13
13
  export interface ITargetContext {
14
14
  add: (config: ITargetConfig) => void;
15
15
  remove: (ref: React.RefObject<any>) => void;
16
+ close: (ref: React.RefObject<any>) => void;
16
17
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gustavo-valsechi/client",
3
- "version": "1.4.118",
3
+ "version": "1.4.120",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/index.js",
6
6
  "types": "dist/index.d.ts",