-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathExample4Page.js
More file actions
148 lines (139 loc) · 5 KB
/
Copy pathExample4Page.js
File metadata and controls
148 lines (139 loc) · 5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import React, { useState } from 'react';
import styled from 'styled-components';
const Wrapper = styled.div`
background-color: #0E76A8;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
`;
const Box = styled.div`
width: 500px;
box-shadow: 5px 5px 10px #333;
display: flex;
flex-direction: column;
justify-content: center;
background-color: white;
border-radius: 16px;
padding: 16px;
margin: 16px;
`;
const Heading = styled.div`
font-weight: bold;
font-size: 24px;
`;
const ListItem = styled.li`
list-style-type: none;
border-bottom: 1px solid black;
padding: 8px;
${({ selected }) => selected ? 'background-color: #DDD;' : ''}
`;
export const Example4Page = () => {
const [selectedItemIndex, setSelectedItemIndex] = useState(-1);
const [clickHeaderText, setClickHeaderText] = useState('Nothing selected');
const [selectedCheckboxes, setSelectedCheckboxes] = useState({
1: false,
2: false,
3: false,
});
const [dropdownHeaderText, setDropdownHeaderText] = useState('Nothing selected');
const [hoveredItem, setHoveredItem] = useState('Nothing selected');
const items = [
'Option One',
'Option Two',
'Option Three',
];
const selectedCount = Object.values(selectedCheckboxes)
.filter(x => x)
.length;
return (
<Wrapper>
<Box>
<Heading>.click() and .dblClick()</Heading>
<p>Click to highlight, double click to select</p>
<p>Selected Item:
<span data-cy="box-1-selected-name">
{clickHeaderText}
</span>
</p>
<ul
data-cy="box-1-items-list"
style={{ paddingLeft: 0 }}>
{items.map((item, i) => (
<ListItem
key={i}
className="box-1-list-item"
onClick={() => setSelectedItemIndex(i)}
onDoubleClick={() => setClickHeaderText(item)}
selected={i === selectedItemIndex}
>{item}</ListItem>
))}
</ul>
</Box>
<Box>
<Heading>.check() and .uncheck()</Heading>
<form>
<p>
<span data-cy="box-2-selected-count">
{selectedCount}
</span>
checkboxes selected
</p>
<div
className="checkboxes"
data-cy="box-2-checkboxes">
{items.map((item, i) => (
<label key={i} style={{ display: 'block' }}>
<input
type="checkbox"
checked={selectedCheckboxes[i]}
onChange={() => setSelectedCheckboxes({
...selectedCheckboxes,
[i]: !selectedCheckboxes[i],
})} />
{item}
</label>
))}
</div>
</form>
</Box>
<Box>
<Heading>.select()</Heading>
<p>Selected Item:
<span data-cy="box-3-selected-name">
{dropdownHeaderText}
</span>
</p>
<select
data-cy="box-3-dropdown"
onChange={e => setDropdownHeaderText(e.target.value)}>
{items.map((item, i) => (
<option
key={i}
value={item}
>{item}</option>
))}
</select>
</Box>
<Box>
<Heading>.trigger('mouseover')</Heading>
<p>Selected Item:
<span data-cy="box-4-selected-name">
{hoveredItem}
</span>
</p>
<ul
style={{ paddingLeft: 0 }}
data-cy="box-4-items-list">
{items.map((item, i) => (
<ListItem
key={i}
onMouseOver={() => setHoveredItem(item)}
selected={item === hoveredItem}
>{item}</ListItem>
))}
</ul>
</Box>
</Wrapper>
);
}