@spaced-out/ui-design-system 0.3.37 → 0.3.38

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 (28) hide show
  1. package/.cspell/custom-words.txt +1 -0
  2. package/CHANGELOG.md +9 -0
  3. package/lib/components/Chip/Chip.js +2 -1
  4. package/lib/components/Chip/Chip.js.flow +2 -0
  5. package/lib/components/TokenListInput/TokenListInput.js +237 -0
  6. package/lib/components/TokenListInput/TokenListInput.js.flow +344 -0
  7. package/lib/components/TokenListInput/TokenListInput.module.css +147 -0
  8. package/lib/components/TokenListInput/TokenValueChips.js +59 -0
  9. package/lib/components/TokenListInput/TokenValueChips.js.flow +71 -0
  10. package/lib/components/TokenListInput/index.js +16 -0
  11. package/lib/components/TokenListInput/index.js.flow +3 -0
  12. package/lib/components/index.js +11 -0
  13. package/lib/components/index.js.flow +1 -0
  14. package/lib/hooks/index.js +22 -0
  15. package/lib/hooks/index.js.flow +2 -0
  16. package/lib/hooks/useArbitraryOptionAddition/index.js +16 -0
  17. package/lib/hooks/useArbitraryOptionAddition/index.js.flow +3 -0
  18. package/lib/hooks/useArbitraryOptionAddition/useArbitraryOptionAddition.js +98 -0
  19. package/lib/hooks/useArbitraryOptionAddition/useArbitraryOptionAddition.js.flow +117 -0
  20. package/lib/hooks/useFilteredOptions/index.js +16 -0
  21. package/lib/hooks/useFilteredOptions/index.js.flow +3 -0
  22. package/lib/hooks/useFilteredOptions/useFilteredOptions.js +62 -0
  23. package/lib/hooks/useFilteredOptions/useFilteredOptions.js.flow +81 -0
  24. package/lib/hooks/useInputState/useInputState.js +5 -3
  25. package/lib/hooks/useInputState/useInputState.js.flow +3 -3
  26. package/lib/utils/token-list-input/token-list-input.js +28 -0
  27. package/lib/utils/token-list-input/token-list-input.js.flow +37 -0
  28. package/package.json +1 -1
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.useFilteredOptions = useFilteredOptions;
7
+ var React = _interopRequireWildcard(require("react"));
8
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
9
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
10
+
11
+ // TODO: V should generic with constraints.
12
+
13
+ function useFilteredOptions(_ref) {
14
+ let {
15
+ searchTerm,
16
+ options = [],
17
+ excludedKeys = [],
18
+ groupTitleOptions = [],
19
+ searchOptionsBy = (option, searchTerm) => {
20
+ // $FlowFixMe
21
+ const {
22
+ label,
23
+ key
24
+ } = option;
25
+ return key.toLowerCase().includes(searchTerm) || (label ? label.toLowerCase().includes(searchTerm) : false);
26
+ }
27
+ } = _ref;
28
+ // Memoize the filterExcluded function for performance
29
+ const filterExcluded = React.useCallback(list =>
30
+ // $FlowFixMe - list has key property
31
+ list ? list.filter(option => !excludedKeys.includes(option.key)) : [], [excludedKeys]);
32
+ return React.useMemo(() => {
33
+ const trimmedValue = (searchTerm || '').trim().toLowerCase();
34
+
35
+ // Initialize filteredOptions as an empty array if options are not provided
36
+ let filteredOptions = [];
37
+ if (options.length) {
38
+ filteredOptions = filterExcluded(options);
39
+
40
+ // Apply search term filtering
41
+ if (trimmedValue) {
42
+ filteredOptions = filteredOptions.filter(option => searchOptionsBy(option, trimmedValue));
43
+ }
44
+ }
45
+ let filteredGroupTitleOptions = [];
46
+
47
+ // Process groupTitleOptions if provided
48
+ if (Array.isArray(groupTitleOptions) && groupTitleOptions.length) {
49
+ filteredGroupTitleOptions = groupTitleOptions.map(group => {
50
+ const filtered = filterExcluded(group.options).filter(option => !trimmedValue || searchOptionsBy(option, trimmedValue));
51
+ return {
52
+ ...group,
53
+ options: filtered
54
+ };
55
+ }).filter(group => group.options ? group.options.length > 0 : false);
56
+ }
57
+ return {
58
+ filteredOptions,
59
+ filteredGroupTitleOptions
60
+ };
61
+ }, [searchTerm, options, excludedKeys, groupTitleOptions, searchOptionsBy, filterExcluded]);
62
+ }
@@ -0,0 +1,81 @@
1
+ // @flow strict
2
+ import * as React from 'react';
3
+
4
+
5
+ export type GroupTitleOption<V> = {
6
+ groupTitle: React.Node,
7
+ options?: V[],
8
+ showLineDivider?: boolean,
9
+ };
10
+
11
+ // TODO: V should generic with constraints.
12
+
13
+ export function useFilteredOptions<V>({
14
+ searchTerm,
15
+ options = [],
16
+ excludedKeys = [],
17
+ groupTitleOptions = [],
18
+ searchOptionsBy = (option: V, searchTerm: string): boolean => {
19
+ // $FlowFixMe
20
+ const {label, key}: {label: string, key: string} = option;
21
+
22
+ return (
23
+ key.toLowerCase().includes(searchTerm) ||
24
+ (label ? label.toLowerCase().includes(searchTerm) : false)
25
+ );
26
+ },
27
+ }: {
28
+ searchTerm: string,
29
+ options?: V[],
30
+ groupTitleOptions?: GroupTitleOption<V>[],
31
+ excludedKeys?: string[],
32
+ searchOptionsBy?: (option: V, searchTerm: string) => boolean,
33
+ }): {filteredOptions?: V[], filteredGroupTitleOptions?: GroupTitleOption<V>[]} {
34
+ // Memoize the filterExcluded function for performance
35
+ const filterExcluded = React.useCallback(
36
+ (list?: V[]): V[] =>
37
+ // $FlowFixMe - list has key property
38
+ list ? list.filter((option) => !excludedKeys.includes(option.key)) : [],
39
+ [excludedKeys],
40
+ );
41
+
42
+ return React.useMemo(() => {
43
+ const trimmedValue = (searchTerm || '').trim().toLowerCase();
44
+
45
+ // Initialize filteredOptions as an empty array if options are not provided
46
+ let filteredOptions: V[] = [];
47
+ if (options.length) {
48
+ filteredOptions = filterExcluded(options);
49
+
50
+ // Apply search term filtering
51
+ if (trimmedValue) {
52
+ filteredOptions = filteredOptions.filter((option) =>
53
+ searchOptionsBy(option, trimmedValue),
54
+ );
55
+ }
56
+ }
57
+
58
+ let filteredGroupTitleOptions: GroupTitleOption<V>[] = [];
59
+
60
+ // Process groupTitleOptions if provided
61
+ if (Array.isArray(groupTitleOptions) && groupTitleOptions.length) {
62
+ filteredGroupTitleOptions = groupTitleOptions
63
+ .map((group) => {
64
+ const filtered = filterExcluded(group.options).filter(
65
+ (option) => !trimmedValue || searchOptionsBy(option, trimmedValue),
66
+ );
67
+ return {...group, options: filtered};
68
+ })
69
+ .filter((group) => (group.options ? group.options.length > 0 : false));
70
+ }
71
+
72
+ return {filteredOptions, filteredGroupTitleOptions};
73
+ }, [
74
+ searchTerm,
75
+ options,
76
+ excludedKeys,
77
+ groupTitleOptions,
78
+ searchOptionsBy,
79
+ filterExcluded,
80
+ ]);
81
+ }
@@ -4,7 +4,9 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.useInputState = void 0;
7
- var _react = require("react");
7
+ var React = _interopRequireWildcard(require("react"));
8
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
9
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
8
10
 
9
11
  /**
10
12
  * Simple Hook to use with input tag. The primary purpose is an abstraction over input onChange property.
@@ -16,8 +18,8 @@ var _react = require("react");
16
18
  */
17
19
  const useInputState = function () {
18
20
  let initialState = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
19
- const [state, setState] = (0, _react.useState)(initialState);
20
- const setInputState = (0, _react.useCallback)(event => setState(event && event.target.value), []);
21
+ const [state, setState] = React.useState(initialState);
22
+ const setInputState = React.useCallback(event => setState(event && event.target.value), []);
21
23
  return [state, setInputState];
22
24
  };
23
25
  exports.useInputState = useInputState;
@@ -1,6 +1,6 @@
1
1
  // @flow strict
2
2
 
3
- import {useCallback, useState} from 'react';
3
+ import * as React from 'react';
4
4
 
5
5
 
6
6
  type ReturnType = [
@@ -17,9 +17,9 @@ type ReturnType = [
17
17
  * }
18
18
  */
19
19
  export const useInputState = (initialState: string = ''): ReturnType => {
20
- const [state, setState] = useState(initialState);
20
+ const [state, setState] = React.useState(initialState);
21
21
 
22
- const setInputState = useCallback(
22
+ const setInputState = React.useCallback(
23
23
  (event) => setState(event && event.target.value),
24
24
  [],
25
25
  );
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.getFirstOptionFromGroup = exports.getFirstOption = void 0;
7
+ var React = _interopRequireWildcard(require("react"));
8
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
9
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
10
+
11
+ const getFirstOption = options => {
12
+ // pick the first option from the options
13
+ if (options.length > 0) {
14
+ return options[0];
15
+ }
16
+ return null;
17
+ };
18
+ exports.getFirstOption = getFirstOption;
19
+ const getFirstOptionFromGroup = groupTitleOptions => {
20
+ if (groupTitleOptions.length > 0) {
21
+ const firstGroup = groupTitleOptions[0];
22
+ if (firstGroup.options && firstGroup.options.length > 0) {
23
+ return firstGroup.options[0];
24
+ }
25
+ }
26
+ return null;
27
+ };
28
+ exports.getFirstOptionFromGroup = getFirstOptionFromGroup;
@@ -0,0 +1,37 @@
1
+ // @flow strict
2
+ import * as React from 'react';
3
+
4
+
5
+ type V = {
6
+ key: string,
7
+ label: string,
8
+ arbitrary?: boolean,
9
+ multiarbitrary?: boolean,
10
+ ...
11
+ };
12
+
13
+ type Group<V> = {
14
+ options: Array<V>,
15
+ groupTitle?: React.Node,
16
+ ...
17
+ };
18
+
19
+ export const getFirstOption = (options: Array<V>): V | null => {
20
+ // pick the first option from the options
21
+ if (options.length > 0) {
22
+ return options[0];
23
+ }
24
+ return null;
25
+ };
26
+
27
+ export const getFirstOptionFromGroup = (
28
+ groupTitleOptions: Array<Group<V>>,
29
+ ): V | null => {
30
+ if (groupTitleOptions.length > 0) {
31
+ const firstGroup = groupTitleOptions[0];
32
+ if (firstGroup.options && firstGroup.options.length > 0) {
33
+ return firstGroup.options[0];
34
+ }
35
+ }
36
+ return null;
37
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@spaced-out/ui-design-system",
3
- "version": "0.3.37",
3
+ "version": "0.3.38",
4
4
  "main": "index.js",
5
5
  "description": "Sense UI components library",
6
6
  "author": {