@wonderflow/react-components 8.3.0 → 8.4.0
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.
|
@@ -2,21 +2,28 @@ import { AutocompleteOptionProps } from './autocomplete-option';
|
|
|
2
2
|
import { ForwardRefExoticComponent, ReactNode } from 'react';
|
|
3
3
|
import { TextfieldProps } from '../..';
|
|
4
4
|
import { MenuProps } from '../menu';
|
|
5
|
+
declare type ValueType = {
|
|
6
|
+
query?: string;
|
|
7
|
+
value?: string;
|
|
8
|
+
};
|
|
5
9
|
export declare type AutocompleteProps = TextfieldProps & {
|
|
6
10
|
/**
|
|
7
11
|
* The callback called when an option is picked from the list
|
|
8
12
|
*/
|
|
9
|
-
onChange?(value
|
|
13
|
+
onChange?(value: ValueType): void;
|
|
10
14
|
/**
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
15
|
+
* Set the maximum height of the options list after which
|
|
16
|
+
* it will scroll.
|
|
17
|
+
*/
|
|
14
18
|
maxHeight?: MenuProps['maxHeight'];
|
|
15
19
|
/**
|
|
16
20
|
* Custom empty content to display when there are no options or
|
|
17
21
|
* when the value does not match any of the options.
|
|
18
22
|
*/
|
|
19
23
|
emptyContent?: ReactNode;
|
|
24
|
+
/**
|
|
25
|
+
* Show skeletons while loading options.
|
|
26
|
+
*/
|
|
20
27
|
busy?: boolean;
|
|
21
28
|
};
|
|
22
29
|
declare type AutocompleteComponent = ForwardRefExoticComponent<AutocompleteProps> & {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"autocomplete.d.ts","sourceRoot":"","sources":["../../../src/components/autocomplete/autocomplete.tsx"],"names":[],"mappings":"AACA,OAAO,EAAsB,uBAAuB,EAAE,MAAM,uBAAuB,CAAA;
|
|
1
|
+
{"version":3,"file":"autocomplete.d.ts","sourceRoot":"","sources":["../../../src/components/autocomplete/autocomplete.tsx"],"names":[],"mappings":"AACA,OAAO,EAAsB,uBAAuB,EAAE,MAAM,uBAAuB,CAAA;AAEnF,OAAO,EACK,yBAAyB,EAEd,SAAS,EAC/B,MAAM,OAAO,CAAA;AAEd,OAAO,EAAyB,cAAc,EAAmB,MAAM,cAAc,CAAA;AAIrF,OAAO,EAAE,SAAS,EAAE,MAAM,SAAS,CAAA;AAEnC,aAAK,SAAS,GAAG;IACf,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,KAAK,CAAC,EAAE,MAAM,CAAA;CACf,CAAA;AAED,oBAAY,iBAAiB,GAAG,cAAc,GAAG;IAC/C;;OAEG;IACH,QAAQ,CAAC,CAAC,KAAK,EAAE,SAAS,GAAG,IAAI,CAAC;IACjC;;;MAGE;IACH,SAAS,CAAC,EAAE,SAAS,CAAC,WAAW,CAAC,CAAC;IACnC;;;OAGG;IACH,YAAY,CAAC,EAAE,SAAS,CAAC;IACzB;;OAEG;IACH,IAAI,CAAC,EAAE,OAAO,CAAC;CAChB,CAAC;AAEF,aAAK,qBAAqB,GAAG,yBAAyB,CAAC,iBAAiB,CAAC,GAAG;IAC1E,MAAM,EAAE,yBAAyB,CAAC,uBAAuB,CAAC,CAAC;CAC5D,CAAA;AAqBD,eAAO,MAAM,YAAY,uBAyJE,CAAA"}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
2
|
import { domMax, LazyMotion, m, AnimatePresence } from 'framer-motion';
|
|
3
3
|
import { AutocompleteOption } from './autocomplete-option';
|
|
4
|
+
import mergeRefs from 'react-merge-refs';
|
|
4
5
|
import { Children, cloneElement, forwardRef, useCallback, useMemo, useRef, useState, useEffect, isValidElement } from 'react';
|
|
5
6
|
import { useDebounce, useFocusWithin, useKeyPress, useSize } from 'ahooks';
|
|
6
7
|
import { Text, Menu, Textfield, Skeleton, Stack } from '../..';
|
|
@@ -25,22 +26,24 @@ const AutocompleteAnimation = {
|
|
|
25
26
|
}
|
|
26
27
|
}
|
|
27
28
|
};
|
|
28
|
-
export const Autocomplete = forwardRef(({ children, onChange, disabled, readOnly, value, busy, maxHeight = '200px', emptyContent = 'No items to show', ...otherProps }, forwardedRef) => {
|
|
29
|
+
export const Autocomplete = forwardRef(({ children, onChange, disabled, readOnly, value: val, busy, maxHeight = '200px', emptyContent = 'No items to show', ...otherProps }, forwardedRef) => {
|
|
29
30
|
const seedID = useUIDSeed();
|
|
30
|
-
const autocompleteRef = useRef(
|
|
31
|
+
const autocompleteRef = useRef(null);
|
|
31
32
|
const [isOpen, setIsOpen] = useState(false);
|
|
32
|
-
const [
|
|
33
|
+
const [query, setQuery] = useState('');
|
|
34
|
+
const [value, setValue] = useState(val ? String(val) : '');
|
|
35
|
+
const [optionsValues, setOptionValues] = useState([]);
|
|
33
36
|
const isInteractive = useMemo(() => !disabled && !readOnly, [disabled, readOnly]);
|
|
34
|
-
const
|
|
37
|
+
const debounceQuery = useDebounce(query, { wait: 100 });
|
|
35
38
|
const filteredOptions = useMemo(() => {
|
|
36
39
|
const items = Children.toArray(children);
|
|
37
|
-
return
|
|
40
|
+
return debounceQuery
|
|
38
41
|
? items.filter((o) => {
|
|
39
42
|
const stringToMatch = typeof o.props.children === 'string' ? o.props.children : o.props.children.join('');
|
|
40
|
-
return stringToMatch?.toLowerCase().includes(
|
|
43
|
+
return stringToMatch?.toLowerCase().includes(debounceQuery.toLowerCase());
|
|
41
44
|
})
|
|
42
45
|
: items;
|
|
43
|
-
}, [
|
|
46
|
+
}, [debounceQuery, children]);
|
|
44
47
|
const isFocusWithin = useFocusWithin(autocompleteRef, {
|
|
45
48
|
onChange: (isFocusWithin) => {
|
|
46
49
|
isFocusWithin ? setIsOpen(true) : setIsOpen(false);
|
|
@@ -58,17 +61,25 @@ export const Autocomplete = forwardRef(({ children, onChange, disabled, readOnly
|
|
|
58
61
|
});
|
|
59
62
|
const triggerSize = useSize(triggerRef);
|
|
60
63
|
const handleOptionClick = useCallback((optionValue, optionContent) => {
|
|
61
|
-
|
|
64
|
+
setQuery(optionContent);
|
|
65
|
+
setValue(optionValue);
|
|
66
|
+
onChange?.({ query: optionContent, value: optionValue });
|
|
62
67
|
setIsOpen(false);
|
|
63
|
-
onChange && onChange(optionValue);
|
|
64
68
|
}, [onChange]);
|
|
65
69
|
const handleFilter = useCallback(({ currentTarget }) => {
|
|
66
|
-
|
|
67
|
-
|
|
70
|
+
setQuery(currentTarget.value);
|
|
71
|
+
setValue(currentTarget.value);
|
|
72
|
+
onChange?.({
|
|
73
|
+
query: currentTarget.value,
|
|
74
|
+
value: optionsValues.includes(currentTarget.value) ? currentTarget.value : ''
|
|
75
|
+
});
|
|
76
|
+
}, [onChange, optionsValues]);
|
|
68
77
|
useEffect(() => {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
78
|
+
const currentValues = Children.map(filteredOptions, (o) => o.props.value);
|
|
79
|
+
val && setValue(String(val));
|
|
80
|
+
setOptionValues(currentValues);
|
|
81
|
+
}, [filteredOptions, val]);
|
|
82
|
+
return (_jsxs("div", { ref: autocompleteRef, className: styles.Autocomplete, "data-focus-within": isFocusWithin, children: [_jsx(Textfield, { ref: mergeRefs([setTriggerRef, forwardedRef]), id: seedID('autocomplete-trigger'), "aria-haspopup": "true", "aria-controls": seedID('autocomplete-menu'), "aria-expanded": isOpen, onChange: handleFilter, "data-current-value": value, value: query, autoComplete: "off", disabled: disabled, readOnly: readOnly, ...otherProps }, seedID('autocomplete-trigger')), _jsx(AnimatePresence, { children: visible && (_jsx("div", { ref: setTooltipRef, ...getTooltipProps({ className: styles.PopUp, style: { minInlineSize: triggerSize ? (triggerSize.width + 2) : 'auto' } }), children: _jsx(LazyMotion, { features: domMax, strict: true, children: _jsx(m.div, { variants: AutocompleteAnimation, initial: "hidden", animate: "visible", exit: "hidden", children: _jsx(Menu, { role: "listbox", id: seedID('autocomplete-menu'), className: styles.OptionsList, maxHeight: maxHeight, "aria-labelledby": seedID('autocomplete-trigger'), children: (filteredOptions.length === 0 && !busy)
|
|
72
83
|
? _jsx(Text, { as: "div", textAlign: "center", dimmed: 5, children: emptyContent })
|
|
73
84
|
: busy
|
|
74
85
|
? _jsx(Stack, { hPadding: 8, as: "li", children: _jsx(Skeleton, { count: 3 }) })
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wonderflow/react-components",
|
|
3
|
-
"version": "8.
|
|
3
|
+
"version": "8.4.0",
|
|
4
4
|
"description": "UI components from Wonderflow's Wanda design system",
|
|
5
5
|
"author": "Wonderflow",
|
|
6
6
|
"license": "MIT",
|
|
@@ -92,6 +92,7 @@
|
|
|
92
92
|
"framer-motion": "6.3.2",
|
|
93
93
|
"react-focus-on": "3.5.4",
|
|
94
94
|
"react-masonry-css": "1.0.16",
|
|
95
|
+
"react-merge-refs": "1.1.0",
|
|
95
96
|
"react-paginate": "8.1.3",
|
|
96
97
|
"react-popper-tooltip": "4.3.1",
|
|
97
98
|
"react-roving-tabindex": "3.2.0",
|