react-restyle-components 0.3.18 → 0.3.19
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/README.md +2 -3
- package/lib/src/core-components/src/molecules/single-select/single-select.component.d.ts +9 -0
- package/lib/src/core-components/src/molecules/single-select/single-select.component.d.ts.map +1 -0
- package/lib/src/core-components/src/molecules/single-select/single-select.component.js +66 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -26,7 +26,6 @@ yarn add react-restyle-components
|
|
|
26
26
|
|
|
27
27
|
https://appasaheb4.github.io/react-restyle-components
|
|
28
28
|
|
|
29
|
-
|
|
30
29
|
### Built With
|
|
31
30
|
|
|
32
31
|
- [ReactJS](https://reactjs.org/)
|
|
@@ -47,8 +46,8 @@ Website: www.tech-abl.com
|
|
|
47
46
|
### How To Contribute
|
|
48
47
|
- Fork and clone this repository
|
|
49
48
|
- Make some changes as required
|
|
50
|
-
- Write unit test to showcase its functionality
|
|
51
|
-
- Run the test suites to make sure it's not breaking anything `$ npm test`
|
|
49
|
+
<!-- - Write unit test to showcase its functionality
|
|
50
|
+
- Run the test suites to make sure it's not breaking anything `$ npm run test` -->
|
|
52
51
|
- Submit a pull request under `master` branch
|
|
53
52
|
|
|
54
53
|
😊 ALWAYS WELCOME 😊
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
interface SingleSelectProps {
|
|
2
|
+
options: Array<any>;
|
|
3
|
+
displayKey?: string;
|
|
4
|
+
hasError?: boolean;
|
|
5
|
+
onSelect: (item: any) => any;
|
|
6
|
+
}
|
|
7
|
+
export declare const SingleSelect: ({ options, displayKey, hasError, onSelect, }: SingleSelectProps) => import("react/jsx-runtime").JSX.Element;
|
|
8
|
+
export {};
|
|
9
|
+
//# sourceMappingURL=single-select.component.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"single-select.component.d.ts","sourceRoot":"","sources":["../../../../../../src/core-components/src/molecules/single-select/single-select.component.tsx"],"names":[],"mappings":"AAIA,UAAU,iBAAiB;IACzB,OAAO,EAAE,KAAK,CAAC,GAAG,CAAC,CAAC;IACpB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,QAAQ,EAAE,CAAC,IAAI,EAAE,GAAG,KAAK,GAAG,CAAC;CAC9B;AAED,eAAO,MAAM,YAAY,iDAKtB,iBAAiB,4CA0KnB,CAAC"}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
|
+
import { useState, useEffect, useRef } from 'react';
|
|
3
|
+
import s from '../../tc.module.css';
|
|
4
|
+
import { cn } from '../../utils';
|
|
5
|
+
export const SingleSelect = ({ options = [], displayKey = 'title', hasError = false, onSelect, }) => {
|
|
6
|
+
const [value, setValue] = useState('');
|
|
7
|
+
const [isListOpen, setIsListOpen] = useState(false);
|
|
8
|
+
// Arrow navigation state
|
|
9
|
+
const [highlightedIndex, setHighlightedIndex] = useState(-1);
|
|
10
|
+
const useOutsideAlerter = (ref) => {
|
|
11
|
+
useEffect(() => {
|
|
12
|
+
function handleClickOutside(event) {
|
|
13
|
+
if (ref.current && !ref.current.contains(event.target) && isListOpen) {
|
|
14
|
+
setIsListOpen(false);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
document.addEventListener('mousedown', handleClickOutside);
|
|
18
|
+
return () => {
|
|
19
|
+
document.removeEventListener('mousedown', handleClickOutside);
|
|
20
|
+
};
|
|
21
|
+
}, [ref, isListOpen]);
|
|
22
|
+
};
|
|
23
|
+
const wrapperRef = useRef(null);
|
|
24
|
+
useOutsideAlerter(wrapperRef);
|
|
25
|
+
// Keyboard navigation
|
|
26
|
+
useEffect(() => {
|
|
27
|
+
if (!isListOpen)
|
|
28
|
+
return;
|
|
29
|
+
const handleKeyDown = (e) => {
|
|
30
|
+
if (e.key === 'ArrowDown') {
|
|
31
|
+
setHighlightedIndex((prev) => prev < options.length - 1 ? prev + 1 : 0);
|
|
32
|
+
}
|
|
33
|
+
else if (e.key === 'ArrowUp') {
|
|
34
|
+
setHighlightedIndex((prev) => prev > 0 ? prev - 1 : options.length - 1);
|
|
35
|
+
}
|
|
36
|
+
else if (e.key === 'Enter' && highlightedIndex >= 0) {
|
|
37
|
+
const item = options[highlightedIndex];
|
|
38
|
+
setValue(item[displayKey]);
|
|
39
|
+
onSelect(item);
|
|
40
|
+
setIsListOpen(false);
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
document.addEventListener('keydown', handleKeyDown);
|
|
44
|
+
return () => document.removeEventListener('keydown', handleKeyDown);
|
|
45
|
+
}, [isListOpen, highlightedIndex, options, displayKey, onSelect]);
|
|
46
|
+
// Reset highlight when list opens/closes
|
|
47
|
+
useEffect(() => {
|
|
48
|
+
if (isListOpen)
|
|
49
|
+
setHighlightedIndex(-1);
|
|
50
|
+
}, [isListOpen]);
|
|
51
|
+
return (_jsx(_Fragment, { children: _jsxs("div", { className: cn(s['flex'], s['dark:bg-boxdark'], s['dark:text-white'], s['flex-col'], s['w-full'], s['rounded-md'], s['border'], {
|
|
52
|
+
[s['border-red']]: hasError,
|
|
53
|
+
[s['border-gray-300']]: !hasError,
|
|
54
|
+
}), ref: wrapperRef, children: [_jsxs("span", { className: cn(s['p-2'], s['shadow-sm'], s['flex'], s['justify-between'], s['items-center']), onClick: () => {
|
|
55
|
+
setIsListOpen(!isListOpen);
|
|
56
|
+
}, tabIndex: 0, style: { cursor: 'pointer' }, children: [value ? value : 'Select', _jsx("span", { children: isListOpen ? (_jsx("svg", { width: "16", height: "16", viewBox: "0 0 20 20", fill: "none", children: _jsx("path", { d: "M5 12l5-5 5 5", stroke: "currentColor", strokeWidth: "2", fill: "none" }) })) : (_jsx("svg", { width: "16", height: "16", viewBox: "0 0 20 20", fill: "none", children: _jsx("path", { d: "M5 8l5 5 5-5", stroke: "currentColor", strokeWidth: "2", fill: "none" }) })) })] }), _jsx("div", { className: cn(s['flex'], { [s['show']]: isListOpen, [s['hidden']]: !isListOpen }, s['relative']), children: options
|
|
57
|
+
? options?.length > 0 && (_jsx("div", { className: cn(s['flex'], s['absolute'], s['rounded-sm'], s['w-full']), style: { zIndex: 999 }, children: _jsx("ul", { className: cn(s['flex'], s['flex-col'], s['gap-1'], s['bg-gray-200'], s['w-full']), children: options?.map((item, index) => (_jsx("li", { className: cn(s['flex'], s['gap-2'], s['p-2'], highlightedIndex === index && s['bg-blue-100']), style: {
|
|
58
|
+
cursor: 'pointer',
|
|
59
|
+
background: highlightedIndex === index ? '#e0e7ff' : undefined,
|
|
60
|
+
}, onClick: () => {
|
|
61
|
+
setValue(item[displayKey]);
|
|
62
|
+
onSelect(item);
|
|
63
|
+
setIsListOpen(false);
|
|
64
|
+
}, onMouseEnter: () => setHighlightedIndex(index), children: _jsx("label", { className: cn(s['flex'], s['gap-2']), children: _jsx("span", { className: cn(s['flex'], s['h-4']), children: item[displayKey] }) }) }, index))) }) }))
|
|
65
|
+
: null })] }) }));
|
|
66
|
+
};
|