@smart-react-components/ui 2.1.0 → 2.1.1

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.
@@ -4,6 +4,7 @@ import React from 'react';
4
4
  export interface Props extends GenericProps {
5
5
  dropdownArrowIconElementProps?: JSXElementProps;
6
6
  dropdownPalette?: PaletteProp;
7
+ hasSearch?: boolean;
7
8
  isDropdownOutline?: boolean;
8
9
  placeholder?: string;
9
10
  }
@@ -24,12 +24,15 @@ const Close_1 = __importDefault(require("../icons/Close"));
24
24
  const InputElement_1 = __importDefault(require("../components/Input/InputElement"));
25
25
  const InputPlaceholder_1 = __importDefault(require("../components/Input/InputPlaceholder"));
26
26
  const useSelectBoxHover_1 = __importDefault(require("../hooks/useSelectBoxHover"));
27
+ const useSelectSearch_1 = __importDefault(require("../hooks/useSelectSearch"));
27
28
  const form_1 = require("../util/form");
28
29
  const props_1 = require("../util/props");
29
30
  const wave_effect_1 = require("../util/wave-effect");
31
+ const DropdownItem_1 = __importDefault(require("../Dropdown/DropdownItem"));
32
+ const Input_1 = __importDefault(require("../Input"));
30
33
  const SelectAddon_1 = __importDefault(require("./SelectAddon"));
31
34
  const SelectBox = react_1.default.forwardRef((props, forwardRef) => {
32
- var _a, _b, _c, _d, _e, _f, _g, _h;
35
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
33
36
  const theme = (0, styled_components_1.useTheme)();
34
37
  const formEl = react_1.default.useRef(null);
35
38
  const { itemList } = (0, useSelectBoxItemList_1.default)({ children: props.children });
@@ -54,10 +57,16 @@ const SelectBox = react_1.default.forwardRef((props, forwardRef) => {
54
57
  itemList[String(item)].children)))));
55
58
  }, [props.active, props.placeholder, itemList]);
56
59
  const [dropdownStatus, setDropdownStatus] = react_1.default.useState(false);
60
+ const [searchValue, setSearchValue] = react_1.default.useState('');
57
61
  const waveEffectPalette = react_1.default.useMemo(() => (0, wave_effect_1.getWaveEffectPalette)(props, theme.$.vars.isDarkMode), [props.waveEffectPalette, props.palette, props.isOutline, props.isSoft, theme.$.vars.isDarkMode]);
62
+ const { optionList } = (0, useSelectSearch_1.default)({
63
+ children: props.children,
64
+ dropdownStatus,
65
+ searchValue,
66
+ });
58
67
  const { hovered, setHovered } = (0, useSelectBoxHover_1.default)({
59
68
  active: props.active,
60
- children: Array.isArray(props.children) ? props.children : [props.children],
69
+ children: optionList,
61
70
  dropdownStatus,
62
71
  hasHover: props.hasHover,
63
72
  isDisabled: props.isDisabled,
@@ -67,7 +76,7 @@ const SelectBox = react_1.default.forwardRef((props, forwardRef) => {
67
76
  (0, useChangeEffect_1.default)(() => {
68
77
  const event = new Event('src.fixedBox.setPosition');
69
78
  formEl.current.dispatchEvent(event);
70
- }, [content]);
79
+ }, [content, optionList]);
71
80
  const { leftAddon, rightAddon } = (0, useAddons_1.default)({
72
81
  Component: SelectAddon_1.default,
73
82
  leftAddon: props.leftAddon,
@@ -88,6 +97,9 @@ const SelectBox = react_1.default.forwardRef((props, forwardRef) => {
88
97
  sizeXl: props.sizeXl,
89
98
  },
90
99
  });
100
+ (0, useChangeEffect_1.default)(() => {
101
+ setHovered(undefined);
102
+ }, [optionList]);
91
103
  const handleOptionClick = active => {
92
104
  props.setActive(active);
93
105
  if (!Array.isArray(active)) {
@@ -107,16 +119,29 @@ const SelectBox = react_1.default.forwardRef((props, forwardRef) => {
107
119
  react_1.default.createElement(DropdownArrowIcon_1.default, Object.assign({ fill: "currentcolor" }, (0, props_1.applyResponsiveStyledProp)(props, 'size', 'iconSize', v => `$size.icon.${v}`), props.dropdownArrowIconElementProps))),
108
120
  (rightAddon && !((_e = rightAddon === null || rightAddon === void 0 ? void 0 : rightAddon.props) === null || _e === void 0 ? void 0 : _e.isSeparated)) && rightAddon),
109
121
  ((_f = rightAddon === null || rightAddon === void 0 ? void 0 : rightAddon.props) === null || _f === void 0 ? void 0 : _f.isSeparated) && rightAddon)),
110
- react_1.default.createElement(DropdownListElement_1.default, { dropdownListSize: props.size, dropdownListSizeSm: props.sizeSm, dropdownListSizeMd: props.sizeMd, dropdownListSizeLg: props.sizeLg, dropdownListSizeXl: props.sizeXl, isOutline: (_g = props.isDropdownOutline) !== null && _g !== void 0 ? _g : props.isOutline, isSoft: props.isSoft, palette: (_h = props.dropdownPalette) !== null && _h !== void 0 ? _h : props.palette }, (Array.isArray(props.children) ? props.children : [props.children]).map((item, idx) => {
111
- var _a, _b, _c;
112
- return item && react_1.default.cloneElement(item, Object.assign({ key: (_a = item.key) !== null && _a !== void 0 ? _a : idx, active: props.active, cursorKey: 'selectBox', hasHover: false, hasWaveEffect: props.hasWaveEffect, hovered, isEmbedded: false, isOutline: (_b = props.isDropdownOutline) !== null && _b !== void 0 ? _b : props.isOutline, isSoft: props.isSoft, palette: (_c = props.dropdownPalette) !== null && _c !== void 0 ? _c : props.palette, setActive: handleOptionClick, setHovered: props.hasHover ? setHovered : null, waveEffectPalette }, (props.isDisabled && { isDisabled: true })));
113
- }))));
122
+ react_1.default.createElement(DropdownListElement_1.default, { dropdownListSize: props.size, dropdownListSizeSm: props.sizeSm, dropdownListSizeMd: props.sizeMd, dropdownListSizeLg: props.sizeLg, dropdownListSizeXl: props.sizeXl, isOutline: (_g = props.isDropdownOutline) !== null && _g !== void 0 ? _g : props.isOutline, isSoft: props.isSoft, palette: (_h = props.dropdownPalette) !== null && _h !== void 0 ? _h : props.palette },
123
+ props.hasSearch && (react_1.default.createElement(DropdownItem_1.default, Object.assign({ elementProps: {
124
+ cursor: 'initial',
125
+ } }, {
126
+ hasHover: false,
127
+ hasWaveEffect: false,
128
+ isOutline: (_j = props.isDropdownOutline) !== null && _j !== void 0 ? _j : props.isOutline,
129
+ isSoft: props.isSoft,
130
+ palette: (_k = props.dropdownPalette) !== null && _k !== void 0 ? _k : props.palette,
131
+ setStatus: () => { },
132
+ }),
133
+ react_1.default.createElement(Input_1.default, { palette: (_l = props.dropdownPalette) !== null && _l !== void 0 ? _l : props.palette, placeholder: "Search...", setValue: setSearchValue, value: searchValue }))),
134
+ optionList.map((item, idx) => {
135
+ var _a, _b, _c;
136
+ return item && react_1.default.cloneElement(item, Object.assign({ key: (_a = item.key) !== null && _a !== void 0 ? _a : idx, active: props.active, cursorKey: 'selectBox', hasHover: false, hasWaveEffect: props.hasWaveEffect, hovered, isEmbedded: false, isOutline: (_b = props.isDropdownOutline) !== null && _b !== void 0 ? _b : props.isOutline, isSoft: props.isSoft, palette: (_c = props.dropdownPalette) !== null && _c !== void 0 ? _c : props.palette, setActive: handleOptionClick, setHovered: props.hasHover ? setHovered : null, waveEffectPalette }, (props.isDisabled && { isDisabled: true })));
137
+ }))));
114
138
  });
115
139
  SelectBox.defaultProps = {
116
140
  dropdownArrowIconElementProps: {},
117
141
  elementProps: {},
118
142
  hasBorder: true,
119
143
  hasHover: true,
144
+ hasSearch: false,
120
145
  hasWaveEffect: true,
121
146
  isBlock: true,
122
147
  isOutline: true,
@@ -15,48 +15,18 @@ const react_1 = __importDefault(require("react"));
15
15
  const styled_components_1 = require("styled-components");
16
16
  const DropdownListElement_1 = __importDefault(require("../components/Dropdown/DropdownListElement"));
17
17
  const useSelectBoxHover_1 = __importDefault(require("../hooks/useSelectBoxHover"));
18
+ const useSelectSearch_1 = __importDefault(require("../hooks/useSelectSearch"));
18
19
  const wave_effect_1 = require("../util/wave-effect");
19
20
  const Dropdown_1 = __importDefault(require("../Dropdown"));
20
21
  const Input_1 = __importDefault(require("../Input"));
21
22
  const SelectSearchBox = react_1.default.forwardRef((props, forwardRef) => {
22
23
  const theme = (0, styled_components_1.useTheme)();
23
24
  const [dropdownStatus, setDropdownStatus] = react_1.default.useState(false);
24
- const lastOptionList = react_1.default.useRef([]);
25
- const applySearch = react_1.default.useCallback((children, list, value) => {
26
- children.forEach(item => {
27
- switch (item.type.displayName) {
28
- case 'Option':
29
- if (!value || item.props.value.trim().toLowerCase().includes(value)) {
30
- list.push(item);
31
- }
32
- break;
33
- case 'OptionGroup':
34
- const group = react_1.default.cloneElement(item, { children: [] });
35
- if (item.props.children) {
36
- applySearch(Array.isArray(item.props.children) ? item.props.children : [item.props.children], group.props.children, value);
37
- }
38
- if (group.props.children.length > 0) {
39
- list.push(group);
40
- }
41
- break;
42
- }
43
- });
44
- }, []);
45
- const optionList = react_1.default.useMemo(() => {
46
- /**
47
- * Returns the same list when the dropdown gets closed since it is not needed to update during the closing animation.
48
- */
49
- if (!dropdownStatus && lastOptionList.current.length > 0) {
50
- return lastOptionList.current;
51
- }
52
- const children = [];
53
- const value = props.value.trim().toLowerCase();
54
- applySearch(Array.isArray(props.children) ? props.children : [props.children], children, value);
55
- if (dropdownStatus) {
56
- lastOptionList.current = children;
57
- }
58
- return children;
59
- }, [props.children, props.value, dropdownStatus]);
25
+ const { optionList } = (0, useSelectSearch_1.default)({
26
+ children: props.children,
27
+ dropdownStatus,
28
+ searchValue: props.value,
29
+ });
60
30
  const { hovered, setHovered } = (0, useSelectBoxHover_1.default)({
61
31
  active: props.value,
62
32
  children: optionList,
@@ -0,0 +1,12 @@
1
+ /// <reference types="react" />
2
+ import { JSXChildren } from '@smart-react-components/core/types';
3
+ interface Props {
4
+ children: JSXChildren;
5
+ dropdownStatus: boolean;
6
+ searchValue: string;
7
+ }
8
+ interface Return {
9
+ optionList: JSX.Element[];
10
+ }
11
+ declare const useSelectSearch: (props: Props) => Return;
12
+ export default useSelectSearch;
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const react_1 = require("react");
4
+ const useSelectSearch = (props) => {
5
+ const lastOptionList = (0, react_1.useRef)([]);
6
+ const applySearch = (0, react_1.useCallback)((children, list, value) => {
7
+ children.forEach(item => {
8
+ switch (item.type.displayName) {
9
+ case 'Option':
10
+ if (!value
11
+ || item.props.value.trim().toLowerCase().includes(value)
12
+ || (typeof item.props.children === 'string' && item.props.children.trim().toLowerCase().includes(value))) {
13
+ list.push(item);
14
+ }
15
+ break;
16
+ case 'OptionGroup':
17
+ const group = (0, react_1.cloneElement)(item, { children: [] });
18
+ if (item.props.children) {
19
+ applySearch(Array.isArray(item.props.children) ? item.props.children : [item.props.children], group.props.children, value);
20
+ }
21
+ if (group.props.children.length > 0) {
22
+ list.push(group);
23
+ }
24
+ break;
25
+ }
26
+ });
27
+ }, []);
28
+ const optionList = (0, react_1.useMemo)(() => {
29
+ /**
30
+ * Returns the same list when the dropdown gets closed since it is not needed to update during the closing animation.
31
+ */
32
+ if (!props.dropdownStatus && lastOptionList.current.length > 0) {
33
+ return lastOptionList.current;
34
+ }
35
+ const children = [];
36
+ const value = props.searchValue.trim().toLowerCase();
37
+ applySearch(Array.isArray(props.children) ? props.children : [props.children], children, value);
38
+ if (props.dropdownStatus) {
39
+ lastOptionList.current = children;
40
+ }
41
+ return children;
42
+ }, [props.children, props.dropdownStatus, props.searchValue]);
43
+ return {
44
+ optionList,
45
+ };
46
+ };
47
+ exports.default = useSelectSearch;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@smart-react-components/ui",
3
- "version": "2.1.0",
3
+ "version": "2.1.1",
4
4
  "license": "MIT",
5
5
  "main": "index.js",
6
6
  "description": "SRC UI includes React and Styled components.",