argous 1.2.156 → 1.2.159

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.
@@ -148,6 +148,9 @@ var useStyles = core_1.makeStyles(function (theme) { return ({
148
148
  background: theme.palette.type === 'light' ? 'rgba(0, 0, 0, 0.05)' : 'rgba(255, 255, 255, 0.08)'
149
149
  }
150
150
  },
151
+ hoverListItem: {
152
+ background: theme.palette.type === 'light' ? 'rgba(0, 0, 0, 0.05)' : 'rgba(255, 255, 255, 0.08)'
153
+ },
151
154
  listItemText: {
152
155
  margin: 0,
153
156
  width: '100%',
@@ -178,7 +181,7 @@ var useStyles = core_1.makeStyles(function (theme) { return ({
178
181
  }
179
182
  },
180
183
  popper: {
181
- zIndex: 1301,
184
+ zIndex: 1300,
182
185
  width: function (_a) {
183
186
  var widthListBox = _a.widthListBox;
184
187
  return widthListBox ? "" + widthListBox + (testWidth(widthListBox.toString()) ? 'px' : '') + " !important" : '';
@@ -206,7 +209,7 @@ exports.CustomTooltip = core_1.withStyles(function (theme) { return ({
206
209
  var ListboxFavorites = react_1.forwardRef(function (props, ref) {
207
210
  var classes = useStyles();
208
211
  var children = props.children, favoritePage = props.favoritePage, dados = props.dados, setDados = props.setDados, group = props.group, groupHeaderColor = props.groupHeaderColor, groupTitleColor = props.groupTitleColor;
209
- var pageNumber = dados.pageNumber, favoritosLength = dados.favoritosLength, direction = dados.direction, slide = dados.slide;
212
+ var pageNumber = dados.pageNumber, favoritosLength = dados.favoritosLength, direction = dados.direction, slide = dados.slide, cursor = dados.cursor;
210
213
  function retornaInicio() {
211
214
  switch (pageNumber) {
212
215
  case 1:
@@ -234,16 +237,16 @@ var ListboxFavorites = react_1.forwardRef(function (props, ref) {
234
237
  return section.props.children[1].props.children.slice(retornaInicio(), retornaFim());
235
238
  return section.props.children[1].props.children;
236
239
  }
237
- function retornaEstrutura(sectionIndex, item) {
240
+ function retornaEstrutura(sectionIndex, item, itemIndex) {
241
+ var _a;
242
+ var index = itemIndex ? itemIndex : sectionIndex;
243
+ var content = react_1.default.createElement(exports.CustomTooltip, { title: item.props.children, arrow: true, enterDelay: '1000', enterNextDelay: '1000', placement: 'top-end', TransitionComponent: core_1.Fade, TransitionProps: { timeout: { enter: 100, exit: 500 } } },
244
+ react_1.default.createElement(core_1.ListItem, __assign({}, item.props, { id: 'listItem', key: item.key, className: clsx_1.default(classes.listItem, (_a = {}, _a[classes.hoverListItem] = (cursor === null || cursor === void 0 ? void 0 : cursor.index) === index && (cursor === null || cursor === void 0 ? void 0 : cursor.page) === pageNumber, _a)) }),
245
+ react_1.default.createElement(core_1.ListItemText, { primary: item.props.children, className: classes.listItemText })));
238
246
  if (sectionIndex === 0 && favoritePage && favoritosLength > 7) {
239
- return (react_1.default.createElement(core_1.Slide, { in: slide, direction: direction },
240
- react_1.default.createElement(exports.CustomTooltip, { title: item.props.children, arrow: true, enterDelay: '1000', enterNextDelay: '1000', placement: 'top-end', TransitionComponent: core_1.Fade, TransitionProps: { timeout: { enter: 100, exit: 500 } } },
241
- react_1.default.createElement(core_1.ListItem, __assign({}, item.props, { key: item.key, className: classes.listItem }),
242
- react_1.default.createElement(core_1.ListItemText, { primary: item.props.children, className: classes.listItemText })))));
247
+ return react_1.default.createElement(core_1.Slide, { in: slide, direction: direction }, content);
243
248
  }
244
- return (react_1.default.createElement(exports.CustomTooltip, { title: item.props.children, arrow: true, enterDelay: '1000', enterNextDelay: '1000', placement: 'top-end', TransitionComponent: core_1.Fade, TransitionProps: { timeout: { enter: 100, exit: 500 } } },
245
- react_1.default.createElement(core_1.ListItem, __assign({}, item.props, { key: item.key, className: classes.listItem }),
246
- react_1.default.createElement(core_1.ListItemText, { primary: item.props.children, className: classes.listItemText }))));
249
+ return content;
247
250
  }
248
251
  function buttonClick(evento, value) {
249
252
  evento.preventDefault();
@@ -272,40 +275,43 @@ var ListboxFavorites = react_1.forwardRef(function (props, ref) {
272
275
  return 'right';
273
276
  return 'left';
274
277
  }
275
- return react_1.default.createElement("ul", __assign({}, props, { className: clsx_1.default('MuiAutocomplete-listbox', classes.box) }), children.map(function (section, sectionIndex) {
278
+ return react_1.default.createElement("ul", __assign({}, props, { id: 'listBox', className: clsx_1.default('MuiAutocomplete-listbox', classes.box) }), children.map(function (section, sectionIndex) {
276
279
  var _a, _b, _c;
277
- return (react_1.default.createElement("ul", __assign({}, props, { className: clsx_1.default('MuiAutocomplete-listbox', classes.box) }), group ?
280
+ return (group ?
278
281
  react_1.default.createElement(react_1.default.Fragment, null,
279
- react_1.default.createElement(core_1.ListSubheader, { className: classes.subHeader, style: { backgroundColor: groupHeaderColor === null || groupHeaderColor === void 0 ? void 0 : groupHeaderColor[sectionIndex], color: groupTitleColor === null || groupTitleColor === void 0 ? void 0 : groupTitleColor[sectionIndex] } },
280
- section.props.children[0].props.children,
281
- react_1.default.createElement(core_1.Fade, { in: sectionIndex === 0 && favoritePage && favoritosLength > 7 },
282
- react_1.default.createElement("div", { className: classes.divButton },
283
- react_1.default.createElement("div", { className: classes.areaButton },
284
- react_1.default.createElement("div", { onClick: function (e) { return buttonClick(e, 1); }, className: clsx_1.default((_a = {}, _a[classes.buttonActive] = pageNumber === 1, _a), classes.button) }, "1")),
285
- react_1.default.createElement("div", { className: classes.areaButton },
286
- react_1.default.createElement("div", { onClick: function (e) { return buttonClick(e, 2); }, className: clsx_1.default((_b = {}, _b[classes.buttonActive] = pageNumber === 2, _b), classes.button) }, "2")),
287
- favoritosLength > 14 &&
288
- react_1.default.createElement(react_1.default.Fragment, null,
289
- react_1.default.createElement("div", { className: classes.areaButton },
290
- react_1.default.createElement("div", { onClick: function (e) { return buttonClick(e, 3); }, className: clsx_1.default((_c = {}, _c[classes.buttonActive] = pageNumber === 3, _c), classes.button) }, "3")))))),
291
- retornaArray(sectionIndex, section).map(function (item, itemIndex) { return retornaEstrutura(sectionIndex, item); }))
292
- : retornaEstrutura(sectionIndex, section)));
282
+ react_1.default.createElement("ul", __assign({}, props, { id: 'listBox', className: clsx_1.default('MuiAutocomplete-listbox', classes.box) }),
283
+ react_1.default.createElement(core_1.ListSubheader, { className: classes.subHeader, style: { backgroundColor: groupHeaderColor === null || groupHeaderColor === void 0 ? void 0 : groupHeaderColor[sectionIndex], color: groupTitleColor === null || groupTitleColor === void 0 ? void 0 : groupTitleColor[sectionIndex] } },
284
+ section.props.children[0].props.children,
285
+ react_1.default.createElement(core_1.Fade, { in: sectionIndex === 0 && favoritePage && favoritosLength > 7 },
286
+ react_1.default.createElement("div", { className: classes.divButton },
287
+ react_1.default.createElement("div", { className: classes.areaButton },
288
+ react_1.default.createElement("div", { onClick: function (e) { return buttonClick(e, 1); }, className: clsx_1.default((_a = {}, _a[classes.buttonActive] = pageNumber === 1, _a), classes.button) }, "1")),
289
+ react_1.default.createElement("div", { className: classes.areaButton },
290
+ react_1.default.createElement("div", { onClick: function (e) { return buttonClick(e, 2); }, className: clsx_1.default((_b = {}, _b[classes.buttonActive] = pageNumber === 2, _b), classes.button) }, "2")),
291
+ favoritosLength > 14 &&
292
+ react_1.default.createElement(react_1.default.Fragment, null,
293
+ react_1.default.createElement("div", { className: classes.areaButton },
294
+ react_1.default.createElement("div", { onClick: function (e) { return buttonClick(e, 3); }, className: clsx_1.default((_c = {}, _c[classes.buttonActive] = pageNumber === 3, _c), classes.button) }, "3")))))),
295
+ retornaArray(sectionIndex, section).map(function (item, itemIndex) { return retornaEstrutura(sectionIndex, item, itemIndex); })))
296
+ : retornaEstrutura(sectionIndex, section));
293
297
  }));
294
298
  });
295
299
  var AutoCompleteFavorites = function (_a) {
296
- var action = _a.action, busca = _a.busca, resposta = _a.resposta, autoSearch = _a.autoSearch, label = _a.label, value = _a.value, pgCode = _a.pgCode, favoritePage = _a.favoritePage, group = _a.group, groupName = _a.groupName, groupHeaderColor = _a.groupHeaderColor, groupTitleColor = _a.groupTitleColor, loadingColor = _a.loadingColor, widthListBox = _a.widthListBox, filterFunction = _a.filterFunction, getOptionLabel = _a.getOptionLabel, _b = _a.onChange, onChange = _b === void 0 ? function (e) { return e; } : _b, _c = _a.textFieldProps, textFieldProps = _c === void 0 ? {} : _c, searchParams = _a.searchParams, props = __rest(_a, ["action", "busca", "resposta", "autoSearch", "label", "value", "pgCode", "favoritePage", "group", "groupName", "groupHeaderColor", "groupTitleColor", "loadingColor", "widthListBox", "filterFunction", "getOptionLabel", "onChange", "textFieldProps", "searchParams"]);
300
+ var _b;
301
+ var action = _a.action, busca = _a.busca, resposta = _a.resposta, autoSearch = _a.autoSearch, label = _a.label, value = _a.value, pgCode = _a.pgCode, favoritePage = _a.favoritePage, _c = _a.group, group = _c === void 0 ? false : _c, groupName = _a.groupName, groupHeaderColor = _a.groupHeaderColor, groupTitleColor = _a.groupTitleColor, loadingColor = _a.loadingColor, widthListBox = _a.widthListBox, filterFunction = _a.filterFunction, getOptionLabel = _a.getOptionLabel, _d = _a.onChange, onChange = _d === void 0 ? function (e) { return e; } : _d, _e = _a.textFieldProps, textFieldProps = _e === void 0 ? {} : _e, searchParams = _a.searchParams, props = __rest(_a, ["action", "busca", "resposta", "autoSearch", "label", "value", "pgCode", "favoritePage", "group", "groupName", "groupHeaderColor", "groupTitleColor", "loadingColor", "widthListBox", "filterFunction", "getOptionLabel", "onChange", "textFieldProps", "searchParams"]);
297
302
  var classes = useStyles({ widthListBox: widthListBox });
298
- var _d = react_1.useState({
303
+ var _f = react_1.useState({
299
304
  lista: [],
300
305
  slide: true,
306
+ cursor: { index: '', page: '' },
301
307
  direction: '',
302
308
  favoritosLength: '',
303
- pageNumber: favoritePage ? 1 : '',
304
- }), dados = _d[0], setDados = _d[1];
305
- var _e = react_1.useState(false), loading = _e[0], setLoading = _e[1];
306
- var _f = react_1.useState(''), inputValue = _f[0], setInputValue = _f[1];
309
+ pageNumber: favoritePage ? 1 : undefined,
310
+ }), dados = _f[0], setDados = _f[1];
311
+ var _g = react_1.useState(false), loading = _g[0], setLoading = _g[1];
312
+ var _h = react_1.useState(''), inputValue = _h[0], setInputValue = _h[1];
307
313
  var interval = react_1.useRef(null);
308
- var _g = react_1.useState(1), seconds = _g[0], setSeconds = _g[1];
314
+ var _j = react_1.useState(1), seconds = _j[0], setSeconds = _j[1];
309
315
  var startCounter = function () { return interval.current = setInterval(function () { return setSeconds(1); }, 1000); };
310
316
  var stopCounter = function () {
311
317
  clearInterval(interval.current);
@@ -352,7 +358,7 @@ var AutoCompleteFavorites = function (_a) {
352
358
  return [3 /*break*/, 4];
353
359
  case 3:
354
360
  if (res[resposta || 'result'])
355
- setDados({ lista: res[resposta || 'result'] || [] });
361
+ setDados(function (v) { return (__assign(__assign({}, v), { lista: res[resposta || 'result'] || [] })); });
356
362
  else
357
363
  api_1.setError('Erro ao tentar buscar dados.');
358
364
  _c.label = 4;
@@ -365,7 +371,108 @@ var AutoCompleteFavorites = function (_a) {
365
371
  react_1.useEffect(function () { return autoSearch && seconds === 1 && buscar(); }, [autoSearch, seconds, buscar]);
366
372
  // eslint-disable-next-line react-hooks/exhaustive-deps
367
373
  react_1.useEffect(function () { return lodash_1.isString(value) && buscar(value); }, []);
368
- return (react_1.default.createElement(lab_1.Autocomplete, __assign({}, props, { value: value, onChange: onChange }, { autoComplete: true, autoHighlight: true, openText: 'Abrir', clearText: 'Limpar', filterSelectedOptions: true, onInputChange: function (e) { var _a; return setInput(((_a = e === null || e === void 0 ? void 0 : e.target) === null || _a === void 0 ? void 0 : _a.value) || ''); }, getOptionSelected: function (o, v) { return o[0] === v; }, getOptionLabel: getOptionLabel && (function (option) { return getOptionLabel(option, dados === null || dados === void 0 ? void 0 : dados.lista); }), noOptionsText: inputValue.length < 2 ? 'Digite para pesquisar' : 'Não Encontrado', options: lodash_1.isObject(value) ? dados === null || dados === void 0 ? void 0 : dados.lista.filter(function (v) { return filterFunction ? filterFunction(v, value) : true || v[0] !== value[0]; }) : dados === null || dados === void 0 ? void 0 : dados.lista, renderInput: function (params) { return (react_1.default.createElement(core_1.TextField, __assign({ margin: 'dense', variant: 'outlined', label: label }, params, textFieldProps, { onKeyUp: function (e) { return ((e.key === 'Enter' || e.key === 'Tab') && !loading) && buscar(); }, inputProps: __assign(__assign({}, params.inputProps), { onKeyDown: function (e) { return (e.key === 'Enter' || e.key === 'Tab') && e.stopPropagation(); } }), InputProps: __assign(__assign(__assign({}, textFieldProps.InputProps), params.InputProps), { endAdornment: (react_1.default.createElement(react_1.default.Fragment, null,
374
+ react_1.useEffect(function () { var _a; return (value !== ((_a = dados.cursor) === null || _a === void 0 ? void 0 : _a.linha)) && setDados(function (v) { return (__assign(__assign({}, v), { cursor: { index: '', page: '' } })); }); }, [value, (_b = dados.cursor) === null || _b === void 0 ? void 0 : _b.linha]);
375
+ var keyDown = function (e) {
376
+ e.preventDefault();
377
+ e.stopPropagation();
378
+ ((e.key === 'Enter' || e.key === 'Tab') && !loading) && buscar();
379
+ (e.key === 'ArrowDown' || e.key === 'ArrowUp') && selectListboxItem(e.key);
380
+ };
381
+ var keyUp = function (e) { return (e.key === 'Enter' || e.key === 'Tab') && e.stopPropagation(); };
382
+ var numbersOnly = function (value) { return value.replace(/[^0-9]/g, ''); };
383
+ var getFinder = function (v) {
384
+ var _a;
385
+ var elInnerText = v.innerText;
386
+ var endIndexCode = elInnerText.indexOf('-') - 1;
387
+ var code = elInnerText.substring(0, endIndexCode);
388
+ var finder = ((_a = numbersOnly(code)) === null || _a === void 0 ? void 0 : _a.length) ? code : elInnerText;
389
+ return finder;
390
+ };
391
+ function validateLine(value, finder, el) {
392
+ var _a, _b, _c;
393
+ var retorno = false;
394
+ if (el) {
395
+ var elIndex = parseInt((_b = (_a = el === null || el === void 0 ? void 0 : el.attributes) === null || _a === void 0 ? void 0 : _a[4]) === null || _b === void 0 ? void 0 : _b.value);
396
+ var itemSelecionado = (_c = dados.lista) === null || _c === void 0 ? void 0 : _c[elIndex];
397
+ if (lodash_1.isObject(itemSelecionado))
398
+ retorno = JSON.stringify(value) === JSON.stringify(itemSelecionado) ? true : false;
399
+ else if (lodash_1.isArray(itemSelecionado))
400
+ retorno = value.equals(itemSelecionado) ? true : false;
401
+ }
402
+ else if (lodash_1.isArray(value)) {
403
+ value.forEach(function (v) {
404
+ if (retorno)
405
+ return;
406
+ if (v && v.toString() === finder)
407
+ retorno = true;
408
+ });
409
+ }
410
+ else if (lodash_1.isObject(value)) {
411
+ Object.values(value).forEach(function (v) {
412
+ if (retorno)
413
+ return;
414
+ if (v && v.toString() === finder)
415
+ retorno = true;
416
+ });
417
+ }
418
+ return retorno;
419
+ }
420
+ function selectListboxItem(key) {
421
+ var _a;
422
+ var directionIndex = key === 'ArrowDown' ? 1 : key === 'ArrowUp' ? -1 : 0;
423
+ var itens = document.querySelectorAll('#listItem');
424
+ var array = Array.from(itens);
425
+ var el = '';
426
+ var linha = '';
427
+ var indexCapture = 0;
428
+ if (value) {
429
+ array.forEach(function (v, i) {
430
+ var finder = getFinder(v);
431
+ if (linha)
432
+ return;
433
+ if (validateLine(value, finder, v)) {
434
+ var nextIndex = i + directionIndex;
435
+ if (nextIndex < array.length)
436
+ indexCapture = nextIndex > -1 ? nextIndex : i;
437
+ else
438
+ indexCapture = i;
439
+ linha = dados.lista[indexCapture];
440
+ }
441
+ });
442
+ el = array[indexCapture];
443
+ }
444
+ if (!linha)
445
+ linha = dados.lista[indexCapture];
446
+ var elTop = 0;
447
+ var elTotalHeight = 0;
448
+ var lastScrollTop = 0;
449
+ var elListBox = document.getElementById('listBox');
450
+ if (el) {
451
+ elTop = (_a = el === null || el === void 0 ? void 0 : el.getBoundingClientRect()) === null || _a === void 0 ? void 0 : _a.top;
452
+ elTotalHeight = el.offsetHeight + el.offsetTop;
453
+ }
454
+ if (elListBox)
455
+ lastScrollTop = elListBox.scrollTop;
456
+ linha && setDados(function (v) { return (__assign(__assign({}, v), { cursor: { index: indexCapture, page: v.pageNumber, linha: linha, elTop: elTop, elTotalHeight: elTotalHeight, lastScrollTop: lastScrollTop } })); });
457
+ if (linha !== value)
458
+ invokeOnChange('', linha);
459
+ }
460
+ var scrollToSelected = react_1.useCallback(function () {
461
+ var _a, _b, _c, _d;
462
+ var elListBox = document.getElementById('listBox');
463
+ if (elListBox) {
464
+ var clientHeight = elListBox === null || elListBox === void 0 ? void 0 : elListBox.clientHeight;
465
+ if (clientHeight < ((_a = dados.cursor) === null || _a === void 0 ? void 0 : _a.elTotalHeight)) {
466
+ var valueScrollTop = ((_b = dados.cursor) === null || _b === void 0 ? void 0 : _b.elTop) - clientHeight;
467
+ if ((_c = dados.cursor) === null || _c === void 0 ? void 0 : _c.lastScrollTop)
468
+ valueScrollTop += (_d = dados.cursor) === null || _d === void 0 ? void 0 : _d.lastScrollTop;
469
+ elListBox.scrollTop = valueScrollTop;
470
+ }
471
+ }
472
+ }, [dados]);
473
+ react_1.useEffect(function () { var _a; return ((_a = dados.lista) === null || _a === void 0 ? void 0 : _a.length) && setTimeout(function () { return scrollToSelected(); }, 0); }, [dados.lista, scrollToSelected]);
474
+ var invokeOnChange = onChange;
475
+ return (react_1.default.createElement(lab_1.Autocomplete, __assign({}, props, { value: value }, { onChange: invokeOnChange, autoComplete: true, autoHighlight: true, openText: 'Abrir', clearText: 'Limpar', filterSelectedOptions: true, onInputChange: function (e) { var _a; return setInput(((_a = e === null || e === void 0 ? void 0 : e.target) === null || _a === void 0 ? void 0 : _a.value) || ''); }, getOptionSelected: function (o, v) { return o[0] === v; }, getOptionLabel: getOptionLabel && (function (option) { return getOptionLabel(option, dados === null || dados === void 0 ? void 0 : dados.lista); }), noOptionsText: inputValue.length < 2 ? 'Digite para pesquisar' : 'Não Encontrado', options: lodash_1.isObject(value) ? dados === null || dados === void 0 ? void 0 : dados.lista.filter(function (v) { return filterFunction ? filterFunction(v, value) : true || v[0] !== value[0]; }) : dados === null || dados === void 0 ? void 0 : dados.lista, renderInput: function (params) { return (react_1.default.createElement(core_1.TextField, __assign({ margin: 'dense', variant: 'outlined', label: label }, params, textFieldProps, { onKeyUp: function (e) { return keyUp(e); }, inputProps: __assign(__assign({}, params.inputProps), { onKeyDown: function (e) { return keyDown(e); } }), InputProps: __assign(__assign(__assign({}, textFieldProps.InputProps), params.InputProps), { endAdornment: (react_1.default.createElement(react_1.default.Fragment, null,
369
476
  params.InputProps.endAdornment,
370
477
  react_1.default.createElement(core_1.Fade, { in: loading },
371
478
  react_1.default.createElement(core_1.CircularProgress, { disableShrink: true, color: loadingColor || 'inherit', size: 20 })),
@@ -2,7 +2,7 @@ import { serverAction, setError } from '../../utils/api';
2
2
  import { CircularProgress, Fade, IconButton, ListItem, ListItemText, ListSubheader, makeStyles, Popper, Slide, TextField, Tooltip, withStyles } from '@material-ui/core';
3
3
  import { Autocomplete } from '@material-ui/lab';
4
4
  import clsx from 'clsx';
5
- import { isObject, isString } from 'lodash';
5
+ import { isArray, isObject, isString } from 'lodash';
6
6
  import React, { forwardRef, useCallback, useEffect, useRef, useState } from 'react';
7
7
  import { IoIosSearch } from 'react-icons/io';
8
8
 
@@ -66,6 +66,9 @@ const useStyles = makeStyles((theme) => ({
66
66
  background: theme.palette.type === 'light' ? 'rgba(0, 0, 0, 0.05)' : 'rgba(255, 255, 255, 0.08)'
67
67
  }
68
68
  },
69
+ hoverListItem: {
70
+ background: theme.palette.type === 'light' ? 'rgba(0, 0, 0, 0.05)' : 'rgba(255, 255, 255, 0.08)'
71
+ },
69
72
  listItemText: {
70
73
  margin: 0,
71
74
  width: '100%',
@@ -96,7 +99,7 @@ const useStyles = makeStyles((theme) => ({
96
99
  }
97
100
  },
98
101
  popper: {
99
- zIndex: 1301,
102
+ zIndex: 1300,
100
103
  width: ({ widthListBox }) => widthListBox ? `${widthListBox}${testWidth(widthListBox.toString()) ? 'px' : ''} !important` : '',
101
104
  }
102
105
  }));
@@ -126,11 +129,11 @@ const ListboxFavorites = forwardRef((props, ref) => {
126
129
 
127
130
  const classes = useStyles();
128
131
  const { children, favoritePage, dados, setDados, group, groupHeaderColor, groupTitleColor } = props;
129
- const { pageNumber, favoritosLength, direction, slide } = dados;
132
+ const { pageNumber, favoritosLength, direction, slide, cursor } = dados;
130
133
 
131
134
  function retornaInicio() {
132
135
  switch (pageNumber) {
133
- case 1:
136
+ case 1:
134
137
  return 0;
135
138
  case 2:
136
139
  return 7;
@@ -157,24 +160,18 @@ const ListboxFavorites = forwardRef((props, ref) => {
157
160
  return section.props.children[1].props.children;
158
161
  }
159
162
 
160
- function retornaEstrutura(sectionIndex, item) {
163
+ function retornaEstrutura(sectionIndex, item, itemIndex) {
164
+ const index = itemIndex ? itemIndex : sectionIndex;
165
+ const content = <CustomTooltip title={item.props.children} arrow enterDelay='1000' enterNextDelay='1000' placement='top-end' TransitionComponent={Fade} TransitionProps={{ timeout: { enter: 100, exit: 500 } }}>
166
+ <ListItem {...item.props} id='listItem' key={item.key} className={clsx(classes.listItem, { [classes.hoverListItem]: cursor?.index === index && cursor?.page === pageNumber })}>
167
+ <ListItemText primary={item.props.children} className={classes.listItemText} />
168
+ </ListItem>
169
+ </CustomTooltip>;
161
170
  if (sectionIndex === 0 && favoritePage && favoritosLength > 7) {
162
- return (
163
- <Slide in={slide} direction={direction}>
164
- <CustomTooltip title={item.props.children} arrow enterDelay='1000' enterNextDelay='1000' placement='top-end' TransitionComponent={Fade} TransitionProps={{ timeout: { enter: 100, exit: 500 } }}>
165
- <ListItem {...item.props} key={item.key} className={classes.listItem}>
166
- <ListItemText primary={item.props.children} className={classes.listItemText} />
167
- </ListItem>
168
- </CustomTooltip>
169
- </Slide>
170
- );
171
- } return (
172
- <CustomTooltip title={item.props.children} arrow enterDelay='1000' enterNextDelay='1000' placement='top-end' TransitionComponent={Fade} TransitionProps={{ timeout: { enter: 100, exit: 500 } }}>
173
- <ListItem {...item.props} key={item.key} className={classes.listItem}>
174
- <ListItemText primary={item.props.children} className={classes.listItemText} />
175
- </ListItem>
176
- </CustomTooltip>
177
- );
171
+ return <Slide in={slide} direction={direction}>
172
+ {content}
173
+ </Slide>;
174
+ } return content;
178
175
  }
179
176
 
180
177
  function buttonClick(evento, value) {
@@ -203,49 +200,48 @@ const ListboxFavorites = forwardRef((props, ref) => {
203
200
  return 'left';
204
201
  }
205
202
 
206
- return <ul {...props} className={clsx('MuiAutocomplete-listbox', classes.box)}>
207
- {children.map((section, sectionIndex) => (
208
- <ul {...props} className={clsx('MuiAutocomplete-listbox', classes.box)}>
209
- {group ?
210
- <React.Fragment>
211
- <ListSubheader className={classes.subHeader} style={{ backgroundColor: groupHeaderColor?.[sectionIndex], color: groupTitleColor?.[sectionIndex] }}>
212
- {section.props.children[0].props.children}
213
- <Fade in={sectionIndex === 0 && favoritePage && favoritosLength > 7}>
214
- <div className={classes.divButton}>
215
- <div className={classes.areaButton}>
216
- <div onClick={e => buttonClick(e, 1)} className={clsx({ [classes.buttonActive]: pageNumber === 1 }, classes.button)}>1</div>
217
- </div>
218
- <div className={classes.areaButton}>
219
- <div onClick={e => buttonClick(e, 2)} className={clsx({ [classes.buttonActive]: pageNumber === 2 }, classes.button)}>2</div>
220
- </div>
221
- {favoritosLength > 14 &&
222
- <React.Fragment>
203
+ return <ul {...props} id='listBox' className={clsx('MuiAutocomplete-listbox', classes.box)}>
204
+ {children.map((section, sectionIndex) => (group ?
205
+ <React.Fragment>
206
+ <ul {...props} id='listBox' className={clsx('MuiAutocomplete-listbox', classes.box)}>
207
+ <ListSubheader className={classes.subHeader} style={{ backgroundColor: groupHeaderColor?.[sectionIndex], color: groupTitleColor?.[sectionIndex] }}>
208
+ {section.props.children[0].props.children}
209
+ <Fade in={sectionIndex === 0 && favoritePage && favoritosLength > 7}>
210
+ <div className={classes.divButton}>
223
211
  <div className={classes.areaButton}>
224
- <div onClick={e => buttonClick(e, 3)} className={clsx({ [classes.buttonActive]: pageNumber === 3 }, classes.button)}>3</div>
212
+ <div onClick={e => buttonClick(e, 1)} className={clsx({ [classes.buttonActive]: pageNumber === 1 }, classes.button)}>1</div>
225
213
  </div>
226
- </React.Fragment>
227
- }
214
+ <div className={classes.areaButton}>
215
+ <div onClick={e => buttonClick(e, 2)} className={clsx({ [classes.buttonActive]: pageNumber === 2 }, classes.button)}>2</div>
228
216
  </div>
229
- </Fade>
230
- </ListSubheader>
231
- {retornaArray(sectionIndex, section).map((item, itemIndex) => retornaEstrutura(sectionIndex, item))}
232
- </React.Fragment>
233
- : retornaEstrutura(sectionIndex, section)
234
- }
235
- </ul>
217
+ {favoritosLength > 14 &&
218
+ <React.Fragment>
219
+ <div className={classes.areaButton}>
220
+ <div onClick={e => buttonClick(e, 3)} className={clsx({ [classes.buttonActive]: pageNumber === 3 }, classes.button)}>3</div>
221
+ </div>
222
+ </React.Fragment>
223
+ }
224
+ </div>
225
+ </Fade>
226
+ </ListSubheader>
227
+ {retornaArray(sectionIndex, section).map((item, itemIndex) => retornaEstrutura(sectionIndex, item, itemIndex))}
228
+ </ul>
229
+ </React.Fragment>
230
+ : retornaEstrutura(sectionIndex, section)
236
231
  ))}
237
232
  </ul>;
238
233
  });
239
234
 
240
- const AutoCompleteFavorites = ({ action, busca, resposta, autoSearch, label, value, pgCode, favoritePage, group, groupName, groupHeaderColor, groupTitleColor, loadingColor, widthListBox, filterFunction, getOptionLabel, onChange = e => e, textFieldProps = {}, searchParams, ...props }) => {
235
+ const AutoCompleteFavorites = ({ action, busca, resposta, autoSearch, label, value, pgCode, favoritePage, group = false, groupName, groupHeaderColor, groupTitleColor, loadingColor, widthListBox, filterFunction, getOptionLabel, onChange = e => e, textFieldProps = {}, searchParams, ...props }) => {
241
236
 
242
237
  const classes = useStyles({ widthListBox });
243
238
  const [dados, setDados] = useState({
244
239
  lista: [],
245
240
  slide: true,
241
+ cursor: { index: '', page: '' },
246
242
  direction: '',
247
243
  favoritosLength: '',
248
- pageNumber: favoritePage ? 1 : '',
244
+ pageNumber: favoritePage ? 1 : undefined,
249
245
  });
250
246
  const [loading, setLoading] = useState(false);
251
247
  const [inputValue, setInputValue] = useState('');
@@ -281,7 +277,7 @@ const AutoCompleteFavorites = ({ action, busca, resposta, autoSearch, label, val
281
277
  setLoading(true);
282
278
  const res = await serverAction(action, { [busca]: valor || inputValue, ...searchParams }, true, pgCode);
283
279
  if (res[resposta || 'result']?.length > 1 && group) await carimbaLista(res[resposta || 'result']);
284
- else if (res[resposta || 'result']) setDados({ lista: res[resposta || 'result'] || [] });
280
+ else if (res[resposta || 'result']) setDados(v => ({ ...v, lista: res[resposta || 'result'] || [] }));
285
281
  else setError('Erro ao tentar buscar dados.');
286
282
  setLoading(false);
287
283
  // eslint-disable-next-line react-hooks/exhaustive-deps
@@ -291,15 +287,112 @@ const AutoCompleteFavorites = ({ action, busca, resposta, autoSearch, label, val
291
287
  // eslint-disable-next-line react-hooks/exhaustive-deps
292
288
  useEffect(() => isString(value) && buscar(value), []);
293
289
 
290
+ useEffect(() => (value !== dados.cursor?.linha) && setDados(v => ({ ...v, cursor: { index: '', page: '' } })), [value, dados.cursor?.linha]);
291
+
292
+ const keyDown = e => {
293
+ e.preventDefault();
294
+ e.stopPropagation();
295
+ ((e.key === 'Enter' || e.key === 'Tab') && !loading) && buscar();
296
+ (e.key === 'ArrowDown' || e.key === 'ArrowUp') && selectListboxItem(e.key);
297
+ };
298
+
299
+ const keyUp = e => (e.key === 'Enter' || e.key === 'Tab') && e.stopPropagation();
300
+
301
+ const numbersOnly = value => value.replace(/[^0-9]/g, '');
302
+
303
+ const getFinder = v => {
304
+ const elInnerText = v.innerText;
305
+ const endIndexCode = elInnerText.indexOf('-') - 1;
306
+ const code = elInnerText.substring(0, endIndexCode);
307
+ const finder = numbersOnly(code)?.length ? code : elInnerText;
308
+ return finder;
309
+ };
310
+
311
+ function validateLine(value, finder, el) {
312
+ let retorno = false;
313
+ if (el) {
314
+ const elIndex = parseInt(el?.attributes?.[4]?.value);
315
+ const itemSelecionado = dados.lista?.[elIndex];
316
+ if (isObject(itemSelecionado)) retorno = JSON.stringify(value) === JSON.stringify(itemSelecionado) ? true : false;
317
+ else if (isArray(itemSelecionado)) retorno = value.equals(itemSelecionado) ? true : false;
318
+ } else if (isArray(value)) {
319
+ value.forEach(v => {
320
+ if (retorno) return;
321
+ if (v && v.toString() === finder) retorno = true;
322
+ });
323
+ } else if (isObject(value)) {
324
+ Object.values(value).forEach(v => {
325
+ if (retorno) return;
326
+ if (v && v.toString() === finder) retorno = true;
327
+ });
328
+ }
329
+ return retorno;
330
+ }
331
+
332
+ function selectListboxItem(key) {
333
+ const directionIndex = key === 'ArrowDown' ? 1 : key === 'ArrowUp' ? -1 : 0;
334
+ const itens = document.querySelectorAll('#listItem');
335
+ const array = Array.from(itens);
336
+
337
+ let el = '';
338
+ let linha = '';
339
+ let indexCapture = 0;
340
+
341
+ if (value) {
342
+ array.forEach((v, i) => {
343
+ const finder = getFinder(v);
344
+ if (linha) return;
345
+ if (validateLine(value, finder, v)) {
346
+ const nextIndex = i + directionIndex;
347
+ if (nextIndex < array.length) indexCapture = nextIndex > -1 ? nextIndex : i;
348
+ else indexCapture = i;
349
+ linha = dados.lista[indexCapture];
350
+ }
351
+ });
352
+ el = array[indexCapture];
353
+ }
354
+ if (!linha)linha = dados.lista[indexCapture];
355
+
356
+ let elTop = 0;
357
+ let elTotalHeight = 0;
358
+ let lastScrollTop = 0;
359
+ const elListBox = document.getElementById('listBox');
360
+
361
+ if (el) {
362
+ elTop = el?.getBoundingClientRect()?.top;
363
+ elTotalHeight = el.offsetHeight + el.offsetTop;
364
+ }
365
+ if (elListBox) lastScrollTop = elListBox.scrollTop;
366
+
367
+ linha && setDados(v => ({ ...v, cursor: { index: indexCapture, page: v.pageNumber, linha: linha, elTop: elTop, elTotalHeight: elTotalHeight, lastScrollTop: lastScrollTop } }));
368
+ if (linha !== value) invokeOnChange('', linha);
369
+ }
370
+
371
+ const scrollToSelected = useCallback(() => {
372
+ const elListBox = document.getElementById('listBox');
373
+ if (elListBox) {
374
+ const clientHeight = elListBox?.clientHeight;
375
+ if (clientHeight < dados.cursor?.elTotalHeight) {
376
+ let valueScrollTop = dados.cursor?.elTop - clientHeight;
377
+ if (dados.cursor?.lastScrollTop) valueScrollTop += dados.cursor?.lastScrollTop;
378
+ elListBox.scrollTop = valueScrollTop;
379
+ }
380
+ }
381
+ }, [dados]);
382
+ useEffect(() => dados.lista?.length && setTimeout(() => scrollToSelected(), 0), [dados.lista, scrollToSelected]);
383
+
384
+ const invokeOnChange = onChange;
385
+
294
386
  return (
295
387
  <Autocomplete
296
388
  {...props}
297
- {...{ value, onChange }}
389
+ {...{ value }}
390
+ onChange={invokeOnChange}
298
391
  autoComplete
299
392
  autoHighlight
300
393
  openText='Abrir'
301
394
  clearText='Limpar'
302
- filterSelectedOptions
395
+ filterSelectedOptions
303
396
  onInputChange={e => setInput(e?.target?.value || '')}
304
397
  getOptionSelected={(o, v) => o[0] === v}
305
398
  getOptionLabel={getOptionLabel && (option => getOptionLabel(option, dados?.lista))}
@@ -312,10 +405,10 @@ const AutoCompleteFavorites = ({ action, busca, resposta, autoSearch, label, val
312
405
  label={label}
313
406
  {...params}
314
407
  {...textFieldProps}
315
- onKeyUp={e => ((e.key === 'Enter' || e.key === 'Tab') && !loading) && buscar()}
408
+ onKeyUp={e => keyUp(e)}
316
409
  inputProps={{
317
410
  ...params.inputProps,
318
- onKeyDown: e => (e.key === 'Enter' || e.key === 'Tab') && e.stopPropagation()
411
+ onKeyDown: e => keyDown(e)
319
412
  }}
320
413
  InputProps={{
321
414
  ...textFieldProps.InputProps,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "argous",
3
- "version": "1.2.156",
3
+ "version": "1.2.159",
4
4
  "description": "Argous.",
5
5
  "main": "lib/index.js",
6
6
  "author": "Carlos Schneider",