|
| 1 | +// @flow |
| 2 | + |
| 3 | +import React from 'react'; |
| 4 | +import { injectIntl, FormattedMessage } from 'react-intl'; |
| 5 | +import classNames from 'classnames'; |
| 6 | + |
| 7 | +import Button from '../../../components/button'; |
| 8 | +import IconGridViewInverted from '../../../icons/general/IconGridViewInverted'; |
| 9 | +import IconListView from '../../../icons/general/IconListView'; |
| 10 | +import messages from '../messages'; |
| 11 | +import Tooltip from '../Tooltip'; |
| 12 | +import type { ViewMode } from '../flowTypes'; |
| 13 | +import { VIEW_MODE_GRID, VIEW_MODE_LIST } from '../../../constants'; |
| 14 | +import './ViewModeChangeButton.scss'; |
| 15 | + |
| 16 | +type Props = { |
| 17 | + className?: string, |
| 18 | + onViewModeChange: (viewMode: ViewMode) => void, |
| 19 | + viewMode: ViewMode, |
| 20 | +} & InjectIntlProvidedProps; |
| 21 | + |
| 22 | +const ViewModeChangeButton = ({ className = '', onViewModeChange, intl, viewMode, ...rest }: Props) => { |
| 23 | + const isGridView = viewMode === VIEW_MODE_GRID; |
| 24 | + |
| 25 | + const onClick = () => { |
| 26 | + onViewModeChange(isGridView ? VIEW_MODE_LIST : VIEW_MODE_GRID); |
| 27 | + }; |
| 28 | + |
| 29 | + return ( |
| 30 | + <Tooltip |
| 31 | + text={ |
| 32 | + isGridView ? <FormattedMessage {...messages.listView} /> : <FormattedMessage {...messages.gridView} /> |
| 33 | + } |
| 34 | + > |
| 35 | + <Button |
| 36 | + data-testid="view-mode-change-button" |
| 37 | + className={classNames('bdl-ViewModeChangeButton', className)} |
| 38 | + type="button" |
| 39 | + onClick={onClick} |
| 40 | + {...rest} |
| 41 | + > |
| 42 | + {isGridView ? <IconListView width={17} height={17} /> : <IconGridViewInverted width={17} height={17} />} |
| 43 | + </Button> |
| 44 | + </Tooltip> |
| 45 | + ); |
| 46 | +}; |
| 47 | + |
| 48 | +export default injectIntl(ViewModeChangeButton); |
0 commit comments