|
1 | 1 | import clsx from "clsx";
|
2 |
| -import { type SyntheticEvent, type MouseEvent, Fragment } from "react"; |
| 2 | +import { |
| 3 | + type SyntheticEvent, |
| 4 | + type MouseEvent, |
| 5 | + Fragment, |
| 6 | + forwardRef, |
| 7 | + useRef, |
| 8 | + useLayoutEffect, |
| 9 | +} from "react"; |
3 | 10 | import type { Col, SortBy, SuperCol } from ".";
|
4 | 11 | import { range } from "../../../common/utils";
|
| 12 | +import { Dropdown } from "react-bootstrap"; |
5 | 13 |
|
6 | 14 | const FilterHeader = ({
|
7 | 15 | colOrder,
|
@@ -157,26 +165,78 @@ export const getSortClassName = (sortBys: SortBy[], i: number) => {
|
157 | 165 | return className;
|
158 | 166 | };
|
159 | 167 |
|
160 |
| -const BulkSelectHeaderCheckbox = ({ checked }: { checked: boolean }) => { |
161 |
| - const onChange = () => { |
162 |
| - console.log("CHANGE"); |
163 |
| - }; |
| 168 | +type CheckboxState = "checked" | "unchecked" | "indeterminate"; |
| 169 | + |
| 170 | +// forwardRef needed for react-bootstrap types |
| 171 | +const CustomToggle = forwardRef( |
| 172 | + ( |
| 173 | + { |
| 174 | + children, |
| 175 | + onClick, |
| 176 | + }: { |
| 177 | + children: CheckboxState; |
| 178 | + onClick: (event: MouseEvent) => void; |
| 179 | + }, |
| 180 | + ref, |
| 181 | + ) => { |
| 182 | + const inputRef = useRef<HTMLInputElement>(null); |
| 183 | + |
| 184 | + useLayoutEffect(() => { |
| 185 | + if (inputRef.current) { |
| 186 | + if (children === "indeterminate") { |
| 187 | + inputRef.current.indeterminate = true; |
| 188 | + } else if (children === "checked") { |
| 189 | + inputRef.current.checked = true; |
| 190 | + inputRef.current.indeterminate = false; |
| 191 | + } else { |
| 192 | + inputRef.current.checked = false; |
| 193 | + inputRef.current.indeterminate = false; |
| 194 | + } |
| 195 | + } |
| 196 | + }, [children]); |
| 197 | + |
| 198 | + return ( |
| 199 | + <input |
| 200 | + className="form-check-input" |
| 201 | + type="checkbox" |
| 202 | + onClick={event => { |
| 203 | + event.preventDefault(); |
| 204 | + onClick(event); |
| 205 | + }} |
| 206 | + ref={element => { |
| 207 | + inputRef.current = element; |
| 208 | + |
| 209 | + if (typeof ref === "function") { |
| 210 | + ref(element); |
| 211 | + } else if (ref) { |
| 212 | + ref.current = element; |
| 213 | + } |
| 214 | + }} |
| 215 | + /> |
| 216 | + ); |
| 217 | + }, |
| 218 | +); |
| 219 | + |
| 220 | +const BulkSelectHeaderCheckbox = () => { |
| 221 | + const state: CheckboxState = "indeterminate"; |
164 | 222 |
|
165 | 223 | // Similar to singleCheckbox stuff below
|
166 | 224 | const onClickCell = (event: MouseEvent) => {
|
167 | 225 | if (event.target && (event.target as any).tagName === "TH") {
|
168 |
| - onChange(); |
| 226 | + console.log("TODO, SHOULD OPEN MENU"); |
169 | 227 | }
|
170 | 228 | };
|
171 | 229 |
|
172 | 230 | return (
|
173 | 231 | <th data-no-row-highlight onClick={onClickCell}>
|
174 |
| - <input |
175 |
| - className="form-check-input" |
176 |
| - type="checkbox" |
177 |
| - checked={checked} |
178 |
| - onChange={onChange} |
179 |
| - /> |
| 232 | + <Dropdown> |
| 233 | + <Dropdown.Toggle as={CustomToggle}>{state}</Dropdown.Toggle> |
| 234 | + <Dropdown.Menu> |
| 235 | + <Dropdown.Item>Select all</Dropdown.Item> |
| 236 | + <Dropdown.Item>Select visible</Dropdown.Item> |
| 237 | + <Dropdown.Item>Clear</Dropdown.Item> |
| 238 | + </Dropdown.Menu> |
| 239 | + </Dropdown> |
180 | 240 | </th>
|
181 | 241 | );
|
182 | 242 | };
|
@@ -214,9 +274,7 @@ const Header = ({
|
214 | 274 | />
|
215 | 275 | ) : null}
|
216 | 276 | <tr>
|
217 |
| - {showBulkSelectCheckboxes ? ( |
218 |
| - <BulkSelectHeaderCheckbox checked={false} /> |
219 |
| - ) : null} |
| 277 | + {showBulkSelectCheckboxes ? <BulkSelectHeaderCheckbox /> : null} |
220 | 278 | {colOrder.map(({ colIndex }) => {
|
221 | 279 | const {
|
222 | 280 | classNames: colClassNames,
|
|
0 commit comments