@m4l/components 9.2.51 → 9.2.53

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.
Files changed (58) hide show
  1. package/.storybook/test-utils/index.d.ts +1 -0
  2. package/.storybook/test-utils/waitForHelpers.d.ts +4 -0
  3. package/.storybook/utils/getStylesColorsByMode.d.ts +98 -70
  4. package/components/Chip/Chip.js +3 -1
  5. package/components/Chip/types.d.ts +4 -0
  6. package/components/DataGrid/constants.d.ts +21 -0
  7. package/components/DataGrid/constants.js +24 -1
  8. package/components/DataGrid/index.d.ts +1 -0
  9. package/components/DataGrid/subcomponents/Table/subcomponents/ActionsColumn.js +2 -2
  10. package/components/DynamicFilter/subcomponents/FieldTypes/DateTimeFilter/helpers.js +3 -2
  11. package/components/DynamicFilter/types.d.ts +2 -2
  12. package/components/ObjectLogs/subcomponents/ObjectLogsByOthers/ObjectLogsByOthers.js +1 -1
  13. package/components/hook-form/RHFAutocomplete/RHFAutocomplete.js +30 -19
  14. package/components/hook-form/RHFAutocomplete/tests/common.d.ts +37 -0
  15. package/components/hook-form/RHFAutocomplete/types.d.ts +3 -8
  16. package/components/hook-form/RHFAutocompleteAsync/RHFAutocompleteAsync.d.ts +32 -5
  17. package/components/hook-form/RHFAutocompleteAsync/RHFAutocompleteAsync.js +49 -84
  18. package/components/hook-form/RHFAutocompleteAsync/RHFAutocompleteAsyncStyles.d.ts +2 -2
  19. package/components/hook-form/RHFAutocompleteAsync/hooks/useFetchOptions.d.ts +5 -0
  20. package/components/hook-form/RHFAutocompleteAsync/hooks/useFetchOptions.js +64 -0
  21. package/components/hook-form/RHFAutocompleteAsync/reducer/RHFAutocompleteReducer.d.ts +14 -0
  22. package/components/hook-form/RHFAutocompleteAsync/reducer/RHFAutocompleteReducer.js +79 -0
  23. package/components/hook-form/RHFAutocompleteAsync/slots/RHFAutocompleteSlotsAsync.d.ts +1 -1
  24. package/components/hook-form/RHFAutocompleteAsync/tests/OnChangFilterParmsDefined.test.d.ts +1 -0
  25. package/components/hook-form/RHFAutocompleteAsync/tests/OnChangFilterParmsUndefined.test.d.ts +1 -0
  26. package/components/hook-form/RHFAutocompleteAsync/tests/RHFAutocompleteReducer.test.d.ts +1 -0
  27. package/components/hook-form/RHFAutocompleteAsync/tests/common.d.ts +41 -0
  28. package/components/hook-form/RHFAutocompleteAsync/types.d.ts +120 -4
  29. package/components/hook-form/RHFAutocompleteAsync/types.js +38 -0
  30. package/components/hook-form/RHFTextField/RHFTextField.js +3 -2
  31. package/components/mui_extended/Autocomplete/Autocomplete.d.ts +7 -0
  32. package/components/mui_extended/Autocomplete/Autocomplete.js +59 -196
  33. package/components/mui_extended/Autocomplete/Autocomplete.styles.js +27 -16
  34. package/components/mui_extended/Autocomplete/hooks/useEndAdornments.d.ts +14 -0
  35. package/components/mui_extended/Autocomplete/hooks/useEndAdornments.js +47 -0
  36. package/components/mui_extended/Autocomplete/hooks/useStartAdornments.d.ts +21 -0
  37. package/components/mui_extended/Autocomplete/hooks/useStartAdornments.js +31 -0
  38. package/components/mui_extended/Autocomplete/hooks/useValuesAndHandlers.d.ts +23 -0
  39. package/components/mui_extended/Autocomplete/hooks/useValuesAndHandlers.js +135 -0
  40. package/components/mui_extended/Autocomplete/icons.d.ts +4 -0
  41. package/components/mui_extended/Autocomplete/icons.js +7 -0
  42. package/components/mui_extended/Autocomplete/renderOptions/index.d.ts +7 -2
  43. package/components/mui_extended/Autocomplete/renderOptions/index.js +7 -6
  44. package/components/mui_extended/Autocomplete/slots/AutocompleteSlots.js +7 -8
  45. package/components/mui_extended/Autocomplete/types.d.ts +2 -0
  46. package/hooks/useDynamicFilterAndSort/useDynamicFilterAndSort.js +13 -10
  47. package/index.js +2 -0
  48. package/mockServiceWorker.js +1 -1
  49. package/package.json +1 -1
  50. package/storybook/components/SideBar/SideBar.stories.d.ts +17 -5
  51. package/storybook/components/extended/mui/Autocomplete/AutoComple.stories.d.ts +1 -0
  52. package/storybook/hook-form/RHFAutocomplete/RHFAutocomplete.stories.d.ts +8 -6
  53. package/storybook/hook-form/RHFAutocomplete/constants.d.ts +23 -0
  54. package/storybook/hook-form/RHFAutocompleteAsync/RHFAutocompleteAsync.stories.d.ts +11 -4
  55. package/storybook/hook-form/RHFAutocompleteAsync/constants.d.ts +10 -1
  56. package/storybook/hook-form/RHFAutocompleteAsync/handlers.d.ts +1 -0
  57. package/components/hook-form/RHFAutocomplete/oldTypes.d.ts +0 -43
  58. /package/components/hook-form/RHFAutocomplete/{RHFAutocomplete.test.d.ts → tests/RHFAutocomplete.test.d.ts} +0 -0
@@ -1,18 +1,17 @@
1
- import { jsx, jsxs } from "react/jsx-runtime";
2
- import { useModuleDictionary, useModuleSkeleton, useEnvironment } from "@m4l/core";
3
- import { forwardRef, useState, useCallback, useEffect, useMemo } from "react";
1
+ import { jsx } from "react/jsx-runtime";
2
+ import { useModuleDictionary, useModuleSkeleton } from "@m4l/core";
3
+ import { forwardRef, useMemo } from "react";
4
4
  import { r as renderOption } from "./renderOptions/index.js";
5
+ import { u as useValuesAndHandlers } from "./hooks/useValuesAndHandlers.js";
6
+ import { u as useStartAdornments } from "./hooks/useStartAdornments.js";
7
+ import { u as useEndAdornments } from "./hooks/useEndAdornments.js";
5
8
  import { u as useComponentSize } from "../../../hooks/useComponentSize/useComponentSize.js";
6
- import { C as ChipStyled, I as ImageStyled, A as AdormentsStyled, a as CircularProgressStyled, b as IconButtonStyled, S as SkeletonAutocompleteStyled, c as AutocompleteRootStyled, R as RenderInputStyled, P as PopperComponentStyled } from "./slots/AutocompleteSlots.js";
9
+ import { S as SkeletonAutocompleteStyled, A as AutocompleteRootStyled, R as RenderInputStyled, P as PopperComponentStyled } from "./slots/AutocompleteSlots.js";
7
10
  import { T as Typography } from "../Typography/Typography.js";
8
11
  const Autocomplete = forwardRef(function Autocomplete2(props, ref) {
9
12
  const {
10
- getOptionLabel,
11
- isOptionEqualToValue,
12
13
  options,
13
14
  disabled,
14
- onOpen,
15
- onClose,
16
15
  loading,
17
16
  variant = "outlined",
18
17
  size = "medium",
@@ -22,203 +21,52 @@ const Autocomplete = forwardRef(function Autocomplete2(props, ref) {
22
21
  // Diferencia
23
22
  refresh,
24
23
  error = false,
25
- onChange,
26
- value
24
+ htmlFor
27
25
  } = props;
28
26
  const { getLabel } = useModuleDictionary();
29
27
  const isSkeleton = useModuleSkeleton();
30
- const { host_static_assets, environment_assets } = useEnvironment();
31
- const [open, setOpen] = useState(false);
32
28
  const { currentSize } = useComponentSize(size);
33
29
  const adjustedSize = currentSize === "small" || currentSize === "medium" ? currentSize : "medium";
34
- const [selectedValue, setSelectedValue] = useState(
35
- value || (multiple ? [] : null)
36
- );
37
- const isOptionEqualToValueLocal = useCallback(
38
- (option, val) => {
39
- if (val === null || val === void 0) {
40
- return false;
41
- }
42
- return isOptionEqualToValue(option, val);
43
- },
44
- [isOptionEqualToValue]
45
- );
46
- useEffect(() => {
47
- if (!multiple && value) {
48
- setInputValue(getOptionLabel(value));
49
- } else if (multiple && Array.isArray(value)) {
50
- setInputValue("");
51
- } else {
52
- setInputValue("");
53
- }
54
- if (multiple) {
55
- if (Array.isArray(value)) {
56
- setSelectedValue(value.filter((option) => options.some((opt) => isOptionEqualToValueLocal(opt, option))));
57
- } else {
58
- setSelectedValue([]);
59
- }
60
- } else {
61
- if (value) {
62
- const option = options.find((opt) => isOptionEqualToValueLocal(opt, value));
63
- setSelectedValue(option || null);
64
- } else {
65
- setSelectedValue(null);
66
- }
67
- }
68
- }, [value, multiple, getOptionLabel, options, isOptionEqualToValueLocal]);
69
- const handleDelete = useCallback((optionToDelete) => {
70
- if (Array.isArray(selectedValue)) {
71
- const updatedValue = selectedValue.filter(
72
- (val) => !isOptionEqualToValueLocal(val, optionToDelete)
73
- );
74
- setSelectedValue(updatedValue);
75
- onChange?.(
76
- {},
77
- updatedValue,
78
- "removeOption"
79
- );
80
- }
81
- }, [selectedValue, isOptionEqualToValueLocal, onChange]);
82
- const handleRefresh = useCallback(() => {
83
- refresh?.();
84
- setOpen(true);
85
- }, [refresh]);
86
- const handleChange = (event, newSelectedValue, reason) => {
87
- const updatedValue = multiple ? Array.isArray(newSelectedValue) ? newSelectedValue : [] : newSelectedValue;
88
- setSelectedValue(updatedValue);
89
- setInputValue(
90
- !multiple && updatedValue ? getOptionLabel(updatedValue) : ""
91
- );
92
- onChange?.(event, updatedValue, reason);
93
- };
94
- const handleInputChange = (_event, newValue, reason) => {
95
- setInputValue(newValue);
96
- if (reason === "input" && onChangeFilterParmsLocal) {
97
- onChangeFilterParmsLocal(newValue, reason);
98
- }
99
- };
100
- const onCloseLocal = (event, reason) => {
101
- setOpen(false);
102
- if (onClose) {
103
- onClose(event, reason);
104
- }
105
- };
106
- const onOpenLocal = useCallback((event) => {
107
- setOpen((currentState) => !currentState);
108
- if (onOpen) {
109
- onOpen(event);
110
- }
111
- }, [onOpen]);
112
- const getOptionLabelLocal = useCallback(
113
- (option) => {
114
- if (typeof option === "string") {
115
- return option;
116
- }
117
- if (option === null || option === void 0) {
118
- return "";
119
- }
120
- return getOptionLabel(option);
121
- },
122
- [getOptionLabel]
123
- );
124
- const getOptionUrlImageLocal = useCallback((option) => {
125
- if (option === void 0 || option === null || getOptionUrlImage === void 0) {
126
- return "";
127
- }
128
- return getOptionUrlImage(option);
129
- }, [getOptionUrlImage]);
30
+ const {
31
+ open,
32
+ selectedValue,
33
+ inputValue,
34
+ isOptionEqualToValueLocal,
35
+ handleDelete,
36
+ handleRefresh,
37
+ handleChange,
38
+ handleInputChange,
39
+ onCloseLocal,
40
+ onOpenLocal,
41
+ getOptionLabelLocal,
42
+ checkKeyDown,
43
+ scrollPositionOptionsRef
44
+ } = useValuesAndHandlers(props);
130
45
  const ownerState = useMemo(() => ({
131
46
  size: adjustedSize,
132
47
  variant,
133
48
  disabled,
134
- multiple: Boolean(multiple)
135
- }), [adjustedSize, disabled, multiple, variant]);
136
- const [inputValue, setInputValue] = useState("");
137
- useEffect(() => {
138
- if (!open && value === null && inputValue !== "") {
139
- setInputValue("");
140
- }
141
- if (!open && value !== null && options && options.length === 0) {
142
- onChangeFilterParmsLocal?.(
143
- getOptionLabelLocal(Array.isArray(value) ? value[0] : value),
144
- "reset"
145
- );
146
- }
147
- }, [
148
- value,
149
- open,
150
- inputValue,
151
- onChangeFilterParmsLocal,
49
+ multiple: Boolean(multiple),
50
+ error
51
+ }), [adjustedSize, disabled, error, multiple, variant]);
52
+ const startAdornments = useStartAdornments({
53
+ selectedValue,
54
+ multiple,
55
+ adjustedSize,
152
56
  getOptionLabelLocal,
153
- options
154
- ]);
155
- const checkKeyDown = (e) => {
156
- if (e.code === "Enter") {
157
- e.preventDefault();
158
- }
159
- };
160
- const selectedOption = options.find((option) => {
161
- return isOptionEqualToValueLocal(option, selectedValue);
57
+ ownerState,
58
+ handleDelete,
59
+ disabled
60
+ });
61
+ const endAdornments = useEndAdornments({
62
+ loading,
63
+ adjustedSize,
64
+ ownerState,
65
+ refresh,
66
+ handleRefresh,
67
+ disabled,
68
+ onOpenLocal
162
69
  });
163
- const adornments = useMemo(() => {
164
- if (disabled) {
165
- return null;
166
- }
167
- const returnValue = {};
168
- if (Array.isArray(selectedValue) && multiple) {
169
- returnValue.startAdornment = selectedValue.map((option, index) => /* @__PURE__ */ jsx(
170
- ChipStyled,
171
- {
172
- size: adjustedSize,
173
- label: getOptionLabelLocal(option),
174
- opacity: true,
175
- onDeleted: () => handleDelete(option),
176
- ownerState: { ...ownerState }
177
- },
178
- index
179
- ));
180
- } else {
181
- if (getOptionUrlImage && selectedOption) {
182
- returnValue.startAdornment = /* @__PURE__ */ jsx(
183
- ImageStyled,
184
- {
185
- ownerState: { ...ownerState },
186
- src: getOptionUrlImageLocal(selectedOption),
187
- alt: getOptionLabelLocal(selectedOption)
188
- }
189
- );
190
- }
191
- }
192
- returnValue.endAdornment = /* @__PURE__ */ jsxs(AdormentsStyled, { ownerState: {}, children: [
193
- loading ? /* @__PURE__ */ jsx(
194
- CircularProgressStyled,
195
- {
196
- ownerState: { ...ownerState },
197
- size: adjustedSize
198
- }
199
- ) : refresh ? /* @__PURE__ */ jsx(
200
- IconButtonStyled,
201
- {
202
- ownerState: { ...ownerState },
203
- icon: `${host_static_assets}/${environment_assets}/frontend/components/RHFAutocompletarAsync/assets/icons/round_refresh.svg`,
204
- onClick: handleRefresh,
205
- disabled,
206
- size: adjustedSize
207
- }
208
- ) : null,
209
- /* @__PURE__ */ jsx(
210
- IconButtonStyled,
211
- {
212
- ownerState: { ...ownerState },
213
- icon: `${host_static_assets}/${environment_assets}/frontend/components/RHFAutocompletarAsync/assets/icons/chevronDown.svg`,
214
- onClick: (event) => onOpenLocal(event),
215
- disabled,
216
- size: adjustedSize
217
- }
218
- )
219
- ] });
220
- return returnValue;
221
- }, [selectedValue, multiple, loading, adjustedSize, getOptionLabelLocal, ownerState, handleDelete, getOptionUrlImage, selectedOption, getOptionUrlImageLocal, refresh, host_static_assets, environment_assets, handleRefresh, disabled, onOpenLocal]);
222
70
  if (isSkeleton) {
223
71
  return /* @__PURE__ */ jsx(
224
72
  SkeletonAutocompleteStyled,
@@ -275,6 +123,20 @@ const Autocomplete = forwardRef(function Autocomplete2(props, ref) {
275
123
  size
276
124
  });
277
125
  },
126
+ ListboxProps: {
127
+ //Esto para no perder el scroll de la lista de opciones, cuando es multiple, ya que MUI no lo guarda
128
+ onScroll: (event) => {
129
+ scrollPositionOptionsRef.current = event.currentTarget.scrollTop;
130
+ },
131
+ /**
132
+ * Ref para guardar el scroll de la lista de opciones
133
+ */
134
+ ref: (listboxNode) => {
135
+ if (listboxNode) {
136
+ listboxNode.scrollTop = scrollPositionOptionsRef.current;
137
+ }
138
+ }
139
+ },
278
140
  renderInput: (params) => {
279
141
  const { InputLabelProps, inputProps, InputProps, ...renderInputOhers } = params;
280
142
  const { className: _classNoCount, ...otherInputProps } = InputProps;
@@ -288,13 +150,14 @@ const Autocomplete = forwardRef(function Autocomplete2(props, ref) {
288
150
  ...InputLabelProps,
289
151
  shrink: true
290
152
  },
291
- inputProps: { ...otherinputProps },
153
+ inputProps: { ...otherinputProps, id: htmlFor },
292
154
  error,
293
155
  ...renderInputOhers,
294
156
  autoComplete: "off",
295
157
  InputProps: {
296
158
  ...otherInputProps,
297
- ...adornments
159
+ startAdornment: startAdornments,
160
+ endAdornment: endAdornments
298
161
  },
299
162
  SelectProps: { native: true },
300
163
  size: adjustedSize,
@@ -45,26 +45,37 @@ const autocompleteSyles = {
45
45
  /**
46
46
  * Styles for the chip component.
47
47
  */
48
- chip: {},
48
+ chip: ({ theme, ownerState }) => ({
49
+ ...ownerState.disabled && {
50
+ background: theme.vars.palette.chips.disabled.contained.backgroundColor,
51
+ color: theme.vars.palette.text.disabled,
52
+ "&& .M4LIcon-icon": {
53
+ backgroundColor: `${theme.vars.palette.text.disabled} !important`
54
+ }
55
+ }
56
+ }),
49
57
  /**
50
58
  * Styles for the text field component.
51
59
  */
52
- textField: ({ theme }) => ({
53
- // position: 'relative',
54
- height: "auto !important",
55
- position: "relative",
56
- "& .MuiInputBase-root": {
57
- padding: `${theme.vars.size.baseSpacings.sp1} ${theme.vars.size.baseSpacings.sp12} ${theme.vars.size.baseSpacings.sp1} 0!important`,
58
- gap: theme.vars.size.baseSpacings.sp1,
59
- flexWrap: "wrap",
60
- "& > input": {
61
- width: "100%",
62
- textOverflow: "ellipsis",
63
- whiteSpace: "nowrap",
64
- overflow: "hidden!important"
60
+ textField: ({ theme }) => {
61
+ return {
62
+ height: "auto !important",
63
+ position: "relative",
64
+ "& .MuiInputBase-root": {
65
+ padding: `${theme.vars.size.baseSpacings.sp1} ${theme.vars.size.baseSpacings.sp12} ${theme.vars.size.baseSpacings.sp1} 0!important`,
66
+ gap: theme.vars.size.baseSpacings.sp1,
67
+ flexWrap: "wrap",
68
+ paddingLeft: `${theme.vars.size.baseSpacings.sp1} !important`,
69
+ // borderColor: `${ ownerState.error ? theme.vars.palette.border.error : theme.vars.palette.border.secondary} !important`,
70
+ "& > input": {
71
+ width: "100%",
72
+ textOverflow: "ellipsis",
73
+ whiteSpace: "nowrap",
74
+ overflow: "hidden!important"
75
+ }
65
76
  }
66
- }
67
- }),
77
+ };
78
+ },
68
79
  /**
69
80
  * Styles for the popper component.
70
81
  */
@@ -0,0 +1,14 @@
1
+ import { AutocompleteOwnerState } from '../types';
2
+ export type UseAdornmentsProps = {
3
+ loading?: boolean;
4
+ adjustedSize: 'small' | 'medium';
5
+ ownerState: AutocompleteOwnerState;
6
+ refresh?: () => void;
7
+ handleRefresh: () => void;
8
+ disabled?: boolean;
9
+ onOpenLocal: (event: React.MouseEvent<HTMLButtonElement>) => void;
10
+ };
11
+ /**
12
+ * Hook para el componente Autocomplete local
13
+ */
14
+ export declare function useEndAdornments(props: UseAdornmentsProps): import("react/jsx-runtime").JSX.Element;
@@ -0,0 +1,47 @@
1
+ import { jsxs, jsx } from "react/jsx-runtime";
2
+ import { i as icons } from "../icons.js";
3
+ import { useEnvironment } from "@m4l/core";
4
+ import { a as AdormentsStyled, C as CircularProgressStyled, I as IconButtonStyled } from "../slots/AutocompleteSlots.js";
5
+ function useEndAdornments(props) {
6
+ const {
7
+ loading,
8
+ adjustedSize,
9
+ ownerState,
10
+ refresh,
11
+ handleRefresh,
12
+ disabled,
13
+ onOpenLocal
14
+ } = props;
15
+ const { host_static_assets, environment_assets } = useEnvironment();
16
+ return /* @__PURE__ */ jsxs(AdormentsStyled, { ownerState: {}, children: [
17
+ loading ? /* @__PURE__ */ jsx(
18
+ CircularProgressStyled,
19
+ {
20
+ ownerState: { ...ownerState },
21
+ size: adjustedSize
22
+ }
23
+ ) : refresh ? /* @__PURE__ */ jsx(
24
+ IconButtonStyled,
25
+ {
26
+ ownerState: { ...ownerState },
27
+ icon: `${host_static_assets}/${environment_assets}/${icons.refresh}`,
28
+ onClick: handleRefresh,
29
+ disabled,
30
+ size: adjustedSize
31
+ }
32
+ ) : null,
33
+ /* @__PURE__ */ jsx(
34
+ IconButtonStyled,
35
+ {
36
+ ownerState: { ...ownerState },
37
+ icon: `${host_static_assets}/${environment_assets}/${icons.chevronDown}`,
38
+ onClick: (event) => onOpenLocal(event),
39
+ disabled,
40
+ size: adjustedSize
41
+ }
42
+ )
43
+ ] });
44
+ }
45
+ export {
46
+ useEndAdornments as u
47
+ };
@@ -0,0 +1,21 @@
1
+ import { AutocompleteOwnerState } from '../types';
2
+ export type UseAdornmentsProps<T> = {
3
+ selectedValue: T | T[] | null;
4
+ multiple?: boolean;
5
+ loading?: boolean;
6
+ adjustedSize: 'small' | 'medium';
7
+ getOptionLabelLocal: (option: T) => string;
8
+ ownerState: AutocompleteOwnerState;
9
+ handleDelete: (option: T) => void;
10
+ getOptionUrlImage?: (option: T) => string;
11
+ selectedOption?: T;
12
+ getOptionUrlImageLocal: (option: T) => string;
13
+ refresh?: () => void;
14
+ handleRefresh: () => void;
15
+ disabled?: boolean;
16
+ onOpenLocal: (event: React.MouseEvent<HTMLButtonElement>) => void;
17
+ };
18
+ /**
19
+ * Hook para el componente Autocomplete local
20
+ */
21
+ export declare function useStartAdornments<T>(props: UseAdornmentsProps<T>): import("react/jsx-runtime").JSX.Element | null;
@@ -0,0 +1,31 @@
1
+ import { jsx, Fragment } from "react/jsx-runtime";
2
+ import { b as ChipStyled } from "../slots/AutocompleteSlots.js";
3
+ function useStartAdornments(props) {
4
+ const {
5
+ selectedValue,
6
+ multiple,
7
+ adjustedSize,
8
+ getOptionLabelLocal,
9
+ ownerState,
10
+ handleDelete,
11
+ disabled
12
+ } = props;
13
+ if (!(Array.isArray(selectedValue) && multiple)) {
14
+ return null;
15
+ }
16
+ return /* @__PURE__ */ jsx(Fragment, { children: selectedValue.map((option, index) => /* @__PURE__ */ jsx(
17
+ ChipStyled,
18
+ {
19
+ size: adjustedSize,
20
+ label: getOptionLabelLocal(option),
21
+ opacity: true,
22
+ onDeleted: () => handleDelete(option),
23
+ disabledDeleteButton: disabled,
24
+ ownerState: { ...ownerState }
25
+ },
26
+ `${option}-${index}`
27
+ )) });
28
+ }
29
+ export {
30
+ useStartAdornments as u
31
+ };
@@ -0,0 +1,23 @@
1
+ import { AutocompleteChangeReason, AutocompleteCloseReason } from '@mui/material';
2
+ import { KeyboardEvent } from 'react';
3
+ import { AutocompleteProps } from '../types';
4
+ /**
5
+ * Hook para el componente Autocomplete local, proporciona los valores y los handlers para el componente Autocomplete.
6
+ */
7
+ export declare function useValuesAndHandlers<T, Multiple extends boolean | undefined>(props: AutocompleteProps<T, Multiple>): {
8
+ open: boolean;
9
+ inputValue: string;
10
+ selectedValue: T | T[] | null;
11
+ selectedOption: T | undefined;
12
+ isOptionEqualToValueLocal: (option: unknown, val: unknown) => boolean;
13
+ handleDelete: (optionToDelete: T) => void;
14
+ handleRefresh: () => void;
15
+ handleChange: (event: React.SyntheticEvent<Element, Event>, newSelectedValue: unknown, reason: AutocompleteChangeReason) => void;
16
+ handleInputChange: (_event: React.SyntheticEvent, newValue: string, reason: string) => void;
17
+ onCloseLocal: (event: React.SyntheticEvent, reason: AutocompleteCloseReason) => void;
18
+ onOpenLocal: (event: React.SyntheticEvent) => void;
19
+ getOptionLabelLocal: (option: unknown) => string;
20
+ getOptionUrlImageLocal: (option: T | null) => string;
21
+ checkKeyDown: (e: KeyboardEvent<HTMLDivElement>) => void;
22
+ scrollPositionOptionsRef: import('react').MutableRefObject<number>;
23
+ };
@@ -0,0 +1,135 @@
1
+ import { useState, useRef, useCallback, useEffect } from "react";
2
+ function useValuesAndHandlers(props) {
3
+ const {
4
+ getOptionLabel,
5
+ isOptionEqualToValue,
6
+ options,
7
+ onOpen,
8
+ onClose,
9
+ onChangeFilterParmsLocal,
10
+ multiple,
11
+ getOptionUrlImage,
12
+ // Diferencia
13
+ refresh,
14
+ onChange,
15
+ value
16
+ } = props;
17
+ const [open, setOpen] = useState(false);
18
+ const scrollPositionOptionsRef = useRef(0);
19
+ const [selectedValue, setSelectedValue] = useState(
20
+ value || (multiple ? [] : null)
21
+ );
22
+ const isOptionEqualToValueLocal = useCallback(
23
+ (option, val) => {
24
+ if (val === null || val === void 0) {
25
+ return false;
26
+ }
27
+ return isOptionEqualToValue(option, val);
28
+ },
29
+ [isOptionEqualToValue]
30
+ );
31
+ const handleDelete = useCallback((optionToDelete) => {
32
+ if (Array.isArray(selectedValue)) {
33
+ const updatedValue = selectedValue.filter(
34
+ (val) => !isOptionEqualToValueLocal(val, optionToDelete)
35
+ );
36
+ setSelectedValue(updatedValue);
37
+ onChange?.(
38
+ {},
39
+ updatedValue,
40
+ "removeOption"
41
+ );
42
+ }
43
+ }, [selectedValue, isOptionEqualToValueLocal, onChange]);
44
+ const handleRefresh = useCallback(() => {
45
+ refresh?.();
46
+ setOpen(true);
47
+ }, [refresh]);
48
+ const handleChange = (event, newSelectedValue, reason) => {
49
+ let updatedValue = multiple ? Array.isArray(newSelectedValue) ? newSelectedValue : [] : newSelectedValue;
50
+ if (multiple && Array.isArray(updatedValue)) {
51
+ updatedValue = updatedValue?.filter((option) => options.some((opt) => isOptionEqualToValueLocal(opt, option)));
52
+ }
53
+ setSelectedValue(updatedValue);
54
+ onChange?.(event, updatedValue, reason);
55
+ };
56
+ const handleInputChange = (_event, newValue, reason) => {
57
+ setInputValue(newValue);
58
+ if (onChangeFilterParmsLocal && reason === "input") {
59
+ onChangeFilterParmsLocal(newValue, reason);
60
+ }
61
+ };
62
+ const onCloseLocal = (event, reason) => {
63
+ let mustClose = true;
64
+ if (!(reason !== "selectOption" && reason !== "removeOption")) {
65
+ if (multiple) {
66
+ mustClose = false;
67
+ }
68
+ }
69
+ if (mustClose) {
70
+ scrollPositionOptionsRef.current = 0;
71
+ setOpen(false);
72
+ if (onClose) {
73
+ onClose(event, reason);
74
+ }
75
+ }
76
+ };
77
+ const onOpenLocal = useCallback((event) => {
78
+ setOpen(true);
79
+ if (onOpen) {
80
+ onOpen(event);
81
+ }
82
+ }, [onOpen]);
83
+ const getOptionLabelLocal = useCallback(
84
+ (option) => {
85
+ if (typeof option === "string") {
86
+ return option;
87
+ }
88
+ if (option === null || option === void 0) {
89
+ return "";
90
+ }
91
+ return getOptionLabel(option);
92
+ },
93
+ [getOptionLabel]
94
+ );
95
+ const getOptionUrlImageLocal = useCallback((option) => {
96
+ if (option === void 0 || option === null || getOptionUrlImage === void 0) {
97
+ return "";
98
+ }
99
+ return getOptionUrlImage(option);
100
+ }, [getOptionUrlImage]);
101
+ const [inputValue, setInputValue] = useState("");
102
+ useEffect(() => {
103
+ if (open === false && value === null && inputValue !== "") {
104
+ setInputValue("");
105
+ }
106
+ }, [value, open, inputValue]);
107
+ const checkKeyDown = (e) => {
108
+ if (e.code === "Enter") {
109
+ e.preventDefault();
110
+ }
111
+ };
112
+ const selectedOption = options.find((option) => {
113
+ return isOptionEqualToValueLocal(option, selectedValue);
114
+ });
115
+ return {
116
+ open,
117
+ inputValue,
118
+ selectedValue,
119
+ selectedOption,
120
+ isOptionEqualToValueLocal,
121
+ handleDelete,
122
+ handleRefresh,
123
+ handleChange,
124
+ handleInputChange,
125
+ onCloseLocal,
126
+ onOpenLocal,
127
+ getOptionLabelLocal,
128
+ getOptionUrlImageLocal,
129
+ checkKeyDown,
130
+ scrollPositionOptionsRef
131
+ };
132
+ }
133
+ export {
134
+ useValuesAndHandlers as u
135
+ };
@@ -0,0 +1,4 @@
1
+ export declare const icons: {
2
+ refresh: string;
3
+ chevronDown: string;
4
+ };
@@ -0,0 +1,7 @@
1
+ const icons = {
2
+ refresh: "frontend/components/RHFAutocompletarAsync/assets/icons/round_refresh.svg",
3
+ chevronDown: "frontend/components/RHFAutocompletarAsync/assets/icons/chevronDown.svg"
4
+ };
5
+ export {
6
+ icons as i
7
+ };
@@ -5,11 +5,16 @@ import { Sizes } from '@m4l/styles';
5
5
  * It supports rendering options with `startAd` and `endIcon`.
6
6
  * @param color - The color for the MenuItem.
7
7
  */
8
- export declare function renderOption<T>(optionProps: HTMLAttributes<HTMLLIElement>, option: T & {
8
+ type OptionType<T> = T & {
9
9
  label: string;
10
10
  selected?: boolean;
11
11
  disabled?: boolean;
12
12
  startAdornment?: ReactNode;
13
13
  endAdornment?: ReactNode;
14
14
  size: Extract<Sizes, 'small' | 'medium'>;
15
- }): import("react/jsx-runtime").JSX.Element;
15
+ };
16
+ /**
17
+ * Render option for Autocomplete.
18
+ */
19
+ export declare const renderOption: <T>(optionProps: HTMLAttributes<HTMLLIElement>, option: OptionType<T>) => import("react/jsx-runtime").JSX.Element;
20
+ export {};