import React, { useState, useEffect, memo, ReactNode } from 'react'; import styled from 'styled-components'; import shortid from 'shortid'; import Grid from '@material-ui/core/Grid'; import FormControlLabel from '@material-ui/core/FormControlLabel'; import Switch from '@material-ui/core/Switch'; import Tooltip from '@material-ui/core/Tooltip'; import IconButton from '@material-ui/core/IconButton'; import ZoomInOutlinedIcon from '@material-ui/icons/ZoomInOutlined'; import ZoomOutOutlinedIcon from '@material-ui/icons/ZoomOutOutlined'; import SettingsBackupRestoreOutlinedIcon from '@material-ui/icons/SettingsBackupRestoreOutlined'; import CloseIcon from '@material-ui/icons/Close'; import FullScreenIcon from '@material-ui/icons/Fullscreen'; import useFullscreen from '../hooks/fullscreen'; import csv from './../assets/csv.png'; import excel from './../assets/excel.jpg'; import './AgGridWrapper.css'; const DEFAULT_BASE_TABLE_FONT_SIZE = 14; interface IWrapperProps { height?: string; fontSize?: string | number; } export const Wrapper = styled.div` && { width: 100%; overflow: auto; ${({ height }: IWrapperProps) => height && `height: ${height}`}; .ag-header { background-color: transparent; ${({ fontSize }: IWrapperProps) => fontSize && `font-size: ${fontSize}px;`}; } .ag-cell { ${({ fontSize }: IWrapperProps) => fontSize && `font-size: ${fontSize + 2}px;`}; } } `; interface IProps { id?: string; onDownloadExcel?: () => any; onDownloadCSV?: () => any; onClose?: () => any; onFullVisualizationChange?: () => any; children: ReactNode; actions?: ReactNode; isFullVisualization?: boolean; manageFont?: boolean; allowFullScreen?: boolean; className?: string; } export function AgGridWrapper({ id, allowFullScreen, onDownloadCSV, onDownloadExcel, onClose, isFullVisualization, onFullVisualizationChange, actions, manageFont, children, ...rest }: IProps) { const [baseTableFontSize, changeBaseTableFontSize] = useState( DEFAULT_BASE_TABLE_FONT_SIZE ); const [opId, changeOpId] = useState(id); const { requestFullscreen } = useFullscreen(); useEffect(() => { if (opId) { return; } changeOpId(shortid.generate()); }, []); return ( {(actions || onFullVisualizationChange || onDownloadCSV || onDownloadExcel || manageFont || allowFullScreen || onClose) && ( {actions} {onFullVisualizationChange && ( } label={ isFullVisualization ? 'Visualizzazione compatta' : 'Visualizzazione estesa' } /> )} {onDownloadCSV && ( CSV icon )} {onDownloadExcel && ( Excel icon )} {manageFont && ( <> changeBaseTableFontSize(baseTableFontSize + 2) } > changeBaseTableFontSize(baseTableFontSize - 2) } > changeBaseTableFontSize(DEFAULT_BASE_TABLE_FONT_SIZE) } > )} {allowFullScreen && ( requestFullscreen(opId)}> )} {onClose && ( )} )} {children} ); } export default memo(AgGridWrapper);