graphen 1.10.6 → 1.10.8
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
CHANGED
|
@@ -1,33 +1,51 @@
|
|
|
1
|
-
import React, { useState, useCallback } from "react";
|
|
2
|
-
import * as _ from "lodash";
|
|
1
|
+
import React, { useState, useCallback, useEffect } from "react";
|
|
3
2
|
import classNames from "classnames";
|
|
4
3
|
|
|
5
4
|
type Props = {
|
|
6
|
-
initValue: Readonly<{ value: string, label: string }
|
|
7
|
-
label?: string
|
|
8
|
-
items: ReadonlyArray<{ value: string, label: string }
|
|
9
|
-
onChange: (arg0: string) => void
|
|
5
|
+
initValue: Readonly<{ value: string, label: string }>;
|
|
6
|
+
label?: string;
|
|
7
|
+
items: ReadonlyArray<{ value: string, label: string }>;
|
|
8
|
+
onChange: (arg0: string) => void;
|
|
9
|
+
isDisabled?: boolean;
|
|
10
10
|
};
|
|
11
11
|
|
|
12
12
|
function Dropdown(props: Props) {
|
|
13
|
-
const { initValue, label, items, onChange } = props;
|
|
13
|
+
const { initValue, label, items, onChange, isDisabled } = props;
|
|
14
|
+
|
|
14
15
|
const [isExpanded, setIsExpanded] = useState(false);
|
|
15
16
|
const [selectedItem, setSelectedItem] = useState(initValue);
|
|
17
|
+
|
|
18
|
+
useEffect(() => {
|
|
19
|
+
setSelectedItem(initValue);
|
|
20
|
+
}, [initValue, setSelectedItem]);
|
|
21
|
+
|
|
16
22
|
const expandMenu = useCallback(() => {
|
|
17
|
-
|
|
23
|
+
if (!isDisabled) {
|
|
24
|
+
setIsExpanded(true);
|
|
25
|
+
}
|
|
26
|
+
}, [setIsExpanded, isDisabled]);
|
|
27
|
+
const closeMenu = useCallback((event) => {
|
|
28
|
+
if (!event.relatedTarget) {
|
|
29
|
+
setIsExpanded(false);
|
|
30
|
+
}
|
|
18
31
|
}, [setIsExpanded]);
|
|
32
|
+
|
|
19
33
|
const selectItem = useCallback(
|
|
20
34
|
(item) => {
|
|
21
35
|
onChange(item.value);
|
|
22
36
|
setIsExpanded(false);
|
|
23
|
-
setSelectedItem(
|
|
37
|
+
setSelectedItem(items.find((i) => i.value === item.value));
|
|
24
38
|
},
|
|
25
39
|
[setIsExpanded, setSelectedItem, items]
|
|
26
40
|
);
|
|
27
|
-
|
|
41
|
+
|
|
42
|
+
const buttonClasses = classNames('gc-dropdown__btn', {
|
|
43
|
+
'gc-dropdown__btn--with-label': label,
|
|
44
|
+
'gc-dropdown__btn--disabled': isDisabled,
|
|
45
|
+
});
|
|
28
46
|
|
|
29
47
|
return (
|
|
30
|
-
<div className="gc-dropdown">
|
|
48
|
+
<div className="gc-dropdown" onBlur={closeMenu}>
|
|
31
49
|
{label && (<label className="gc-dropdown__label" htmlFor="gc-dropdown__label">
|
|
32
50
|
{label}
|
|
33
51
|
</label>)}
|
|
@@ -41,13 +59,16 @@ function Dropdown(props: Props) {
|
|
|
41
59
|
{selectedItem.label}
|
|
42
60
|
</button>
|
|
43
61
|
{isExpanded && (
|
|
44
|
-
|
|
62
|
+
// eslint-disable-next-line jsx-a11y/no-noninteractive-tabindex
|
|
63
|
+
<div className="gc-dropdown__content" tabIndex={0}>
|
|
45
64
|
<ul className="gc-dropdown__list">
|
|
46
65
|
{/* eslint-disable jsx-a11y/no-static-element-interactions */}
|
|
47
|
-
{
|
|
66
|
+
{items.map((item, index) => {
|
|
48
67
|
const dropdownItemClasses = classNames("gc-dropdown__item", {
|
|
49
|
-
"gc-dropdown__item--first":
|
|
68
|
+
"gc-dropdown__item--first": index === 0,
|
|
69
|
+
"gc-dropdown__item--selected": item.value === selectedItem.value,
|
|
50
70
|
});
|
|
71
|
+
|
|
51
72
|
return (
|
|
52
73
|
/* eslint-disable react/button-has-type */
|
|
53
74
|
<li
|
|
@@ -73,6 +94,7 @@ function Dropdown(props: Props) {
|
|
|
73
94
|
// @ts-ignore
|
|
74
95
|
Dropdown.defaultProps = {
|
|
75
96
|
label: undefined,
|
|
97
|
+
isDisabled: false,
|
|
76
98
|
};
|
|
77
99
|
|
|
78
100
|
export default Dropdown;
|
|
@@ -19,6 +19,10 @@
|
|
|
19
19
|
border: 1px solid $color-silver;
|
|
20
20
|
border-radius: $radius-medium;
|
|
21
21
|
|
|
22
|
+
&--disabled {
|
|
23
|
+
border: 0;
|
|
24
|
+
}
|
|
25
|
+
|
|
22
26
|
&--with-label {
|
|
23
27
|
margin-top: 5px;
|
|
24
28
|
}
|
|
@@ -62,8 +66,13 @@
|
|
|
62
66
|
border-top: none;
|
|
63
67
|
}
|
|
64
68
|
|
|
69
|
+
&--selected {
|
|
70
|
+
background-color: $colour-gallery;
|
|
71
|
+
}
|
|
72
|
+
|
|
65
73
|
&:hover {
|
|
66
74
|
cursor: pointer;
|
|
75
|
+
background-color: $color-michka;
|
|
67
76
|
}
|
|
68
77
|
}
|
|
69
78
|
|
package/src/example.tsx
CHANGED
|
@@ -627,11 +627,13 @@ class ExampleApp extends React.PureComponent<Props, State> {
|
|
|
627
627
|
<p>
|
|
628
628
|
<Dropdown
|
|
629
629
|
initValue={{ label: "Select value", value: "selectValue" }}
|
|
630
|
+
label="Disabled dropdown"
|
|
630
631
|
items={[
|
|
631
632
|
{ label: "Red", value: "red" },
|
|
632
633
|
{ label: "Blue", value: "blue" },
|
|
633
634
|
]}
|
|
634
635
|
onChange={_.noop}
|
|
636
|
+
isDisabled
|
|
635
637
|
/>
|
|
636
638
|
</p>
|
|
637
639
|
</div>
|