import React from 'react'; import PropTypes from 'prop-types'; import { Dropdown, DropdownMenu, DropdownToggle, DropdownItem } from 'reactstrap'; import { Utils } from '../utils/utils'; import { gettext } from '../utils/constants'; import { GRID_MODE, LIST_MODE } from './dir-view-mode/constants'; import '../css/view-modes.css'; const propTypes = { currentViewMode: PropTypes.string.isRequired, switchViewMode: PropTypes.func.isRequired }; class ViewModes extends React.Component { constructor(props) { super(props); this.state = { isDropdownMenuOpen: false }; } componentDidMount() { document.addEventListener('keydown', this.onKeyDown); } componentWillUnmount() { document.removeEventListener('keydown', this.onKeyDown); } onKeyDown = (e) => { if ((e.ctrlKey || e.metaKey) && e.shiftKey) { if (e.keyCode === 49) { this.props.switchViewMode(LIST_MODE); } else if (e.keyCode === 50) { this.props.switchViewMode(GRID_MODE); } } }; toggleDropdownMenu = () => { this.setState({ isDropdownMenuOpen: !this.state.isDropdownMenuOpen }); }; render() { const { isDropdownMenuOpen } = this.state; const { currentViewMode } = this.props; const shortcutMain = Utils.isMac() ? '⇧ ⌘' : 'Ctrl + Shift +'; const options = [ { 'icon': 'list-view', 'text': gettext('List view'), 'value': LIST_MODE, 'shortcut': `${shortcutMain} 1` }, { 'icon': 'grid-view', 'text': gettext('Grid view'), 'value': GRID_MODE, 'shortcut': `${shortcutMain} 2` } ]; return ( {options.map((item, index) => { return (
{currentViewMode === item.value && } {item.text} {item.shortcut}
); })}
); } } ViewModes.propTypes = propTypes; export default ViewModes;