@addev-be/ui 0.3.3 → 0.3.5
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.
- package/package.json +2 -1
- package/src/components/index.ts +1 -0
- package/src/components/layout/Dropdown/index.tsx +3 -2
- package/src/components/layout/Dropdown/styles.ts +3 -2
- package/src/components/search/HighlightedText.tsx +30 -0
- package/src/components/search/QuickSearchBar.tsx +78 -0
- package/src/components/search/QuickSearchResults.tsx +86 -0
- package/src/components/search/index.ts +5 -0
- package/src/components/search/styles.ts +60 -0
- package/src/components/search/types.ts +26 -0
- package/src/helpers/index.ts +2 -0
- package/src/index.ts +1 -0
- package/src/providers/ThemeProvider/index.ts +1 -1
- package/src/services/index.ts +1 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@addev-be/ui",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.5",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"watch": "tsc -b --watch",
|
|
@@ -41,6 +41,7 @@
|
|
|
41
41
|
"vite-plugin-svgr": "^4.2.0"
|
|
42
42
|
},
|
|
43
43
|
"dependencies": {
|
|
44
|
+
"@uidotdev/usehooks": "^2.4.1",
|
|
44
45
|
"fp-ts": "^2.16.9",
|
|
45
46
|
"io-ts": "^2.2.21",
|
|
46
47
|
"lodash": "^4.17.21",
|
package/src/components/index.ts
CHANGED
|
@@ -57,6 +57,7 @@ const getDropdownStyle = (dropdown: DropdownProps): CSSProperties => {
|
|
|
57
57
|
export const Dropdown: FC<DropdownProps> = ({
|
|
58
58
|
children,
|
|
59
59
|
onClose,
|
|
60
|
+
$backdropAlpha,
|
|
60
61
|
...props
|
|
61
62
|
}) => {
|
|
62
63
|
const { createPortal } = usePortals();
|
|
@@ -66,13 +67,13 @@ export const Dropdown: FC<DropdownProps> = ({
|
|
|
66
67
|
const modalPortal = useMemo(
|
|
67
68
|
() =>
|
|
68
69
|
createPortal(
|
|
69
|
-
<styles.DropdownBackdrop onClick={onClose}>
|
|
70
|
+
<styles.DropdownBackdrop onClick={onClose} $alpha={$backdropAlpha}>
|
|
70
71
|
<styles.DropdownContainer {...props} style={style}>
|
|
71
72
|
{children}
|
|
72
73
|
</styles.DropdownContainer>
|
|
73
74
|
</styles.DropdownBackdrop>
|
|
74
75
|
),
|
|
75
|
-
[children, createPortal, onClose, props, style]
|
|
76
|
+
[$backdropAlpha, children, createPortal, onClose, props, style]
|
|
76
77
|
);
|
|
77
78
|
|
|
78
79
|
return <>{modalPortal}</>;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import styled, { css } from 'styled-components';
|
|
2
2
|
|
|
3
|
-
export const DropdownBackdrop = styled.div.attrs({
|
|
3
|
+
export const DropdownBackdrop = styled.div.attrs<{ $alpha?: number }>({
|
|
4
4
|
className: 'DropdownBackdrop',
|
|
5
5
|
})`
|
|
6
6
|
position: absolute;
|
|
@@ -9,7 +9,7 @@ export const DropdownBackdrop = styled.div.attrs({
|
|
|
9
9
|
left: 0;
|
|
10
10
|
right: 0;
|
|
11
11
|
bottom: 0;
|
|
12
|
-
background
|
|
12
|
+
background: rgba(0, 0, 0, ${({ $alpha = 0.5 }) => $alpha});
|
|
13
13
|
display: flex;
|
|
14
14
|
justify-content: center;
|
|
15
15
|
align-items: center;
|
|
@@ -19,6 +19,7 @@ export type DropdownContainerProps = {
|
|
|
19
19
|
$width: number;
|
|
20
20
|
$height: number | number[];
|
|
21
21
|
$zIndex?: number;
|
|
22
|
+
$backdropAlpha?: number;
|
|
22
23
|
};
|
|
23
24
|
|
|
24
25
|
export const DropdownContainer = styled.div.attrs({
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { FC, HTMLAttributes } from 'react';
|
|
2
|
+
|
|
3
|
+
export const HighlightedText: FC<
|
|
4
|
+
{
|
|
5
|
+
text: string;
|
|
6
|
+
highlight?: string;
|
|
7
|
+
style?: React.CSSProperties;
|
|
8
|
+
} & HTMLAttributes<HTMLSpanElement>
|
|
9
|
+
> = ({ text, highlight, ...props }) => {
|
|
10
|
+
const parts = text?.split(new RegExp(`(${highlight})`, 'gi')) ?? [];
|
|
11
|
+
if (!highlight) return <span {...props}>{text}</span>;
|
|
12
|
+
return (
|
|
13
|
+
<span {...props}>
|
|
14
|
+
{parts
|
|
15
|
+
.filter((part) => !!part)
|
|
16
|
+
.map((part, index) => (
|
|
17
|
+
<span
|
|
18
|
+
key={index}
|
|
19
|
+
style={
|
|
20
|
+
part.toLowerCase() === highlight.toLowerCase()
|
|
21
|
+
? { backgroundColor: 'gold' }
|
|
22
|
+
: {}
|
|
23
|
+
}
|
|
24
|
+
>
|
|
25
|
+
{part}
|
|
26
|
+
</span>
|
|
27
|
+
))}
|
|
28
|
+
</span>
|
|
29
|
+
);
|
|
30
|
+
};
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { SearchResults, SearchTypeDefinitions } from './types';
|
|
2
|
+
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
3
|
+
|
|
4
|
+
import { Dropdown } from '../layout';
|
|
5
|
+
import { QuickSearchBarInput } from './styles';
|
|
6
|
+
import { QuickSearchResults } from './QuickSearchResults';
|
|
7
|
+
import { useDebounce } from '@uidotdev/usehooks';
|
|
8
|
+
import { useGlobalSearchRequestHandler } from '../../services';
|
|
9
|
+
|
|
10
|
+
type QuickSearchBarProps<R> = {
|
|
11
|
+
definitions: SearchTypeDefinitions<R>;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export const QuickSearchBar = <R,>({ definitions }: QuickSearchBarProps<R>) => {
|
|
15
|
+
const [term, setTerm] = useState('');
|
|
16
|
+
const [dropdownVisible, setDropdownVisible] = useState(false);
|
|
17
|
+
const debouncedTerm = useDebounce(term, 300);
|
|
18
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
19
|
+
const [results, setResults] = useState<SearchResults<R> | null>(null);
|
|
20
|
+
|
|
21
|
+
const inputRef = useRef<HTMLInputElement | null>(null);
|
|
22
|
+
const rect = inputRef.current?.getBoundingClientRect() ?? new DOMRect();
|
|
23
|
+
const globalSearch = useGlobalSearchRequestHandler();
|
|
24
|
+
|
|
25
|
+
useEffect(() => {
|
|
26
|
+
if (debouncedTerm) {
|
|
27
|
+
globalSearch({
|
|
28
|
+
types: ['Customer', 'ScanGroup'],
|
|
29
|
+
searchTerm: debouncedTerm,
|
|
30
|
+
limit: 5,
|
|
31
|
+
}).then((response) => {
|
|
32
|
+
setResults(response.data as SearchResults<R>);
|
|
33
|
+
setDropdownVisible(true);
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
}, [globalSearch, debouncedTerm]);
|
|
37
|
+
|
|
38
|
+
const onFocus = useCallback(() => {
|
|
39
|
+
if (term) {
|
|
40
|
+
setDropdownVisible(true);
|
|
41
|
+
}
|
|
42
|
+
}, [term]);
|
|
43
|
+
|
|
44
|
+
useEffect(() => {
|
|
45
|
+
const input = inputRef.current;
|
|
46
|
+
input?.addEventListener('focus', onFocus);
|
|
47
|
+
return () => {
|
|
48
|
+
input?.removeEventListener('focus', onFocus);
|
|
49
|
+
};
|
|
50
|
+
}, [onFocus]);
|
|
51
|
+
|
|
52
|
+
return (
|
|
53
|
+
<>
|
|
54
|
+
<QuickSearchBarInput
|
|
55
|
+
type="text"
|
|
56
|
+
value={term}
|
|
57
|
+
onChange={(e) => setTerm(e.target.value)}
|
|
58
|
+
ref={inputRef}
|
|
59
|
+
$opened={dropdownVisible}
|
|
60
|
+
/>
|
|
61
|
+
{results && dropdownVisible && rect && (
|
|
62
|
+
<Dropdown
|
|
63
|
+
$sourceRect={rect}
|
|
64
|
+
onClose={() => setDropdownVisible(false)}
|
|
65
|
+
$width={rect.width}
|
|
66
|
+
$height={[250, 400]}
|
|
67
|
+
$autoPositionY={false}
|
|
68
|
+
>
|
|
69
|
+
<QuickSearchResults
|
|
70
|
+
results={results}
|
|
71
|
+
definitions={definitions}
|
|
72
|
+
term={debouncedTerm}
|
|
73
|
+
/>
|
|
74
|
+
</Dropdown>
|
|
75
|
+
)}
|
|
76
|
+
</>
|
|
77
|
+
);
|
|
78
|
+
};
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import * as styles from './styles';
|
|
2
|
+
|
|
3
|
+
import { ReactNode, useMemo, useState } from 'react';
|
|
4
|
+
import { ResultArray, SearchResults, SearchTypeDefinitions } from './types';
|
|
5
|
+
|
|
6
|
+
import { pickBy } from 'lodash';
|
|
7
|
+
|
|
8
|
+
type QuickSearchResultsProps<R> = {
|
|
9
|
+
definitions: SearchTypeDefinitions<R>;
|
|
10
|
+
results: SearchResults<R>;
|
|
11
|
+
term: string;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export const QuickSearchResults = <R,>(props: QuickSearchResultsProps<R>) => {
|
|
15
|
+
const { definitions, results: allResults, term } = props;
|
|
16
|
+
const [highlightedIndex, setHighlightedIndex] = useState(-1);
|
|
17
|
+
|
|
18
|
+
const notEmptyResults = pickBy<R[keyof R][]>(
|
|
19
|
+
allResults,
|
|
20
|
+
(results) => results.length > 0
|
|
21
|
+
);
|
|
22
|
+
const resultsArray = useMemo(
|
|
23
|
+
() =>
|
|
24
|
+
Object.keys(notEmptyResults).reduce(
|
|
25
|
+
(acc, type) => [
|
|
26
|
+
...acc,
|
|
27
|
+
...notEmptyResults[type].map((result) => ({
|
|
28
|
+
result,
|
|
29
|
+
type: type as keyof R,
|
|
30
|
+
})),
|
|
31
|
+
],
|
|
32
|
+
[] as ResultArray<R>
|
|
33
|
+
),
|
|
34
|
+
[notEmptyResults]
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
const elements = useMemo(() => {
|
|
38
|
+
const elements: ReactNode[] = [];
|
|
39
|
+
let currentType: keyof R | null = null;
|
|
40
|
+
resultsArray.forEach(({ result, type }, index) => {
|
|
41
|
+
const definition = definitions[type];
|
|
42
|
+
if (currentType !== type) {
|
|
43
|
+
currentType = type;
|
|
44
|
+
elements.push(
|
|
45
|
+
<styles.QuickSearchResultsTitle key={`title-${String(type)}`}>
|
|
46
|
+
{definition.title}
|
|
47
|
+
</styles.QuickSearchResultsTitle>
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
elements.push(
|
|
51
|
+
<styles.QuickSearchResultsItem
|
|
52
|
+
key={`result-${index}`}
|
|
53
|
+
onClick={() => definition.onClick?.(result)}
|
|
54
|
+
onMouseEnter={() => setHighlightedIndex(index)}
|
|
55
|
+
className={highlightedIndex === index ? 'highlighted' : ''}
|
|
56
|
+
>
|
|
57
|
+
{definition.quickRenderer(result, term)}
|
|
58
|
+
</styles.QuickSearchResultsItem>
|
|
59
|
+
);
|
|
60
|
+
});
|
|
61
|
+
return elements;
|
|
62
|
+
}, [definitions, highlightedIndex, resultsArray, term]);
|
|
63
|
+
|
|
64
|
+
const highlightedResult = resultsArray[highlightedIndex];
|
|
65
|
+
const highlightedDefinition = highlightedResult
|
|
66
|
+
? definitions[highlightedResult?.type]
|
|
67
|
+
: null;
|
|
68
|
+
|
|
69
|
+
return (
|
|
70
|
+
<styles.QuickSearchResultsContainer>
|
|
71
|
+
<styles.QuickSearchResultsListContainer>
|
|
72
|
+
{elements}
|
|
73
|
+
</styles.QuickSearchResultsListContainer>
|
|
74
|
+
{highlightedDefinition && (
|
|
75
|
+
<styles.QuickSearchResultsDetailsContainer>
|
|
76
|
+
{highlightedDefinition.titleRenderer(highlightedResult.result, term)}
|
|
77
|
+
<styles.QuickSearchResultsDetailsDivider />
|
|
78
|
+
{highlightedDefinition.detailsRenderer(
|
|
79
|
+
highlightedResult.result,
|
|
80
|
+
term
|
|
81
|
+
)}
|
|
82
|
+
</styles.QuickSearchResultsDetailsContainer>
|
|
83
|
+
)}
|
|
84
|
+
</styles.QuickSearchResultsContainer>
|
|
85
|
+
);
|
|
86
|
+
};
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { Input } from '../forms';
|
|
2
|
+
import styled from 'styled-components';
|
|
3
|
+
|
|
4
|
+
export const QuickSearchResultsContainer = styled.div`
|
|
5
|
+
display: flex;
|
|
6
|
+
flex-direction: row;
|
|
7
|
+
height: 100%;
|
|
8
|
+
`;
|
|
9
|
+
|
|
10
|
+
export const QuickSearchResultsListContainer = styled.div`
|
|
11
|
+
display: flex;
|
|
12
|
+
flex-direction: column;
|
|
13
|
+
padding: var(--space-2);
|
|
14
|
+
border-right: 1px solid var(--color-gray-200);
|
|
15
|
+
flex: 3;
|
|
16
|
+
overflow: auto;
|
|
17
|
+
`;
|
|
18
|
+
|
|
19
|
+
export const QuickSearchResultsTitle = styled.div`
|
|
20
|
+
margin: 0;
|
|
21
|
+
margin-bottom: var(--space-1);
|
|
22
|
+
&:not(:first-child) {
|
|
23
|
+
margin-top: var(--space-2);
|
|
24
|
+
}
|
|
25
|
+
font-weight: bold;
|
|
26
|
+
font-size: var(--text-lg);
|
|
27
|
+
`;
|
|
28
|
+
|
|
29
|
+
export const QuickSearchResultsItem = styled.div`
|
|
30
|
+
padding: var(--space-2) var(--space-3);
|
|
31
|
+
cursor: pointer;
|
|
32
|
+
border-radius: 4px;
|
|
33
|
+
&:hover {
|
|
34
|
+
background-color: var(--color-gray-100);
|
|
35
|
+
}
|
|
36
|
+
`;
|
|
37
|
+
|
|
38
|
+
export const QuickSearchResultsDetailsContainer = styled.div`
|
|
39
|
+
display: flex;
|
|
40
|
+
flex-direction: column;
|
|
41
|
+
padding: var(--space-2);
|
|
42
|
+
flex: 1;
|
|
43
|
+
`;
|
|
44
|
+
|
|
45
|
+
export const QuickSearchResultsDetailsDivider = styled.hr`
|
|
46
|
+
margin: var(--space-2) 0;
|
|
47
|
+
height: 1px;
|
|
48
|
+
border: none;
|
|
49
|
+
background-color: var(--color-gray-200);
|
|
50
|
+
`;
|
|
51
|
+
|
|
52
|
+
export const QuickSearchResultDetailsTitle = styled.div`
|
|
53
|
+
margin: 0;
|
|
54
|
+
`;
|
|
55
|
+
|
|
56
|
+
export const QuickSearchBarInput = styled(Input)<{ $opened?: boolean }>`
|
|
57
|
+
position: relative;
|
|
58
|
+
width: 100%;
|
|
59
|
+
z-index: ${({ $opened }) => ($opened ? 10000 : 0)};
|
|
60
|
+
`;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { FC, ReactNode } from 'react';
|
|
2
|
+
|
|
3
|
+
export type SearchTypeDefinition<T> = {
|
|
4
|
+
title: string;
|
|
5
|
+
quickRenderer: (result: T, term: string) => ReactNode;
|
|
6
|
+
titleRenderer: (result: T, term: string) => ReactNode;
|
|
7
|
+
detailsRenderer: (result: T, term: string) => ReactNode;
|
|
8
|
+
onClick?: (result: T) => void;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export type SearchTypeDefinitions<R> = {
|
|
12
|
+
[K in keyof R]: SearchTypeDefinition<R[K]>;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export type SearchResults<R> = {
|
|
16
|
+
[K in keyof R]: Array<R[K]>;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export type SearchDetailsFC<T> = FC<{
|
|
20
|
+
searchResult: T;
|
|
21
|
+
}>;
|
|
22
|
+
|
|
23
|
+
export type ResultArray<R> = {
|
|
24
|
+
result: R[keyof R];
|
|
25
|
+
type: keyof R;
|
|
26
|
+
}[];
|
package/src/index.ts
CHANGED