@tecsinapse/cortex-react 2.2.0-beta.5 → 2.2.0-beta.6

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.
@@ -3,6 +3,8 @@
3
3
  var jsxRuntime = require('react/jsx-runtime');
4
4
  var cortexCore = require('@tecsinapse/cortex-core');
5
5
  var Option = require('./Option.js');
6
+ var useAutocompleteGroupedOptions = require('../../hooks/useAutocompleteGroupedOptions.js');
7
+ var SkeletonOptions = require('../Select/SkeletonOptions.js');
6
8
 
7
9
  const { groupedTitle, list } = cortexCore.selectVariants();
8
10
  const AutocompleteGroupedOptions = ({
@@ -10,7 +12,10 @@ const AutocompleteGroupedOptions = ({
10
12
  groupedLabelExtractor,
11
13
  options
12
14
  }) => {
13
- return /* @__PURE__ */ jsxRuntime.jsx("ul", { role: "listbox", className: list(), children: [...options ?? []].map(([key, value]) => /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
15
+ const { options: _options, isLoading } = useAutocompleteGroupedOptions.useAutocompleteGroupedOptions({
16
+ options
17
+ });
18
+ return isLoading ? /* @__PURE__ */ jsxRuntime.jsx(SkeletonOptions.SkeletonOptions, {}) : /* @__PURE__ */ jsxRuntime.jsx("ul", { role: "listbox", className: list(), children: [..._options ?? []].map(([key, value]) => /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
14
19
  /* @__PURE__ */ jsxRuntime.jsx("span", { className: groupedTitle(), children: groupedLabelExtractor?.(key) }),
15
20
  value.map((option) => /* @__PURE__ */ jsxRuntime.jsx(
16
21
  Option.AutocompleteOption,
@@ -4,6 +4,8 @@ var jsxRuntime = require('react/jsx-runtime');
4
4
  var cortexCore = require('@tecsinapse/cortex-core');
5
5
  var React = require('react');
6
6
  var Option = require('./Option.js');
7
+ var useAutocompleteOptions = require('../../hooks/useAutocompleteOptions.js');
8
+ var SkeletonOptions = require('../Select/SkeletonOptions.js');
7
9
 
8
10
  const { list } = cortexCore.selectVariants();
9
11
  const AutocompleteOptions = ({
@@ -11,8 +13,9 @@ const AutocompleteOptions = ({
11
13
  onSelect,
12
14
  children
13
15
  }) => {
16
+ const { options: _options, isLoading } = useAutocompleteOptions.useAutocompleteOptions({ options });
14
17
  const defaultOptions = React.useMemo(
15
- () => options?.map((option) => /* @__PURE__ */ jsxRuntime.jsx(
18
+ () => _options?.map((option) => /* @__PURE__ */ jsxRuntime.jsx(
16
19
  Option.AutocompleteOption,
17
20
  {
18
21
  option,
@@ -20,9 +23,9 @@ const AutocompleteOptions = ({
20
23
  },
21
24
  option.value
22
25
  )) ?? [],
23
- [options, onSelect]
26
+ [_options, onSelect]
24
27
  );
25
- return /* @__PURE__ */ jsxRuntime.jsx("ul", { role: "listbox", className: list(), children: children ?? defaultOptions });
28
+ return isLoading ? /* @__PURE__ */ jsxRuntime.jsx(SkeletonOptions.SkeletonOptions, {}) : /* @__PURE__ */ jsxRuntime.jsx("ul", { role: "listbox", className: list(), children: children ?? defaultOptions });
26
29
  };
27
30
 
28
31
  exports.AutocompleteOptions = AutocompleteOptions;
@@ -0,0 +1,35 @@
1
+ 'use strict';
2
+
3
+ var React = require('react');
4
+
5
+ const useAutocompleteGroupedOptions = ({
6
+ options: _options
7
+ }) => {
8
+ const [options, setOptions] = React.useState();
9
+ const [isLoading, setIsLoading] = React.useState();
10
+ const [error, setError] = React.useState();
11
+ const initData = async (fetch) => {
12
+ setIsLoading(true);
13
+ try {
14
+ const result = await fetch();
15
+ if (result) {
16
+ setOptions(result ?? []);
17
+ }
18
+ } catch (e) {
19
+ setError(String(e));
20
+ } finally {
21
+ setIsLoading(false);
22
+ }
23
+ };
24
+ React.useEffect(() => {
25
+ if (typeof _options === "function") initData(_options);
26
+ else setOptions(_options);
27
+ }, [_options]);
28
+ return {
29
+ isLoading,
30
+ options,
31
+ error
32
+ };
33
+ };
34
+
35
+ exports.useAutocompleteGroupedOptions = useAutocompleteGroupedOptions;
@@ -0,0 +1,35 @@
1
+ 'use strict';
2
+
3
+ var React = require('react');
4
+
5
+ const useAutocompleteOptions = ({
6
+ options: _options
7
+ }) => {
8
+ const [options, setOptions] = React.useState();
9
+ const [isLoading, setIsLoading] = React.useState();
10
+ const [error, setError] = React.useState();
11
+ const initData = React.useCallback(async (fetch) => {
12
+ setIsLoading(true);
13
+ try {
14
+ const result = await fetch();
15
+ if (result) {
16
+ setOptions(result ?? []);
17
+ }
18
+ } catch (e) {
19
+ setError(String(e));
20
+ } finally {
21
+ setIsLoading(false);
22
+ }
23
+ }, []);
24
+ React.useEffect(() => {
25
+ if (typeof _options === "function") initData(_options);
26
+ else setOptions(_options);
27
+ }, [_options]);
28
+ return {
29
+ isLoading,
30
+ options,
31
+ error
32
+ };
33
+ };
34
+
35
+ exports.useAutocompleteOptions = useAutocompleteOptions;
@@ -1,6 +1,8 @@
1
1
  import { jsx, jsxs } from 'react/jsx-runtime';
2
2
  import { selectVariants } from '@tecsinapse/cortex-core';
3
3
  import { AutocompleteOption } from './Option.js';
4
+ import { useAutocompleteGroupedOptions } from '../../hooks/useAutocompleteGroupedOptions.js';
5
+ import { SkeletonOptions } from '../Select/SkeletonOptions.js';
4
6
 
5
7
  const { groupedTitle, list } = selectVariants();
6
8
  const AutocompleteGroupedOptions = ({
@@ -8,7 +10,10 @@ const AutocompleteGroupedOptions = ({
8
10
  groupedLabelExtractor,
9
11
  options
10
12
  }) => {
11
- return /* @__PURE__ */ jsx("ul", { role: "listbox", className: list(), children: [...options ?? []].map(([key, value]) => /* @__PURE__ */ jsxs("div", { children: [
13
+ const { options: _options, isLoading } = useAutocompleteGroupedOptions({
14
+ options
15
+ });
16
+ return isLoading ? /* @__PURE__ */ jsx(SkeletonOptions, {}) : /* @__PURE__ */ jsx("ul", { role: "listbox", className: list(), children: [..._options ?? []].map(([key, value]) => /* @__PURE__ */ jsxs("div", { children: [
12
17
  /* @__PURE__ */ jsx("span", { className: groupedTitle(), children: groupedLabelExtractor?.(key) }),
13
18
  value.map((option) => /* @__PURE__ */ jsx(
14
19
  AutocompleteOption,
@@ -2,6 +2,8 @@ import { jsx } from 'react/jsx-runtime';
2
2
  import { selectVariants } from '@tecsinapse/cortex-core';
3
3
  import { useMemo } from 'react';
4
4
  import { AutocompleteOption } from './Option.js';
5
+ import { useAutocompleteOptions } from '../../hooks/useAutocompleteOptions.js';
6
+ import { SkeletonOptions } from '../Select/SkeletonOptions.js';
5
7
 
6
8
  const { list } = selectVariants();
7
9
  const AutocompleteOptions = ({
@@ -9,8 +11,9 @@ const AutocompleteOptions = ({
9
11
  onSelect,
10
12
  children
11
13
  }) => {
14
+ const { options: _options, isLoading } = useAutocompleteOptions({ options });
12
15
  const defaultOptions = useMemo(
13
- () => options?.map((option) => /* @__PURE__ */ jsx(
16
+ () => _options?.map((option) => /* @__PURE__ */ jsx(
14
17
  AutocompleteOption,
15
18
  {
16
19
  option,
@@ -18,9 +21,9 @@ const AutocompleteOptions = ({
18
21
  },
19
22
  option.value
20
23
  )) ?? [],
21
- [options, onSelect]
24
+ [_options, onSelect]
22
25
  );
23
- return /* @__PURE__ */ jsx("ul", { role: "listbox", className: list(), children: children ?? defaultOptions });
26
+ return isLoading ? /* @__PURE__ */ jsx(SkeletonOptions, {}) : /* @__PURE__ */ jsx("ul", { role: "listbox", className: list(), children: children ?? defaultOptions });
24
27
  };
25
28
 
26
29
  export { AutocompleteOptions };
@@ -0,0 +1,33 @@
1
+ import { useState, useEffect } from 'react';
2
+
3
+ const useAutocompleteGroupedOptions = ({
4
+ options: _options
5
+ }) => {
6
+ const [options, setOptions] = useState();
7
+ const [isLoading, setIsLoading] = useState();
8
+ const [error, setError] = useState();
9
+ const initData = async (fetch) => {
10
+ setIsLoading(true);
11
+ try {
12
+ const result = await fetch();
13
+ if (result) {
14
+ setOptions(result ?? []);
15
+ }
16
+ } catch (e) {
17
+ setError(String(e));
18
+ } finally {
19
+ setIsLoading(false);
20
+ }
21
+ };
22
+ useEffect(() => {
23
+ if (typeof _options === "function") initData(_options);
24
+ else setOptions(_options);
25
+ }, [_options]);
26
+ return {
27
+ isLoading,
28
+ options,
29
+ error
30
+ };
31
+ };
32
+
33
+ export { useAutocompleteGroupedOptions };
@@ -0,0 +1,33 @@
1
+ import { useState, useCallback, useEffect } from 'react';
2
+
3
+ const useAutocompleteOptions = ({
4
+ options: _options
5
+ }) => {
6
+ const [options, setOptions] = useState();
7
+ const [isLoading, setIsLoading] = useState();
8
+ const [error, setError] = useState();
9
+ const initData = useCallback(async (fetch) => {
10
+ setIsLoading(true);
11
+ try {
12
+ const result = await fetch();
13
+ if (result) {
14
+ setOptions(result ?? []);
15
+ }
16
+ } catch (e) {
17
+ setError(String(e));
18
+ } finally {
19
+ setIsLoading(false);
20
+ }
21
+ }, []);
22
+ useEffect(() => {
23
+ if (typeof _options === "function") initData(_options);
24
+ else setOptions(_options);
25
+ }, [_options]);
26
+ return {
27
+ isLoading,
28
+ options,
29
+ error
30
+ };
31
+ };
32
+
33
+ export { useAutocompleteOptions };
@@ -20,12 +20,12 @@ export interface AutocompletePopoverProps {
20
20
  className?: string;
21
21
  }
22
22
  export interface AutocompleteOptionsProps {
23
- options?: Option[];
23
+ options?: Option[] | (() => Promise<Option[]>);
24
24
  onSelect?: (option: Option) => void;
25
25
  children?: ReactNode;
26
26
  }
27
27
  export interface AutocompleteGroupedOptionsProps {
28
- options?: Map<string, Option[]>;
28
+ options?: Map<string, Option[]> | (() => Promise<Map<string, Option[]>>);
29
29
  groupedLabelExtractor: (value: string) => string;
30
30
  onSelect?: (option: Option) => void;
31
31
  }
@@ -34,11 +34,11 @@ export interface AutocompleteOptionProps {
34
34
  onSelect?: (option: Option) => void;
35
35
  grouped?: boolean;
36
36
  }
37
- export interface AutocompleteProps<T> {
37
+ export interface AutocompleteProps {
38
38
  inputValue: string;
39
39
  setInputValue: React.Dispatch<React.SetStateAction<string>>;
40
40
  onChange: ChangeEventHandler<HTMLInputElement>;
41
- options: Option[];
41
+ options: Option[] | (() => Promise<Option[]>);
42
42
  onSelect?: (option: Option) => void;
43
43
  label?: string;
44
44
  placeholder?: string;
@@ -0,0 +1,10 @@
1
+ import { Option } from '../components/Autocomplete/types';
2
+ interface useAutocompleteGroupedOptionsProps {
3
+ options?: Map<string, Option[]> | (() => Promise<Map<string, Option[]>>);
4
+ }
5
+ export declare const useAutocompleteGroupedOptions: ({ options: _options, }: useAutocompleteGroupedOptionsProps) => {
6
+ isLoading: boolean | undefined;
7
+ options: Map<string, Option[]> | undefined;
8
+ error: string | undefined;
9
+ };
10
+ export {};
@@ -0,0 +1,10 @@
1
+ import { Option } from '../components/Autocomplete/types';
2
+ interface useAutocompleteOptionsProps {
3
+ options?: Option[] | (() => Promise<Option[]>);
4
+ }
5
+ export declare const useAutocompleteOptions: ({ options: _options, }: useAutocompleteOptionsProps) => {
6
+ isLoading: boolean | undefined;
7
+ options: Option[] | undefined;
8
+ error: string | undefined;
9
+ };
10
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tecsinapse/cortex-react",
3
- "version": "2.2.0-beta.5",
3
+ "version": "2.2.0-beta.6",
4
4
  "description": "React components based in @tecsinapse/cortex-core",
5
5
  "license": "MIT",
6
6
  "main": "dist/esm/index.js",
@@ -48,5 +48,5 @@
48
48
  "react-icons": ">=5.2.0",
49
49
  "tailwindcss": "^4.1.16"
50
50
  },
51
- "gitHead": "bd4d4157b201d85cbbbe653fb1e080ca1034cebc"
51
+ "gitHead": "dc4acfa02813924f4707075b33eec14db030b521"
52
52
  }