/* eslint-disable react/jsx-props-no-spreading */
/* eslint-disable react/jsx-boolean-value */
/* eslint-disable no-return-assign */
/* eslint-disable no-nested-ternary */
/* eslint-disable react/prop-types */
/* eslint-disable react/destructuring-assignment */
import React from 'react';
import { GridComponent, ColumnsDirective, ColumnDirective, Inject, Sort, Resize, Filter, Group, Toolbar, ToolbarItems, ExcelExport, ColumnChooser, } from '@syncfusion/ej2-react-grids';
import styled from 'styled-components';
import { MYSQL_TYPE, DATASET_TYPE } from '../sql/data_type_constants';
const Styles = styled.div`
.e-grid * {
font-size: 12px !important;
}
.e-grid .e-content {
overflow-y: auto !important;
}
.e-checkbox-wrapper .e-frame,
.e-css.e-checkbox-wrapper .e-frame {
border: 1px solid white;
height: 16px;
width: 16px;
}
.e-checkbox-wrapper .e-frame.e-check,
.e-css.e-checkbox-wrapper .e-frame.e-check {
padding-top: 2px;
background-color: #00b0ff;
border-color: white;
color: white !important;
}
.e-checkbox-wrapper:hover .e-frame.e-check,
.e-css.e-checkbox-wrapper:hover .e-frame.e-check {
padding-top: 2px;
background-color: #0090aa;
border-color: white;
color: white !important;
}
.e-grid .e-headercell {
padding-right: 8px;
}
`;
const createColumnDefinitionJson = (field, key) => {
const format = { width: '80' };
return <ColumnDirective key={key} {...format} field={field} />;
// not shown for brevity
};
const createColumnDefinitionSQL = (field, key) => {
// code omitted for brevity.........
const format = { width: '80' };
return <ColumnDirective key={key} {...format} field={field.name} />;
};
const createTableColumns = (dataFields, datasetType) => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
if (dataFields.length === 0) {
console.log('NO FIELDS TO CREATE');
return '';
}
console.log('CREATING FIELDS');
const results = (
<ColumnsDirective key="any1">
<ColumnDirective type="checkbox" width="50" />
{dataFields.map((field, i) =>
datasetType === DATASET_TYPE.JSON
? createColumnDefinitionJson(field, i)
: createColumnDefinitionSQL(field, i)
)}
</ColumnsDirective>
);
return results;
};
/**
* Main DataGrid functional component.
* Receives props:
* if JSON fields are extracted from the first row of the datasource
* if SQL fields are provided to props.dataFields
*/
const DataGrid = (props) => {
// extract the data from props. Handle differently based on "datasetType"
let grid;
const toolbarOptions = ['ExcelExport', 'ColumnChooser', 'Update'];
const dataBound = () => {
if (grid) {
console.log('DATABOUND');
}
};
const handleRefresh = () => {
console.log('about to refresh');
if (grid) {
console.log('refreshing');
grid.refresh();
}
};
if (grid) {
console.log('REFRESHING');
}
return (
<Styles style={{ width: '100%', height: '100%' }}>
<GridComponent
dataSource={props.dataSource}
dataBound={dataBound}
dataSourceChanged={handleRefresh}
ref={(g) => (grid = g)}
height="100%"
width="100%"
showColumnChooser={true}
rowHeight="28"
resizeSettings={{ mode: 'Normal' }}
toolbar={toolbarOptions}
allowExcelExport={true}
allowMultiSorting={true}
allowResizing={true}
allowSorting={true}
filterSettings={{ type: 'Menu' }}
allowFiltering={true}
allowSelection={true}
allowGrouping={true}
selectionSettings={{ type: 'Multiple', mode: 'Both' }}
>
<Inject
services={[
Resize,
Filter,
Sort,
Group,
Toolbar,
ExcelExport,
ColumnChooser,
]}
/>
{createTableColumns(props.dataFields, props.datasetType)}
</GridComponent>
</Styles>
);
};
export default DataGrid;