-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathSortButtons.js
More file actions
72 lines (65 loc) · 1.5 KB
/
SortButtons.js
File metadata and controls
72 lines (65 loc) · 1.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
import React, { useState, useEffect } from 'react';
import { FiArrowUp, FiArrowDown } from 'react-icons/fi';
import { ToolBarButton } from '@/components/Buttons';
export default function SortButtons({ buttons, selected, onChange }) {
const [allButtons, setAllButtons] = useState([]);
useEffect(() => {
if (buttons.length) {
allButtons.length = 0;
buttons.forEach((b) => {
const b_obj = {
...b,
...{
asc: false,
},
};
if (b.field === selected) {
b.selected = false;
}
allButtons.push(b_obj);
});
if (selected === undefined) {
allButtons[0].selected = true;
setAllButtons([...allButtons]);
}
}
}, []);
const onButtonClicked = (button) => {
const new_buttons = [];
const selectedButton = {};
allButtons.forEach((b) => {
if (b.field === button.field) {
if (b.selected) {
b.asc = !b.asc;
} else {
b.selected = true;
b.asc = true;
}
b.selected = true;
selectedButton = { ...b };
} else {
b.selected = false;
}
new_buttons.push({ ...b });
});
setAllButtons(new_buttons);
if (onChange) {
onChange({ ...selectedButton });
}
};
return (
<div className='flex'>
{allButtons.map((b, bi) => {
return (
<ToolBarButton
key={bi}
selected={b.selected ? '1' : '0'}
handleOnClick={() => onButtonClicked(b)}
cclas={b.selected ? `text-brand-hightlight` : ''}>
{b.label} {b.asc ? <FiArrowDown /> : <FiArrowUp />}
</ToolBarButton>
);
})}
</div>
);
}