Skip to content

Commit

Permalink
Merge pull request #98 from komarovalexander/dev
Browse files Browse the repository at this point in the history
Add column.visible property
  • Loading branch information
komarovalexander authored Oct 28, 2020
2 parents c4185ed + daa412b commit 294e7de
Show file tree
Hide file tree
Showing 18 changed files with 270 additions and 74 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ka-table",
"version": "5.4.0",
"version": "5.5.0",
"license": "MIT",
"repository": "github:komarovalexander/ka-table",
"homepage": "https://komarovalexander.github.io/ka-table/#/overview",
Expand Down
36 changes: 36 additions & 0 deletions public/static/icons/ColumnSettingsDemo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
10 changes: 10 additions & 0 deletions src/Demos/ColumnSettingsDemo/ColumnSettingsDemo.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from 'react';
import ReactDOM from 'react-dom';

import ColumnSettingsDemo from './ColumnSettingsDemo';

it('renders without crashing', () => {
const div = document.createElement('div');
ReactDOM.render(<ColumnSettingsDemo />, div);
ReactDOM.unmountComponentAtNode(div);
});
96 changes: 96 additions & 0 deletions src/Demos/ColumnSettingsDemo/ColumnSettingsDemo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import React, { useState } from 'react';

import { ITableAllProps, ITableProps, kaReducer, Table } from '../../lib';
import { hideColumn, showColumn } from '../../lib/actionCreators';
import CellEditorBoolean from '../../lib/Components/CellEditorBoolean/CellEditorBoolean';
import { ActionType, DataType, EditingMode, SortingMode } from '../../lib/enums';
import { DispatchFunc } from '../../lib/types';

const dataArray = Array(10).fill(undefined).map(
(_, index) => ({
column1: `column:1 row:${index}`,
column2: `column:2 row:${index}`,
column3: `column:3 row:${index}`,
column4: `column:4 row:${index}`,
column5: `column:5 row:${index}`,
column6: `column:6 row:${index}`,
id: index,
}),
);

const tablePropsInit: ITableProps = {
columns: [
{ key: 'column1', title: 'Column 1', dataType: DataType.String },
{ key: 'column2', title: 'Column 2', dataType: DataType.String },
{ key: 'column3', title: 'Column 3', dataType: DataType.String, visible: false },
{ key: 'column4', title: 'Column 4', dataType: DataType.String },
{ key: 'column5', title: 'Column 5', dataType: DataType.String },
{ key: 'column6', title: 'Column 6', dataType: DataType.String },
],
data: dataArray,
editingMode: EditingMode.Cell,
rowKeyField: 'id',
sortingMode: SortingMode.Single,
};

const ColumnSettings: React.FC<ITableAllProps> = (tableProps: ITableAllProps) => {
const columnsSettingsProps: ITableProps = {
data: tableProps.columns.map(c => ({...c, visible: c.visible !== false })),
rowKeyField: 'key',
columns: [{
key: 'title',
isEditable: false,
title: 'Field',
dataType: DataType.String
}, {
key: 'visible',
title: 'Visible',
isEditable: false,
style: { width: 45 },
dataType: DataType.Boolean
}],
editingMode: EditingMode.None,
}
const dispatchSettings: DispatchFunc = (action) => {
if (action.type === ActionType.UpdateCellValue){
tableProps.dispatch(action.value ? showColumn(action.rowKeyValue) : hideColumn(action.rowKeyValue));
}
};
return (
<Table
{...columnsSettingsProps}
childComponents={{
rootDiv: { elementAttributes: () => ({style: {width: 400, marginBottom: 20}})},
cell: {
content: (props) => {
switch (props.column.key){
case 'visible': return <CellEditorBoolean {...props}/>;
}
}
}
}}
dispatch={dispatchSettings}
/>
);
}

const ColumnSettingsDemo: React.FC = () => {
const [tableProps, changeTableProps] = useState<ITableProps>(tablePropsInit);
const dispatch: DispatchFunc = (action) => {
changeTableProps((prevState: ITableProps) => kaReducer(prevState, action));
};
return (
<div className='column-settings-demo'>
<ColumnSettings
{...tableProps}
dispatch={dispatch}
/>
<Table
{...tableProps}
dispatch={dispatch}
/>
</div>
);
};

export default ColumnSettingsDemo;
2 changes: 1 addition & 1 deletion src/Demos/DemoText.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface IDemoTextProps {
const DemoText: React.FunctionComponent<IDemoTextProps> = ({ demoFileName }) => {
const [text, changeText]: [string, any] = useState('');
useEffect(() => {
fetch(`demos/${demoFileName}/${demoFileName}.tsx?c=2`)
fetch(`demos/${demoFileName}/${demoFileName}.tsx?c=3`)
.then((res) => res.text())
.then((fileText) => changeText(fileText));
}, [demoFileName]);
Expand Down
14 changes: 8 additions & 6 deletions src/Demos/Demos.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import BootstrapDemo from './BootstrapDemo/BootstrapDemo';
import ClassComponentDemo from './ClassComponentDemo/ClassComponentDemo';
import ColumnReorderingDemo from './ColumnReorderingDemo/ColumnReorderingDemo';
import ColumnResizingDemo from './ColumnResizingDemo/ColumnResizingDemo';
import ColumnSettingsDemo from './ColumnSettingsDemo/ColumnSettingsDemo';
import CustomAttributesDemo from './CustomAttributesDemo/CustomAttributesDemo';
import CustomCellDemo from './CustomCellDemo/CustomCellDemo';
import CustomDataRowDemo from './CustomDataRowDemo/CustomDataRowDemo';
Expand Down Expand Up @@ -59,22 +60,23 @@ initializeGA();

const demos: Demo[] = [
new Demo(OverviewDemo, '/overview', 'Basics', 'OverviewDemo', 'https://stackblitz.com/edit/table-overview-js', 'https://stackblitz.com/edit/table-overview-ts', ''),
new Demo(AddRowDemo, '/add-row', 'Add New Row', 'AddRowDemo', 'https://stackblitz.com/edit/table-add-row-js', 'https://stackblitz.com/edit/table-add-row-ts', 'Data Editing'),
new Demo(AddRowDemo, '/add-row', 'Add New Row', 'AddRowDemo', 'https://stackblitz.com/edit/table-add-row-js', 'https://stackblitz.com/edit/table-add-row-ts', 'Editing'),
new Demo(AlertCellDemo, '/alert-cell', 'Alert Cell', 'AlertCellDemo', 'https://stackblitz.com/edit/table-alert-cell-js', 'https://stackblitz.com/edit/table-alert-cell-ts', 'Customization'),
new Demo(BootstrapDemo, '/bootstrap', 'Bootstrap', 'BootstrapDemo', 'https://stackblitz.com/edit/table-bootstrap-js', 'https://stackblitz.com/edit/table-bootstrap-ts', ''),
new Demo(MaterialDemo, '/material', 'Material', 'MaterialDemo', 'https://stackblitz.com/edit/table-material-js', 'https://stackblitz.com/edit/table-material-ts', ''),
new Demo(CustomAttributesDemo, '/custom-attributes', 'Custom Attributes', 'CustomAttributesDemo', 'https://stackblitz.com/edit/table-custom-attributes-js', 'https://stackblitz.com/edit/table-custom-attributes-ts', 'Customization'),
new Demo(ColumnSettingsDemo, '/column-settings', 'Column Settings', 'ColumnSettingsDemo', 'https://stackblitz.com/edit/table-column-settings-js', 'https://stackblitz.com/edit/table-column-settings-ts', 'Columns'),
new Demo(ColumnReorderingDemo, '/column-reordering', 'Column Reordering', 'ColumnReorderingDemo', 'https://stackblitz.com/edit/table-column-reordering-js', 'https://stackblitz.com/edit/table-column-reordering-ts', 'Columns'),
new Demo(ColumnResizingDemo, '/column-resizing', 'Column Resizing', 'ColumnResizingDemo', 'https://stackblitz.com/edit/table-column-resizing-js', 'https://stackblitz.com/edit/table-column-resizing-ts', 'Columns'),
new Demo(ClassComponentDemo, '/class-component', 'Class Component', 'ClassComponentDemo', 'https://stackblitz.com/edit/table-class-component-js', 'https://stackblitz.com/edit/table-class-component-ts', ''),
new Demo(CustomCellDemo, '/custom-cell', 'Custom Cell', 'CustomCellDemo', 'https://stackblitz.com/edit/table-custom-cell-js', 'https://stackblitz.com/edit/table-custom-cell-ts', 'Customization'),
new Demo(CustomDataRowDemo, '/custom-data-row', 'Custom Row', 'CustomDataRowDemo', 'https://stackblitz.com/edit/table-custom-data-row-js', 'https://stackblitz.com/edit/table-custom-data-row-ts', 'Customization'),
new Demo(CustomEditorDemo, '/custom-editor', 'Custom Editor', 'CustomEditorDemo', 'https://stackblitz.com/edit/table-custom-editor-js', 'https://stackblitz.com/edit/table-custom-editor-ts', 'Data Editing'),
new Demo(CustomEditorDemo, '/custom-editor', 'Custom Editor', 'CustomEditorDemo', 'https://stackblitz.com/edit/table-custom-editor-js', 'https://stackblitz.com/edit/table-custom-editor-ts', 'Editing'),
new Demo(CustomHeaderCellDemo, '/custom-header-cell', 'Custom Header Cell', 'CustomHeaderCellDemo', 'https://stackblitz.com/edit/table-custom-header-cell-js', 'https://stackblitz.com/edit/table-custom-header-cell-ts', 'Customization'),
new Demo(DeleteRowDemo, '/delete-row', 'Delete Row', 'DeleteRowDemo', 'https://stackblitz.com/edit/table-delete-row-js', 'https://stackblitz.com/edit/table-delete-row-ts', 'Data Editing'),
new Demo(DeleteRowDemo, '/delete-row', 'Delete Row', 'DeleteRowDemo', 'https://stackblitz.com/edit/table-delete-row-js', 'https://stackblitz.com/edit/table-delete-row-ts', 'Editing'),
new Demo(DetailsRowDemo, '/details-row', 'Details Row', 'DetailsRowDemo', 'https://stackblitz.com/edit/table-details-row-js', 'https://stackblitz.com/edit/table-details-row-ts', 'Rows'),
new Demo(EditingDemo, '/editing', 'Editing', 'EditingDemo', 'https://stackblitz.com/edit/table-editing-js', 'https://stackblitz.com/edit/table-editing-ts', 'Data Editing'),
new Demo(EditingRowDemo, '/editing-row', 'Editing - Row', 'EditingRowDemo', 'https://stackblitz.com/edit/table-editing-row-js', 'https://stackblitz.com/edit/table-editing-row-ts', 'Data Editing'),
new Demo(EditingDemo, '/editing', 'Editing - Cell', 'EditingDemo', 'https://stackblitz.com/edit/table-editing-js', 'https://stackblitz.com/edit/table-editing-ts', 'Editing'),
new Demo(EditingRowDemo, '/editing-row', 'Editing - Row', 'EditingRowDemo', 'https://stackblitz.com/edit/table-editing-row-js', 'https://stackblitz.com/edit/table-editing-row-ts', 'Editing'),
new Demo(EventsDemo, '/events', 'Events', 'EventsDemo', 'https://stackblitz.com/edit/table-events-js', 'https://stackblitz.com/edit/table-events-ts', 'Miscellaneous'),
new Demo(ExportDemo, '/export', 'Export to CSV', 'ExportDemo', 'https://stackblitz.com/edit/table-export-js', 'https://stackblitz.com/edit/table-export-ts', 'Export / Print'),
new Demo(ExportToPdfDemo, '/export-pdf', 'Export to PDF', 'ExportToPdfDemo', 'https://stackblitz.com/edit/table-export-pdf-js', 'https://stackblitz.com/edit/table-export-pdf-ts', 'Export / Print'),
Expand Down Expand Up @@ -102,7 +104,7 @@ const demos: Demo[] = [
new Demo(SelectionSingleDemo, '/selection-single', 'Selection - Single', 'SelectionSingleDemo', 'https://stackblitz.com/edit/table-selection-single-js', 'https://stackblitz.com/edit/table-selection-single-ts', 'Selection'),
new Demo(SortingDemo, '/sorting', 'Sorting', 'SortingDemo', 'https://stackblitz.com/edit/table-sorting-js', 'https://stackblitz.com/edit/table-sorting-ts', ''),
new Demo(StateStoringDemo, '/state-storing', 'State Storing', 'StateStoringDemo', 'https://stackblitz.com/edit/table-state-storing-js', '', 'Miscellaneous', true),
new Demo(ValidationDemo, '/validation', 'Validation', 'ValidationDemo', 'https://stackblitz.com/edit/table-validation-js', 'https://stackblitz.com/edit/table-validation-ts', 'Data Editing'),
new Demo(ValidationDemo, '/validation', 'Validation', 'ValidationDemo', 'https://stackblitz.com/edit/table-validation-js', 'https://stackblitz.com/edit/table-validation-ts', 'Editing'),
];

const cases: DemoCase[] = demos.map((d: Demo) => {
Expand Down
4 changes: 2 additions & 2 deletions src/Demos/MenuItems.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ export class MenuItem {
public isActive?: boolean;
}

const newItems: string[] = ['FixedColumnDemo', 'PrintDemo', 'ExportToPdfDemo'];
const updateItems: string[] = ['Columns', 'ExportPrint', 'SortingDemo', 'RemoteDataDemo'];
const newItems: string[] = ['ColumnSettingsDemo'];
const updateItems: string[] = ['Columns'];

const MenuItems: React.FC<{ items: MenuItem[] }> = ({ items }) => {

Expand Down
34 changes: 16 additions & 18 deletions src/Demos/RemoteDataDemo/RemoteDataDemo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,45 +45,43 @@ const tablePropsInit: ITableProps = {
rowKeyField: 'id',
};

const initGetData = (props: ITableProps, dispatch: DispatchFunc) => {
return (action?: any) => {
serverEmulator.get(props.paging, props.columns, action?.pageIndex).then((result) => {
dispatch(updatePagesCount(result.pagesCount));
dispatch(updateData(result.data));
dispatch(hideLoading());
});
}
};

const RemoteDataDemo: React.FC = () => {
const [tableProps, changeTableProps] = useState(tablePropsInit);
const dispatch: DispatchFunc = (action) => {
const getData = (props: ITableProps) => {
serverEmulator.get({ ...props.paging, pageIndex: action.pageIndex }, props.columns).then((result) => {
dispatch(updatePagesCount(result.pagesCount));
dispatch(updateData(result.data));
dispatch(hideLoading());
});
};
if (action.type === ActionType.DeleteRow) {
dispatch(showLoading('Deleting Row..'));
serverEmulator.delete(action.rowKeyValue).then((result) => {
getData(tableProps);
getData(action);
});
} else if (action.type === ActionType.UpdateCellValue) {
dispatch(showLoading('Updating Data..'));
const column = tableProps.columns.find((c) => c.key === action.columnKey)!;
serverEmulator.update(action.rowKeyValue, { [getField(column)]: action.value }).then(() => {
getData(tableProps);
getData(action);
});
} else if (action.type === ActionType.UpdatePageIndex) {
dispatch(showLoading('Loading Data..'));
getData(tableProps);
getData(action);
} else if (action.type === ActionType.UpdateSortDirection) {
dispatch(showLoading('Loading Data..'));
getData(kaReducer(tableProps, action));
initGetData(kaReducer(tableProps, action), dispatch)(action);
}
changeTableProps((prevState: ITableProps) => kaReducer(prevState, action));
};
const getData = initGetData(tableProps, dispatch);

if (!tableProps.data) {
serverEmulator.get(tableProps.paging).then(
(result) => {
dispatch(updatePagesCount(result.pagesCount));
dispatch(updateData(result.data));
dispatch(hideLoading());
},
);
getData();
}

return (
Expand Down
8 changes: 6 additions & 2 deletions src/Demos/RemoteDataDemo/serverEmulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@ let dataArray = Array(100).fill(undefined).map(
}),
);

const get = (paging?: PagingOptions, columns?: Column[]): Promise<any> => {
const get = (paging?: PagingOptions, columns?: Column[], pageIndexNew?: number): Promise<any> => {
return new Promise((resolve) => {
setTimeout(() => {
const sortedData = columns ? sortData(columns, dataArray) : dataArray;
const data = getPageData(sortedData, { ...paging, pagesCount: undefined });
const data = getPageData(sortedData, {
...paging,
pagesCount: undefined,
pageIndex: pageIndexNew != null ? pageIndexNew : paging?.pageIndex
});
resolve({
data,
pagesCount: getPagesCount(dataArray, { ...paging, pagesCount: undefined })
Expand Down
3 changes: 1 addition & 2 deletions src/lib/Components/Table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ export const Table: React.FunctionComponent<ITableAllProps> = (props) => {
const { elementAttributes, content: rootDivContent } = getElementCustomization({
className: kaCss
}, { ...props, dispatch }, childComponents.rootDiv);
elementAttributes.style = {...elementAttributes.style, width, height}

elementAttributes.style = {width, height, ...elementAttributes.style}
return (
<div {...elementAttributes}>
{rootDivContent || <TableWrapper {...props} />}
Expand Down
5 changes: 3 additions & 2 deletions src/lib/Models/Column.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ import { Field } from '../types';

export class Column {
public dataType?: DataType;
public filterRowValue?: any;
public filterRowOperator?: any;
public field?: Field;
public filterRowOperator?: any;
public filterRowValue?: any;
public isEditable?: boolean;
public isResizable?: boolean;
public key!: string;
public sortDirection?: SortDirection;
public sortIndex?: number;
public style?: React.CSSProperties;
public title?: string;
public visible?: boolean;
}
12 changes: 12 additions & 0 deletions src/lib/Reducers/kaReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ const kaReducer: any = (props: ITableProps, action: any) => {
} = props;

switch (action.type) {
case ActionType.ShowColumn: {
const newColumns = [...columns];
const columnIndex = newColumns.findIndex(c => c.key === action.columnKey);
newColumns[columnIndex] = {...newColumns[columnIndex], visible: true};
return {...props, columns: newColumns};
}
case ActionType.HideColumn: {
const newColumns = [...columns];
const columnIndex = newColumns.findIndex(c => c.key === action.columnKey);
newColumns[columnIndex] = {...newColumns[columnIndex], visible: false};
return {...props, columns: newColumns};
}
case ActionType.ReorderRows: {
const newData = reorderData(data, (d) => getValueByField(d, rowKeyField), action.rowKeyValue, action.targetRowKeyValue);
return {...props, data: newData};
Expand Down
Loading

0 comments on commit 294e7de

Please sign in to comment.