playbook_ui 13.24.0 → 13.25.0.pre.alpha.PLAY761globalpaddingpropsbuttons2713
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/pb_kits/playbook/_playbook.scss +2 -0
- data/app/pb_kits/playbook/index.js +1 -0
- data/app/pb_kits/playbook/pb_avatar/avatar.html.erb +1 -6
- data/app/pb_kits/playbook/pb_background/_background.tsx +7 -5
- data/app/pb_kits/playbook/pb_bar_graph/_bar_graph.tsx +41 -6
- data/app/pb_kits/playbook/pb_bar_graph/bar_graph.rb +4 -0
- data/app/pb_kits/playbook/pb_bar_graph/docs/_bar_graph_negative_numbers.html.erb +23 -0
- data/app/pb_kits/playbook/pb_bar_graph/docs/_bar_graph_negative_numbers.jsx +35 -0
- data/app/pb_kits/playbook/pb_bar_graph/docs/_bar_graph_secondary_y_axis.html.erb +26 -0
- data/app/pb_kits/playbook/pb_bar_graph/docs/_bar_graph_secondary_y_axis.jsx +36 -0
- data/app/pb_kits/playbook/pb_bar_graph/docs/_bar_graph_secondary_y_axis.md +3 -0
- data/app/pb_kits/playbook/pb_bar_graph/docs/_bar_graph_stacked.html.erb +22 -0
- data/app/pb_kits/playbook/pb_bar_graph/docs/_bar_graph_stacked.jsx +34 -0
- data/app/pb_kits/playbook/pb_bar_graph/docs/_bar_graph_stacked.md +1 -0
- data/app/pb_kits/playbook/pb_bar_graph/docs/example.yml +6 -0
- data/app/pb_kits/playbook/pb_bar_graph/docs/index.js +3 -0
- data/app/pb_kits/playbook/pb_body/_body.scss +3 -3
- data/app/pb_kits/playbook/pb_button/_button.scss +1 -1
- data/app/pb_kits/playbook/pb_dialog/docs/example.yml +1 -1
- data/app/pb_kits/playbook/pb_dropdown/_dropdown.scss +98 -0
- data/app/pb_kits/playbook/pb_dropdown/_dropdown.tsx +186 -0
- data/app/pb_kits/playbook/pb_dropdown/context/index.tsx +5 -0
- data/app/pb_kits/playbook/pb_dropdown/docs/_dropdown_default.jsx +50 -0
- data/app/pb_kits/playbook/pb_dropdown/docs/_dropdown_with_custom_display.jsx +102 -0
- data/app/pb_kits/playbook/pb_dropdown/docs/_dropdown_with_custom_options.jsx +66 -0
- data/app/pb_kits/playbook/pb_dropdown/docs/_dropdown_with_custom_trigger.jsx +77 -0
- data/app/pb_kits/playbook/pb_dropdown/docs/example.yml +9 -0
- data/app/pb_kits/playbook/pb_dropdown/docs/index.js +4 -0
- data/app/pb_kits/playbook/pb_dropdown/dropdown.test.jsx +17 -0
- data/app/pb_kits/playbook/pb_dropdown/hooks/useDropdown.tsx +17 -0
- data/app/pb_kits/playbook/pb_dropdown/hooks/useHandleOnKeydown.tsx +56 -0
- data/app/pb_kits/playbook/pb_dropdown/subcomponents/DropdownContainer.tsx +95 -0
- data/app/pb_kits/playbook/pb_dropdown/subcomponents/DropdownOption.tsx +91 -0
- data/app/pb_kits/playbook/pb_dropdown/subcomponents/DropdownTrigger.tsx +128 -0
- data/app/pb_kits/playbook/pb_dropdown/utilities/subComponentHelper.tsx +59 -0
- data/app/pb_kits/playbook/pb_icon_circle/_icon_circle.scss +1 -1
- data/app/pb_kits/playbook/pb_icon_circle/_icon_circle.tsx +1 -1
- data/app/pb_kits/playbook/pb_icon_circle/icon_circle.rb +1 -1
- data/app/pb_kits/playbook/pb_icon_circle/icon_circle.test.js +3 -3
- data/app/pb_kits/playbook/pb_list/_list_item.tsx +2 -2
- data/app/pb_kits/playbook/pb_source/source.test.js +2 -2
- data/app/pb_kits/playbook/pb_typeahead/_typeahead.tsx +27 -19
- data/app/pb_kits/playbook/pb_typeahead/components/MenuList.tsx +4 -2
- data/app/pb_kits/playbook/pb_typeahead/components/index.tsx +19 -0
- data/app/pb_kits/playbook/pb_typeahead/docs/_typeahead_custom_menu_list.jsx +51 -0
- data/app/pb_kits/playbook/pb_typeahead/docs/_typeahead_with_highlight.jsx +1 -1
- data/app/pb_kits/playbook/pb_typeahead/docs/example.yml +1 -0
- data/app/pb_kits/playbook/pb_typeahead/docs/index.js +1 -0
- data/app/pb_kits/playbook/playbook-doc.js +2 -0
- data/app/pb_kits/playbook/tokens/_colors.scss +1 -1
- data/dist/menu.yml +5 -1
- data/dist/playbook-rails.js +6 -6
- data/lib/playbook/kit_base.rb +21 -1
- data/lib/playbook/version.rb +2 -2
- metadata +32 -6
- /data/app/pb_kits/playbook/pb_dialog/docs/{_dialog_props_table.md → _dialog_props_swift.md} +0 -0
@@ -0,0 +1,186 @@
|
|
1
|
+
import React, { useState, useRef, useEffect, ReactElement } from "react";
|
2
|
+
import classnames from "classnames";
|
3
|
+
import { buildAriaProps, buildCss, buildDataProps } from "../utilities/props";
|
4
|
+
import { globalProps } from "../utilities/globalProps";
|
5
|
+
|
6
|
+
import Body from "../pb_body/_body";
|
7
|
+
|
8
|
+
import DropdownContainer from "./subcomponents/DropdownContainer";
|
9
|
+
import DropdownOption from "./subcomponents/DropdownOption";
|
10
|
+
import DropdownTrigger from "./subcomponents/DropdownTrigger";
|
11
|
+
import DropdownContext from "./context";
|
12
|
+
import useDropdown from "./hooks/useDropdown";
|
13
|
+
|
14
|
+
import {
|
15
|
+
separateChildComponents,
|
16
|
+
prepareSubcomponents,
|
17
|
+
} from "./utilities/subComponentHelper";
|
18
|
+
import { GenericObject } from "../types";
|
19
|
+
|
20
|
+
type DropdownProps = {
|
21
|
+
aria?: { [key: string]: string };
|
22
|
+
autocomplete?: boolean;
|
23
|
+
className?: string;
|
24
|
+
data?: { [key: string]: string };
|
25
|
+
id?: string;
|
26
|
+
children?: React.ReactChild[] | React.ReactChild | ReactElement[];
|
27
|
+
options: GenericObject;
|
28
|
+
onSelect?: (arg: GenericObject) => null;
|
29
|
+
};
|
30
|
+
|
31
|
+
const Dropdown = (props: DropdownProps) => {
|
32
|
+
const {
|
33
|
+
aria = {},
|
34
|
+
autocomplete = false,
|
35
|
+
children,
|
36
|
+
className,
|
37
|
+
data = {},
|
38
|
+
id,
|
39
|
+
options,
|
40
|
+
onSelect,
|
41
|
+
} = props;
|
42
|
+
|
43
|
+
const ariaProps = buildAriaProps(aria);
|
44
|
+
const dataProps = buildDataProps(data);
|
45
|
+
const classes = classnames(
|
46
|
+
buildCss("pb_dropdown"),
|
47
|
+
globalProps(props),
|
48
|
+
className
|
49
|
+
);
|
50
|
+
|
51
|
+
const [isDropDownClosed, setIsDropDownClosed, toggleDropdown] = useDropdown();
|
52
|
+
|
53
|
+
const [filterItem, setFilterItem] = useState("");
|
54
|
+
const [selected, setSelected] = useState({});
|
55
|
+
const [isInputFocused, setIsInputFocused] = useState(false);
|
56
|
+
const [hasTriggerSubcomponent, setHasTriggerSubcomponent] = useState(true);
|
57
|
+
const [hasContainerSubcomponent, setHasContainerSubcomponent] =
|
58
|
+
useState(true);
|
59
|
+
|
60
|
+
//state for keyboard events
|
61
|
+
const [focusedOptionIndex, setFocusedOptionIndex] = useState(-1);
|
62
|
+
|
63
|
+
const dropdownRef = useRef(null);
|
64
|
+
const inputRef = useRef(null);
|
65
|
+
|
66
|
+
const { trigger, container, otherChildren } =
|
67
|
+
separateChildComponents(children);
|
68
|
+
|
69
|
+
// useEffect to handle clicks outside the dropdown
|
70
|
+
useEffect(() => {
|
71
|
+
const handleClickOutside = (e: MouseEvent) => {
|
72
|
+
if (dropdownRef.current && !dropdownRef.current.contains(e.target)) {
|
73
|
+
setIsDropDownClosed(true);
|
74
|
+
setIsInputFocused(false);
|
75
|
+
}
|
76
|
+
};
|
77
|
+
window.addEventListener("click", handleClickOutside);
|
78
|
+
return () => {
|
79
|
+
window.removeEventListener("click", handleClickOutside);
|
80
|
+
};
|
81
|
+
}, []);
|
82
|
+
|
83
|
+
useEffect(() => {
|
84
|
+
setHasTriggerSubcomponent(!!trigger);
|
85
|
+
setHasContainerSubcomponent(!!container);
|
86
|
+
}, []);
|
87
|
+
|
88
|
+
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
89
|
+
setFilterItem(e.target.value);
|
90
|
+
setIsDropDownClosed(false);
|
91
|
+
};
|
92
|
+
|
93
|
+
const handleOptionClick = (selectedItem: GenericObject) => {
|
94
|
+
setSelected(selectedItem);
|
95
|
+
setFilterItem("");
|
96
|
+
setIsDropDownClosed(true);
|
97
|
+
onSelect(selectedItem);
|
98
|
+
};
|
99
|
+
|
100
|
+
const handleWrapperClick = () => {
|
101
|
+
autocomplete && inputRef.current.focus();
|
102
|
+
toggleDropdown();
|
103
|
+
};
|
104
|
+
|
105
|
+
const handleBackspace = () => {
|
106
|
+
setSelected({});
|
107
|
+
onSelect(null);
|
108
|
+
setFocusedOptionIndex(-1);
|
109
|
+
};
|
110
|
+
|
111
|
+
const filteredOptions = options?.filter((option: GenericObject) =>
|
112
|
+
option.label.toLowerCase().includes(filterItem.toLowerCase())
|
113
|
+
);
|
114
|
+
|
115
|
+
const componentsToRender = prepareSubcomponents({
|
116
|
+
children,
|
117
|
+
hasTriggerSubcomponent,
|
118
|
+
hasContainerSubcomponent,
|
119
|
+
trigger,
|
120
|
+
container,
|
121
|
+
otherChildren,
|
122
|
+
});
|
123
|
+
|
124
|
+
return (
|
125
|
+
<div {...ariaProps}
|
126
|
+
{...dataProps}
|
127
|
+
className={classes}
|
128
|
+
id={id}
|
129
|
+
>
|
130
|
+
<DropdownContext.Provider
|
131
|
+
value={{
|
132
|
+
autocomplete,
|
133
|
+
filteredOptions,
|
134
|
+
filterItem,
|
135
|
+
focusedOptionIndex,
|
136
|
+
handleBackspace,
|
137
|
+
handleChange,
|
138
|
+
handleOptionClick,
|
139
|
+
handleWrapperClick,
|
140
|
+
inputRef,
|
141
|
+
isDropDownClosed,
|
142
|
+
isInputFocused,
|
143
|
+
options,
|
144
|
+
selected,
|
145
|
+
setFocusedOptionIndex,
|
146
|
+
setIsDropDownClosed,
|
147
|
+
setIsInputFocused,
|
148
|
+
setSelected,
|
149
|
+
toggleDropdown,
|
150
|
+
}}
|
151
|
+
>
|
152
|
+
<div className="dropdown_wrapper"
|
153
|
+
ref={dropdownRef}
|
154
|
+
>
|
155
|
+
{children ? (
|
156
|
+
<>
|
157
|
+
{componentsToRender.map((component, index) => (
|
158
|
+
<React.Fragment key={index}>{component}</React.Fragment>
|
159
|
+
))}
|
160
|
+
</>
|
161
|
+
) : (
|
162
|
+
<>
|
163
|
+
<DropdownTrigger />
|
164
|
+
<DropdownContainer>
|
165
|
+
{options &&
|
166
|
+
options?.map((option: GenericObject) => (
|
167
|
+
<Dropdown.Option key={option.id}
|
168
|
+
option={option}
|
169
|
+
>
|
170
|
+
<Body text={option.label} />
|
171
|
+
</Dropdown.Option>
|
172
|
+
))}
|
173
|
+
</DropdownContainer>
|
174
|
+
</>
|
175
|
+
)}
|
176
|
+
</div>
|
177
|
+
</DropdownContext.Provider>
|
178
|
+
</div>
|
179
|
+
);
|
180
|
+
};
|
181
|
+
|
182
|
+
Dropdown.Option = DropdownOption;
|
183
|
+
Dropdown.Trigger = DropdownTrigger;
|
184
|
+
Dropdown.Container = DropdownContainer;
|
185
|
+
|
186
|
+
export default Dropdown;
|
@@ -0,0 +1,50 @@
|
|
1
|
+
import React, { useState } from 'react'
|
2
|
+
import { Dropdown } from '../../'
|
3
|
+
|
4
|
+
const DropdownDefault = (props) => {
|
5
|
+
// eslint-disable-next-line no-unused-vars
|
6
|
+
const [selectedOption, setSelectedOption] = useState();
|
7
|
+
|
8
|
+
const options = [
|
9
|
+
{
|
10
|
+
label: "United States",
|
11
|
+
value: "United States",
|
12
|
+
areaCode: "+1",
|
13
|
+
icon: "🇺🇸",
|
14
|
+
id: "United-states"
|
15
|
+
},
|
16
|
+
{
|
17
|
+
label: "Ukraine",
|
18
|
+
value: "Ukraine",
|
19
|
+
areaCode: "+380",
|
20
|
+
icon: "🇺🇦",
|
21
|
+
id: "ukraine"
|
22
|
+
},
|
23
|
+
{
|
24
|
+
label: "Pakistan",
|
25
|
+
value: "Pakistan",
|
26
|
+
areaCode: "+92",
|
27
|
+
icon: "🇵🇰",
|
28
|
+
id: "pakistan"
|
29
|
+
}
|
30
|
+
];
|
31
|
+
|
32
|
+
|
33
|
+
return (
|
34
|
+
<div>
|
35
|
+
<Dropdown
|
36
|
+
onSelect={(selectedItem) => setSelectedOption(selectedItem)}
|
37
|
+
options={options}
|
38
|
+
{...props}
|
39
|
+
>
|
40
|
+
{options.map((option) => (
|
41
|
+
<Dropdown.Option key={option.id}
|
42
|
+
option={option}
|
43
|
+
/>
|
44
|
+
))}
|
45
|
+
</Dropdown>
|
46
|
+
</div>
|
47
|
+
)
|
48
|
+
}
|
49
|
+
|
50
|
+
export default DropdownDefault
|
@@ -0,0 +1,102 @@
|
|
1
|
+
import React, { useState } from 'react'
|
2
|
+
import { Dropdown, User, FlexItem, Badge, Avatar } from '../../'
|
3
|
+
|
4
|
+
const DropdownWithCustomDisplay = (props) => {
|
5
|
+
const [selectedOption, setSelectedOption] = useState();
|
6
|
+
|
7
|
+
const options = [
|
8
|
+
{
|
9
|
+
label: "Jasper Furniss",
|
10
|
+
value: "Jasper Furniss",
|
11
|
+
territory: "PHL",
|
12
|
+
title: "Senior UX Engineer",
|
13
|
+
id: "jasper-furniss",
|
14
|
+
status: "Offline"
|
15
|
+
},
|
16
|
+
{
|
17
|
+
label: "Ramon Ruiz",
|
18
|
+
value: "Ramon Ruiz",
|
19
|
+
territory: "PHL",
|
20
|
+
title: "Senior UX Desinger",
|
21
|
+
id: "ramon-ruiz",
|
22
|
+
status: "Away"
|
23
|
+
},
|
24
|
+
{
|
25
|
+
label: "Jason Cypret",
|
26
|
+
value: "Jason Cypret",
|
27
|
+
territory: "PHL",
|
28
|
+
title: "VP of User Experience",
|
29
|
+
id: "jason-cypret",
|
30
|
+
status: "Online"
|
31
|
+
},
|
32
|
+
{
|
33
|
+
label: "Courtney Long",
|
34
|
+
value: "Courtney Long",
|
35
|
+
territory: "PHL",
|
36
|
+
title: "UX Design Mentor",
|
37
|
+
id: "courtney-long",
|
38
|
+
status: "Online"
|
39
|
+
}
|
40
|
+
];
|
41
|
+
|
42
|
+
const customDisplay = () => {
|
43
|
+
return (
|
44
|
+
<>
|
45
|
+
{
|
46
|
+
selectedOption && (
|
47
|
+
<Avatar
|
48
|
+
name={selectedOption.label}
|
49
|
+
size="xs"
|
50
|
+
/>
|
51
|
+
)
|
52
|
+
}
|
53
|
+
</>
|
54
|
+
)
|
55
|
+
};
|
56
|
+
|
57
|
+
|
58
|
+
return (
|
59
|
+
<div>
|
60
|
+
<Dropdown
|
61
|
+
onSelect={(selectedItem) => setSelectedOption(selectedItem)}
|
62
|
+
options={options}
|
63
|
+
{...props}
|
64
|
+
>
|
65
|
+
<Dropdown.Trigger customDisplay={customDisplay()}/>
|
66
|
+
{options.map((option) => (
|
67
|
+
<Dropdown.Option key={option.id}
|
68
|
+
option={option}
|
69
|
+
>
|
70
|
+
<>
|
71
|
+
<FlexItem>
|
72
|
+
<User
|
73
|
+
align="left"
|
74
|
+
avatar
|
75
|
+
name={option.label}
|
76
|
+
orientation="horizontal"
|
77
|
+
territory={option.territory}
|
78
|
+
title={option.title}
|
79
|
+
/>
|
80
|
+
</FlexItem>
|
81
|
+
<FlexItem>
|
82
|
+
<Badge
|
83
|
+
rounded
|
84
|
+
text={option.status}
|
85
|
+
variant={`${
|
86
|
+
option.status === "Offline"
|
87
|
+
? "neutral"
|
88
|
+
: option.status === "Online"
|
89
|
+
? "success"
|
90
|
+
: "warning"
|
91
|
+
}`}
|
92
|
+
/>
|
93
|
+
</FlexItem>
|
94
|
+
</>
|
95
|
+
</Dropdown.Option>
|
96
|
+
))}
|
97
|
+
</Dropdown>
|
98
|
+
</div>
|
99
|
+
)
|
100
|
+
}
|
101
|
+
|
102
|
+
export default DropdownWithCustomDisplay
|
@@ -0,0 +1,66 @@
|
|
1
|
+
import React, { useState } from 'react'
|
2
|
+
import { Dropdown, Icon, Body, FlexItem, Flex } from '../..'
|
3
|
+
|
4
|
+
const DropdownWithCustomOptions = (props) => {
|
5
|
+
// eslint-disable-next-line no-unused-vars
|
6
|
+
const [selectedOption, setSelectedOption] = useState();
|
7
|
+
|
8
|
+
const options = [
|
9
|
+
{
|
10
|
+
label: "United States",
|
11
|
+
value: "United States",
|
12
|
+
areaCode: "+1",
|
13
|
+
icon: "🇺🇸",
|
14
|
+
id: "United-states"
|
15
|
+
},
|
16
|
+
{
|
17
|
+
label: "Ukraine",
|
18
|
+
value: "Ukraine",
|
19
|
+
areaCode: "+380",
|
20
|
+
icon: "🇺🇦",
|
21
|
+
id: "ukraine"
|
22
|
+
},
|
23
|
+
{
|
24
|
+
label: "Pakistan",
|
25
|
+
value: "Pakistan",
|
26
|
+
areaCode: "+92",
|
27
|
+
icon: "🇵🇰",
|
28
|
+
id: "pakistan"
|
29
|
+
}
|
30
|
+
];
|
31
|
+
|
32
|
+
|
33
|
+
return (
|
34
|
+
<div>
|
35
|
+
<Dropdown
|
36
|
+
onSelect={(selectedItem) => setSelectedOption(selectedItem)}
|
37
|
+
options={options}
|
38
|
+
{...props}
|
39
|
+
>
|
40
|
+
{options.map((option) => (
|
41
|
+
<Dropdown.Option key={option.id}
|
42
|
+
option={option}
|
43
|
+
>
|
44
|
+
<>
|
45
|
+
<FlexItem>
|
46
|
+
<Flex>
|
47
|
+
<Icon icon={option.icon}
|
48
|
+
paddingRight="xs"
|
49
|
+
/>
|
50
|
+
<Body text={option.label} />
|
51
|
+
</Flex>
|
52
|
+
</FlexItem>
|
53
|
+
<FlexItem>
|
54
|
+
<Body color="light"
|
55
|
+
text={option.areaCode}
|
56
|
+
/>
|
57
|
+
</FlexItem>
|
58
|
+
</>
|
59
|
+
</Dropdown.Option>
|
60
|
+
))}
|
61
|
+
</Dropdown>
|
62
|
+
</div>
|
63
|
+
)
|
64
|
+
}
|
65
|
+
|
66
|
+
export default DropdownWithCustomOptions
|
@@ -0,0 +1,77 @@
|
|
1
|
+
import React, { useState } from 'react'
|
2
|
+
import { Dropdown, Icon, Body, FlexItem, Flex, IconCircle } from '../..'
|
3
|
+
|
4
|
+
const DropdownWithCustomTrigger = (props) => {
|
5
|
+
|
6
|
+
const [selectedOption, setSelectedOption] = useState();
|
7
|
+
|
8
|
+
const options = [
|
9
|
+
{
|
10
|
+
label: "United States",
|
11
|
+
value: "United States",
|
12
|
+
areaCode: "+1",
|
13
|
+
icon: "🇺🇸",
|
14
|
+
id: "United-states"
|
15
|
+
},
|
16
|
+
{
|
17
|
+
label: "Ukraine",
|
18
|
+
value: "Ukraine",
|
19
|
+
areaCode: "+380",
|
20
|
+
icon: "🇺🇦",
|
21
|
+
id: "ukraine"
|
22
|
+
},
|
23
|
+
{
|
24
|
+
label: "Pakistan",
|
25
|
+
value: "Pakistan",
|
26
|
+
areaCode: "+92",
|
27
|
+
icon: "🇵🇰",
|
28
|
+
id: "pakistan"
|
29
|
+
}
|
30
|
+
];
|
31
|
+
|
32
|
+
|
33
|
+
return (
|
34
|
+
<div>
|
35
|
+
<Dropdown
|
36
|
+
onSelect={(selectedItem) => setSelectedOption(selectedItem)}
|
37
|
+
options={options}
|
38
|
+
{...props}
|
39
|
+
>
|
40
|
+
<Dropdown.Trigger>
|
41
|
+
<div key={selectedOption ? selectedOption.icon : "flag"}>
|
42
|
+
<IconCircle
|
43
|
+
cursor="pointer"
|
44
|
+
icon={selectedOption ? selectedOption.icon : "flag"}
|
45
|
+
variant="royal"
|
46
|
+
/>
|
47
|
+
</div>
|
48
|
+
</Dropdown.Trigger>
|
49
|
+
<Dropdown.Container maxWidth="xs">
|
50
|
+
{options.map((option) => (
|
51
|
+
<Dropdown.Option key={option.id}
|
52
|
+
option={option}
|
53
|
+
>
|
54
|
+
<>
|
55
|
+
<FlexItem>
|
56
|
+
<Flex>
|
57
|
+
<Icon icon={option.icon}
|
58
|
+
paddingRight="xs"
|
59
|
+
/>
|
60
|
+
<Body text={option.label} />
|
61
|
+
</Flex>
|
62
|
+
</FlexItem>
|
63
|
+
<FlexItem>
|
64
|
+
<Body color="light"
|
65
|
+
text={option.areaCode}
|
66
|
+
/>
|
67
|
+
</FlexItem>
|
68
|
+
</>
|
69
|
+
</Dropdown.Option>
|
70
|
+
))}
|
71
|
+
</Dropdown.Container>
|
72
|
+
</Dropdown>
|
73
|
+
</div>
|
74
|
+
)
|
75
|
+
}
|
76
|
+
|
77
|
+
export default DropdownWithCustomTrigger
|
@@ -0,0 +1,4 @@
|
|
1
|
+
export { default as DropdownDefault } from './_dropdown_default.jsx'
|
2
|
+
export { default as DropdownWithCustomDisplay } from './_dropdown_with_custom_display.jsx'
|
3
|
+
export { default as DropdownWithCustomOptions } from './_dropdown_with_custom_options.jsx'
|
4
|
+
export { default as DropdownWithCustomTrigger } from './_dropdown_with_custom_trigger.jsx'
|
@@ -0,0 +1,17 @@
|
|
1
|
+
import { renderKit } from '../utilities/test-utils'
|
2
|
+
|
3
|
+
import { Dropdown } from '../'
|
4
|
+
|
5
|
+
/* See these resources for more testing info:
|
6
|
+
- https://github.com/testing-library/jest-dom#usage for useage and examples
|
7
|
+
- https://jestjs.io/docs/en/using-matchers
|
8
|
+
*/
|
9
|
+
|
10
|
+
test('generated scaffold test - update me', () => {
|
11
|
+
const props = {
|
12
|
+
data: { testid: 'default' }
|
13
|
+
}
|
14
|
+
|
15
|
+
const kit = renderKit(Dropdown , props)
|
16
|
+
expect(kit).toBeInTheDocument()
|
17
|
+
})
|
@@ -0,0 +1,17 @@
|
|
1
|
+
import {useState} from 'react';
|
2
|
+
|
3
|
+
|
4
|
+
const useDropdown = (initial=true) => {
|
5
|
+
|
6
|
+
const [isDropDownClosed, setIsDropDownClosed] = useState(initial);
|
7
|
+
|
8
|
+
const toggleDropdown = () => setIsDropDownClosed(!isDropDownClosed);
|
9
|
+
|
10
|
+
return [
|
11
|
+
isDropDownClosed,
|
12
|
+
setIsDropDownClosed as any,
|
13
|
+
toggleDropdown
|
14
|
+
]
|
15
|
+
}
|
16
|
+
|
17
|
+
export default useDropdown
|
@@ -0,0 +1,56 @@
|
|
1
|
+
import React, { useContext } from "react";
|
2
|
+
import DropdownContext from "../context";
|
3
|
+
|
4
|
+
|
5
|
+
export const useHandleOnKeyDown = () => {
|
6
|
+
|
7
|
+
const {
|
8
|
+
autocomplete,
|
9
|
+
focusedOptionIndex,
|
10
|
+
filteredOptions,
|
11
|
+
setFocusedOptionIndex,
|
12
|
+
handleOptionClick,
|
13
|
+
setIsDropDownClosed,
|
14
|
+
handleBackspace,
|
15
|
+
selected
|
16
|
+
}= useContext(DropdownContext)
|
17
|
+
|
18
|
+
return (e: React.KeyboardEvent) => {
|
19
|
+
switch (e.key) {
|
20
|
+
case "Backspace":
|
21
|
+
case "Delete":
|
22
|
+
if (autocomplete) {
|
23
|
+
handleBackspace();
|
24
|
+
}
|
25
|
+
break;
|
26
|
+
case "ArrowDown": {
|
27
|
+
e.preventDefault();
|
28
|
+
setIsDropDownClosed(false);
|
29
|
+
const nextIndex = (focusedOptionIndex + 1) % filteredOptions.length;
|
30
|
+
setFocusedOptionIndex(nextIndex);
|
31
|
+
break;
|
32
|
+
}
|
33
|
+
case "ArrowUp": {
|
34
|
+
e.preventDefault();
|
35
|
+
const nextIndexUp =
|
36
|
+
(focusedOptionIndex - 1 + filteredOptions.length) %
|
37
|
+
filteredOptions.length;
|
38
|
+
setFocusedOptionIndex(nextIndexUp);
|
39
|
+
break;
|
40
|
+
}
|
41
|
+
case "Enter":
|
42
|
+
if (focusedOptionIndex !== -1) {
|
43
|
+
e.preventDefault();
|
44
|
+
handleOptionClick(filteredOptions[focusedOptionIndex]);
|
45
|
+
setFocusedOptionIndex(-1)
|
46
|
+
}
|
47
|
+
break;
|
48
|
+
default:
|
49
|
+
if (selected && selected.label) {
|
50
|
+
e.preventDefault();
|
51
|
+
handleBackspace();
|
52
|
+
}
|
53
|
+
break;
|
54
|
+
}
|
55
|
+
}
|
56
|
+
};
|
@@ -0,0 +1,95 @@
|
|
1
|
+
import React, { useContext } from "react";
|
2
|
+
import classnames from "classnames";
|
3
|
+
import {
|
4
|
+
buildAriaProps,
|
5
|
+
buildCss,
|
6
|
+
buildDataProps,
|
7
|
+
} from "../../utilities/props";
|
8
|
+
import { globalProps } from "../../utilities/globalProps";
|
9
|
+
|
10
|
+
import DropdownContext from "../context";
|
11
|
+
|
12
|
+
import List from "../../pb_list/_list";
|
13
|
+
import ListItem from "../../pb_list/_list_item";
|
14
|
+
import TextInput from "../../pb_text_input/_text_input";
|
15
|
+
import Body from "../../pb_body/_body";
|
16
|
+
|
17
|
+
type DropdownContainerProps = {
|
18
|
+
aria?: { [key: string]: string };
|
19
|
+
className?: string;
|
20
|
+
children?: React.ReactChild[] | React.ReactChild;
|
21
|
+
data?: { [key: string]: string };
|
22
|
+
id?: string;
|
23
|
+
searchbar?: boolean;
|
24
|
+
};
|
25
|
+
|
26
|
+
const DropdownContainer = (props: DropdownContainerProps) => {
|
27
|
+
const {
|
28
|
+
aria = {},
|
29
|
+
className,
|
30
|
+
children,
|
31
|
+
data = {},
|
32
|
+
id,
|
33
|
+
searchbar = false,
|
34
|
+
} = props;
|
35
|
+
|
36
|
+
const {
|
37
|
+
isDropDownClosed,
|
38
|
+
handleChange,
|
39
|
+
filterItem,
|
40
|
+
filteredOptions,
|
41
|
+
inputRef,
|
42
|
+
setFocusedOptionIndex,
|
43
|
+
} = useContext(DropdownContext);
|
44
|
+
|
45
|
+
const ariaProps = buildAriaProps(aria);
|
46
|
+
const dataProps = buildDataProps(data);
|
47
|
+
const classes = classnames(
|
48
|
+
buildCss("pb_dropdown_container"),
|
49
|
+
`${isDropDownClosed ? "close" : "open"}`,
|
50
|
+
globalProps(props),
|
51
|
+
className
|
52
|
+
);
|
53
|
+
|
54
|
+
return (
|
55
|
+
<div {...ariaProps}
|
56
|
+
{...dataProps}
|
57
|
+
className={classes}
|
58
|
+
id={id}
|
59
|
+
onMouseEnter={() => setFocusedOptionIndex(-1)}
|
60
|
+
>
|
61
|
+
{searchbar && (
|
62
|
+
<TextInput paddingTop="xs"
|
63
|
+
paddingX="xs"
|
64
|
+
>
|
65
|
+
<input
|
66
|
+
onChange={handleChange}
|
67
|
+
placeholder="Select..."
|
68
|
+
ref={inputRef}
|
69
|
+
value={filterItem}
|
70
|
+
/>
|
71
|
+
</TextInput>
|
72
|
+
)}
|
73
|
+
<List>{
|
74
|
+
filteredOptions?.length === 0 ? (
|
75
|
+
<ListItem
|
76
|
+
display="flex"
|
77
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
78
|
+
// @ts-ignore
|
79
|
+
justifyContent="center"
|
80
|
+
padding="xs"
|
81
|
+
>
|
82
|
+
<Body color="light"
|
83
|
+
text="no option"
|
84
|
+
/>
|
85
|
+
</ListItem>
|
86
|
+
): (
|
87
|
+
children
|
88
|
+
)
|
89
|
+
}
|
90
|
+
</List>
|
91
|
+
</div>
|
92
|
+
);
|
93
|
+
};
|
94
|
+
|
95
|
+
export default DropdownContainer;
|