react-artasys-ui 0.1.6 → 0.1.7
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/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/lib/Select/Select.d.ts +2 -2
- package/package.json +1 -1
- package/src/Select/Select.tsx +43 -8
package/lib/Select/Select.d.ts
CHANGED
|
@@ -8,9 +8,9 @@ export declare const Context: import("react").Context<{
|
|
|
8
8
|
setSelected: (value: string) => void;
|
|
9
9
|
setTitle: (title: string) => void;
|
|
10
10
|
}>;
|
|
11
|
-
export interface ISelect extends Omit<IElement, 'children'
|
|
11
|
+
export interface ISelect extends Omit<IElement, 'children'> {
|
|
12
12
|
children?: React.FunctionComponentElement<IOption>[];
|
|
13
|
-
|
|
13
|
+
onChangeSelect?: (value: string) => void;
|
|
14
14
|
}
|
|
15
15
|
declare const Select: import("react").ForwardRefExoticComponent<ISelect & import("react").RefAttributes<HTMLInputElement>>;
|
|
16
16
|
export default Select;
|
package/package.json
CHANGED
package/src/Select/Select.tsx
CHANGED
|
@@ -3,7 +3,9 @@ import {
|
|
|
3
3
|
createContext,
|
|
4
4
|
useState,
|
|
5
5
|
useEffect,
|
|
6
|
-
useRef
|
|
6
|
+
useRef,
|
|
7
|
+
ChangeEvent,
|
|
8
|
+
useImperativeHandle
|
|
7
9
|
} from "react";
|
|
8
10
|
import Element,{
|
|
9
11
|
IElement
|
|
@@ -19,13 +21,14 @@ export const Context = createContext({
|
|
|
19
21
|
setTitle: (title: string) => {},
|
|
20
22
|
});
|
|
21
23
|
|
|
22
|
-
export interface ISelect extends Omit<IElement, 'children'
|
|
24
|
+
export interface ISelect extends Omit<IElement, 'children'> {
|
|
23
25
|
children?: React.FunctionComponentElement<IOption>[];
|
|
24
|
-
|
|
26
|
+
onChangeSelect?: (value: string) => void;
|
|
25
27
|
};
|
|
26
28
|
|
|
27
|
-
const Select = forwardRef<HTMLInputElement, ISelect>(({children,
|
|
29
|
+
const Select = forwardRef<HTMLInputElement, ISelect>(({children, onChangeSelect, value, ...props}, ref) => {
|
|
28
30
|
const containerRef = useRef<HTMLDivElement>(null);
|
|
31
|
+
const inputRef = useRef<HTMLInputElement>(null);
|
|
29
32
|
const emptyValue = useRef(false);
|
|
30
33
|
|
|
31
34
|
const [isOpen, setOpen] = useState(false);
|
|
@@ -41,14 +44,44 @@ const Select = forwardRef<HTMLInputElement, ISelect>(({children, onChange, value
|
|
|
41
44
|
setOpen(false);
|
|
42
45
|
};
|
|
43
46
|
|
|
47
|
+
const handleClick = () => {
|
|
48
|
+
isOpen ? close() : open();
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const triggerNativeEvent = (value: string) => {
|
|
52
|
+
const nativeInputValueSetter = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value")?.set;
|
|
53
|
+
nativeInputValueSetter?.call(inputRef.current, value);
|
|
54
|
+
|
|
55
|
+
const event = new Event('input', { bubbles: true});
|
|
56
|
+
inputRef.current!.dispatchEvent(event);
|
|
57
|
+
};
|
|
58
|
+
|
|
44
59
|
const setSelect = (value: string) => {
|
|
45
|
-
if (typeof
|
|
46
|
-
|
|
60
|
+
if (typeof onChangeSelect === 'function') {
|
|
61
|
+
onChangeSelect(value);
|
|
47
62
|
}
|
|
63
|
+
|
|
48
64
|
setSelected(value);
|
|
49
65
|
close();
|
|
50
66
|
};
|
|
51
67
|
|
|
68
|
+
const handleInput = (e: ChangeEvent<HTMLInputElement>) => {
|
|
69
|
+
if (props.disabled) return;
|
|
70
|
+
if (typeof props.onInput === 'function') {
|
|
71
|
+
props.onInput(e);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (typeof props.onChange === 'function') {
|
|
75
|
+
props.onChange(e);
|
|
76
|
+
}
|
|
77
|
+
setSelected(e.target.value);
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
useEffect(() => {
|
|
81
|
+
if (!selected) return;
|
|
82
|
+
triggerNativeEvent(selected);
|
|
83
|
+
}, [selected]);
|
|
84
|
+
|
|
52
85
|
useEffect(() => {
|
|
53
86
|
if (typeof value === 'undefined') return;
|
|
54
87
|
setSelected(String(value));
|
|
@@ -74,6 +107,8 @@ const Select = forwardRef<HTMLInputElement, ISelect>(({children, onChange, value
|
|
|
74
107
|
}
|
|
75
108
|
}, [isOpen]);
|
|
76
109
|
|
|
110
|
+
useImperativeHandle(ref, () => inputRef.current as HTMLInputElement, []);
|
|
111
|
+
|
|
77
112
|
const classes = [''];
|
|
78
113
|
classes.push(styles['container'], styles['hidden']);
|
|
79
114
|
|
|
@@ -85,9 +120,9 @@ const Select = forwardRef<HTMLInputElement, ISelect>(({children, onChange, value
|
|
|
85
120
|
setSelected,
|
|
86
121
|
setTitle
|
|
87
122
|
}}>
|
|
88
|
-
<input {...props} type="hidden" value={selected} ref={
|
|
123
|
+
<input {...props} type="hidden" value={selected} onInput={handleInput} ref={inputRef}/>
|
|
89
124
|
<div className={classes.join(' ')} ref={containerRef} tabIndex={1} onBlur={close}>
|
|
90
|
-
<div className={styles['select']} onClick={
|
|
125
|
+
<div className={styles['select']} onClick={handleClick}>
|
|
91
126
|
<span className={styles['title']}>{title}</span>
|
|
92
127
|
</div>
|
|
93
128
|
<ul className={styles['select-list']}>
|