-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselectable.toolbar.tsx
More file actions
61 lines (54 loc) · 2.44 KB
/
selectable.toolbar.tsx
File metadata and controls
61 lines (54 loc) · 2.44 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
import React, { useCallback } from 'react';
import { Button, Form, Icon, Label, Menu } from 'semantic-ui-react';
import { entries, filter, head, map } from 'lodash-es';
import { useRelatableStateContext, useRelatableToolbarContext } from '../../states';
import { getToolbarStateClass } from '../../utils/relatable-state-classes';
import { IWithFiltersInstance, IWithSelectionInstance, withSelection } from '../../add-ons';
import { ToolbarPopup } from './toolbar-popup';
import arrayHasItems from '../../utils/array-has-items';
export default function SelectableToolbar() {
const { selectedFlatRows, state: { selectedRowIds } } = useRelatableStateContext<any, IWithSelectionInstance>();
const [selectedToolbarAction, setToolbar, clearToolbar] = useRelatableToolbarContext();
const selectedRows = filter(entries(selectedRowIds), ([, selected]) => selected);
const isSelected = arrayHasItems(selectedRows);
return <ToolbarPopup
name={withSelection.name}
content={<SelectionPopup
rows={selectedFlatRows}
selectedRowIds={map(selectedRows, head)}
selectedToolbarAction={selectedToolbarAction}/>}
selectedToolbarAction={selectedToolbarAction}
onClose={clearToolbar}>
<Menu.Item name="selection" disabled={!isSelected} onClick={() => setToolbar(withSelection.name)}>
<Icon name='list ul' className="relatable__toolbar-icon"/>
Selected
{isSelected &&
<Label className={isSelected ? getToolbarStateClass('isSelected') : ''}>{selectedRows.length}</Label>}
</Menu.Item>
</ToolbarPopup>;
}
function SelectionPopup({ rows, selectedRowIds }: any) {
const { onCustomSelectionChange } = useRelatableStateContext<any, IWithSelectionInstance & IWithFiltersInstance>();
const [, , clearToolbar] = useRelatableToolbarContext();
const onSelectionClear = useCallback(() => {
const selectedRows = filter(rows, 'isSelected');
clearToolbar();
onCustomSelectionChange(selectedRows, false);
}, [onCustomSelectionChange, rows]);
return <div className="relatable__toolbar-popup relatable__toolbar-selection-popup">
<Form className="relatable__toolbar-selection-form">
<h4>You have selected {selectedRowIds.length} rows</h4>
<Form.Group>
<Button
basic
icon
color="black"
title="Clear selection"
type="button"
onClick={onSelectionClear}>
<Icon name="remove"/> Clear
</Button>
</Form.Group>
</Form>
</div>;
}