-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathLayoutSwitcher.js
More file actions
92 lines (84 loc) · 2.35 KB
/
Copy pathLayoutSwitcher.js
File metadata and controls
92 lines (84 loc) · 2.35 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
/*
* This file is part of React-SearchKit.
* Copyright (C) 2018-2022 CERN.
*
* React-SearchKit is free software; you can redistribute it and/or modify it
* under the terms of the MIT License; see LICENSE file for more details.
*/
import PropTypes from "prop-types";
import React, { Component, useContext } from "react";
import Overridable from "react-overridable";
import { Icon, Menu } from "semantic-ui-react";
import { AppContext } from "../ReactSearchKit";
import { ShouldRender } from "../ShouldRender";
class LayoutSwitcher extends Component {
constructor(props) {
super(props);
this.updateLayout = props.updateLayout;
}
onLayoutChange = (layoutName) => {
this.updateLayout(layoutName);
};
render() {
const {
currentLayout = null,
loading,
totalResults,
overridableId = "",
} = this.props;
return (
<ShouldRender condition={currentLayout !== null && !loading && totalResults > 0}>
<Element
currentLayout={currentLayout}
onLayoutChange={this.onLayoutChange}
overridableId={overridableId}
/>
</ShouldRender>
);
}
}
LayoutSwitcher.propTypes = {
overridableId: PropTypes.string,
/* REDUX */
updateLayout: PropTypes.func.isRequired,
loading: PropTypes.bool.isRequired,
currentLayout: PropTypes.string,
totalResults: PropTypes.number.isRequired,
};
const Element = ({
overridableId = "",
currentLayout = null,
onLayoutChange,
}) => {
const { buildUID } = useContext(AppContext);
return (
<Overridable
id={buildUID("LayoutSwitcher.element", overridableId)}
currentLayout={currentLayout}
onLayoutChange={onLayoutChange}
>
<Menu compact icon>
<Menu.Item
name="list"
active={currentLayout === "list"}
onClick={(_, { name }) => onLayoutChange(name)}
>
<Icon name="list layout" />
</Menu.Item>
<Menu.Item
name="grid"
active={currentLayout === "grid"}
onClick={(_, { name }) => onLayoutChange(name)}
>
<Icon name="grid layout" />
</Menu.Item>
</Menu>
</Overridable>
);
};
Element.propTypes = {
currentLayout: PropTypes.string,
onLayoutChange: PropTypes.func.isRequired,
overridableId: PropTypes.string,
};
export default Overridable.component("LayoutSwitcher", LayoutSwitcher);