diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 0dbd838..16bae3b 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -19,14 +19,23 @@ - None - All - Last - - - - - ) + // const testRow: TestRow = { + // id: "1234", + // deliverableId: "string", + // deliverableBody:"string", + // wpId: "string", + // wpBody: "string", + // activityId: "string", + // activityBody: "string", + // order: 1, + // taskId: "string", + // taskBody: "string" + // } + // console.log(buildCellFromHeader(testRow, headers[0])) + return ( + <> + {/**/} + + setOutsideClickDeselect((e?.target as any).checked)} /> + Deselect on click away + setDarkTheme((e?.target as any).checked)} /> + Dark Theme Test + + StretchMode + + + + + ) } -storiesOf("VirtualizedTable (DEMOS)", module) - .add("Large data set", () => ) - .add("Main table", () => ) +storiesOf('VirtualizedTable (DEMOS)', module) + .add('Large data set', () => ) + .add('Main table', () => ) diff --git a/src/stories/Stretch.stories.tsx b/src/stories/Stretch.stories.tsx index 88ea456..891838e 100644 --- a/src/stories/Stretch.stories.tsx +++ b/src/stories/Stretch.stories.tsx @@ -1,63 +1,56 @@ -import {getSimpleData} from "../utils/generateData" -import {ApolloSpreadSheet} from "../index" -import React from "react" -import {storiesOf} from "@storybook/react" -import {StretchMode} from "../types/stretch-mode.enum" -import {Column} from "../column-grid/types/header.type" -import {createColumnMock} from "../column-grid/__mocks__/column-mock" +import { getSimpleData } from './generateData' +import { ApolloSpreadSheet } from '../index' +import React from 'react' +import { storiesOf } from '@storybook/react' +import { StretchMode } from '../types/stretch-mode.enum' +import { Column } from '../column-grid/types/header.type' +import { createColumnMock } from '../column-grid/__mocks__/column-mock' const ExampleTable = ({ headerData, data, stretchMode }) => { return ( - + ) } -storiesOf("Stretch Modes", module) -.add("None (with remaining width left and no scroll)", () => { - const { headerData, data } = getSimpleData(10, 3) - return -}) -.add("None (fixed widths with scroll)", () => { - const headers: Column[] = [ - createColumnMock({ width: 140, title: "First" }), - createColumnMock({ width: 140, title: "Second" }), - ] - const { data } = getSimpleData(5, 2) - return -}) -.add("None (default with scroll)", () => { - const { headerData, data } = getSimpleData(10, 50) - return -}) -.add("All", () => { - const headers: Column[] = [ - createColumnMock({ width: "20%", title: "First" }), - createColumnMock({ width: "30%", title: "Second" }), - createColumnMock({ width: "10%", title: "Remain" }), - ] - const { data } = getSimpleData(4, 3) - return -}) -.add("All (suppress scroll on overflow)", () => { - const { headerData, data } = getSimpleData(10, 50) - return -}) -.add("All (fixed widths all)", () => { - const { headerData, data } = getSimpleData(10, 50) - return -}) -.add("Last", () => { - const { headerData, data } = getSimpleData(10, 50) - return -}) -.add("Last (fixed widths all)", () => { - const { headerData, data } = getSimpleData(10, 50) - return -}) +storiesOf('Stretch Modes', module) + .add('None (with remaining width left and no scroll)', () => { + const { headerData, data } = getSimpleData(10, 3) + return + }) + .add('None (fixed widths with scroll)', () => { + const headers: Column[] = [ + createColumnMock({ width: 140, title: 'First' }), + createColumnMock({ width: 140, title: 'Second' }), + ] + const { data } = getSimpleData(5, 2) + return + }) + .add('None (default with scroll)', () => { + const { headerData, data } = getSimpleData(10, 50) + return + }) + .add('All', () => { + const headers: Column[] = [ + createColumnMock({ width: '20%', title: 'First' }), + createColumnMock({ width: '30%', title: 'Second' }), + createColumnMock({ width: '10%', title: 'Remain' }), + ] + const { data } = getSimpleData(4, 3) + return + }) + .add('Last', () => { + const { data } = getSimpleData(10, 3) + const headers: Column[] = [ + createColumnMock({ width: '20%', title: 'First' }), + createColumnMock({ width: '30%', title: 'Second' }), + createColumnMock({ width: 200, title: 'Adjusted one' }), + ] + return + }) diff --git a/src/stories/dataUseCases.tsx b/src/stories/dataUseCases.tsx index bdc88c2..cc6af19 100644 --- a/src/stories/dataUseCases.tsx +++ b/src/stories/dataUseCases.tsx @@ -1,423 +1,419 @@ -import { getSimpleData } from "../utils/generateData" -import {Tooltip, Checkbox, IconButton} from "@material-ui/core" -import React from "react" -import { HeadersData } from "../column-grid/types/header.type" -import { GridData } from "../types/row.interface" -import PeopleIcon from "@material-ui/icons/People" +import { getSimpleData } from './generateData' +import { Tooltip, Checkbox, IconButton } from '@material-ui/core' +import React from 'react' +import { HeadersData } from '../column-grid/types/header.type' +import { GridData } from '../types/row.interface' +import PeopleIcon from '@material-ui/icons/People' import DeleteIcon from '@material-ui/icons/Delete' export const getTopUseCase = () => { - const headerData: HeadersData = [ - [ - { - id: "deliverable", - title: "Deliverable", - accessor: "deliverableBody", - width: '15%' - }, - { - // children: "Work Package", - renderer: () => { - return ( - -

Work Package

-
- ) - }, - id: "wp", - title: "Work Package", - tooltip: "You can preview the tooltip in here", - accessor: "deliverableBody", - width: '15%' - }, - { - id: "activity", - accessor: "activityBody", - title: "Activity", - width: '15%' - }, - { - id: "order", - accessor: "order", - title: "", - disableNavigation: true, - readOnly: true, - width: "2%" - }, - { - id: "task", - accessor: "taskContent", - title: "Task", - width: '16%' - }, - { - id: "dp", - accessor: "dependencies", - title: "DP", - width: "2%" - }, - { - id: "estimatedTime", - title: "Est. Time", - accessor: "estimatedTime", - width: "3%" - }, - { - id: "realTime", - title: "Real Time", - accessor: "realTime", - width: "3%" - }, - { - id: "allocated", - title: "", - renderer: () => { - return - }, - accessor: "allocated", - width: "3%" - }, - { - id: "materials", - title: "Mat Costs", - accessor: "materials", - width: "3%" - }, - { - id: "startDate", - title: "Start Date", - accessor: "startDate", - width: "5%" - }, - { - id: "endDate", - title: "End Date", - accessor: "endDate", - width: "5%" - }, - { - id: "taskControl", - title: "Task Control", - accessor: "taskControl", - width: "8%" - }, - { - id: "delete", - title: "", - renderer: () => { - return ( - - - - ) - }, - cellRenderer: () => { - return - }, - accessor: "delete", - width: "2%" - }, - ], - ] + const headerData: HeadersData = [ + [ + { + id: 'deliverable', + title: 'Deliverable', + accessor: 'deliverableBody', + width: '15%', + }, + { + // children: "Work Package", + renderer: () => { + return ( + +

Work Package

+
+ ) + }, + id: 'wp', + title: 'Work Package', + tooltip: 'You can preview the tooltip in here', + accessor: 'deliverableBody', + width: '15%', + }, + { + id: 'activity', + accessor: 'activityBody', + title: 'Activity', + width: '15%', + }, + { + id: 'order', + accessor: 'order', + title: '', + disableNavigation: true, + readOnly: true, + width: '2%', + }, + { + id: 'task', + accessor: 'taskContent', + title: 'Task', + width: '16%', + }, + { + id: 'dp', + accessor: 'dependencies', + title: 'DP', + width: '2%', + }, + { + id: 'estimatedTime', + title: 'Est. Time', + accessor: 'estimatedTime', + width: '3%', + }, + { + id: 'realTime', + title: 'Real Time', + accessor: 'realTime', + width: '3%', + }, + { + id: 'allocated', + title: '', + renderer: () => { + return + }, + accessor: 'allocated', + width: '3%', + }, + { + id: 'materials', + title: 'Mat Costs', + accessor: 'materials', + width: '3%', + }, + { + id: 'startDate', + title: 'Start Date', + accessor: 'startDate', + width: '5%', + }, + { + id: 'endDate', + title: 'End Date', + accessor: 'endDate', + width: '5%', + }, + { + id: 'taskControl', + title: 'Task Control', + accessor: 'taskControl', + width: '8%', + }, + { + id: 'delete', + title: '', + renderer: () => { + return ( + + + + ) + }, + cellRenderer: () => { + return + }, + accessor: 'delete', + width: '2%', + }, + ], + ] - const data: GridData = [ - [ - { - id: "1", - children: "My first del 1", - rowSpan: 3, - }, - { - id: "wp1", - children: "WP 1", - }, - { - id: "act1", - children: "Activity 1", - }, - { - id: "order1", - children: "1", - }, - { - id: "t1", - children: "Task 1", - }, - { - id: "dp1", - children: "[]", - }, - { - id: "es1", - children: "0", - }, - { - id: "rt1", - children: "0", - }, - { - id: "allocated1", - children: "", - }, - { - id: "materials1", - children: "0", - }, - { - id: "startDate1", - children: "", - }, - { - id: "endDate1", - children: "", - }, - { - id: "taskControl1", - children: "", - }, - { - id: 'delete1', - children: 'the trash' - } - ], - [ - { - id: "wp-id-2", - children: "WP 2", - rowSpan: 2, - }, - { - id: "act-id-111", - children: "ACT 2", - rowSpan: 2, - }, - { - id: "order2", - children: "2", - }, - { - id: "t2", - children: - "TASK 2 ssssssssssssssssssssssssssssssssssssssssssssssssssssss ddddddddddddddddddddddddddddddddddddddd", - }, - { - id: "dp2", - children: "[]", - }, - { - id: "est2", - children: "0", - }, - { - id: "rt2", - children: "0", - }, - { - id: "allocated2", - children: "", - }, - { - id: "materials2", - children: "0", - }, - { - id: "startDate2", - children: "", - }, - { - id: "endDate2", - children: "", - }, - { - id: "taskControl2", - children: "", - }, - { - id: 'delete2', - children: 'the trash' - } - ], - [ - { - id: "order3", - children: "3", - }, - { - id: "t3", - children: "Task 3", - }, - { - id: "dp3", - children: "[ 1,2 ]", - }, - { - id: "est3", - children: "123", - }, - { - id: "rt3", - children: "4", - }, - { - id: "allocated3", - children: "", - }, - { - id: "materials3", - children: "0", - }, - { - id: "startDate3", - children: "", - }, - { - id: "endDate3", - children: "", - }, - { - id: "taskControl3", - children: "", - }, - { - id: 'delete3', - children: 'the trash' - } - ], - ] + const data: GridData = [ + [ + { + id: '1', + children: 'My first del 1', + rowSpan: 3, + }, + { + id: 'wp1', + children: 'WP 1', + }, + { + id: 'act1', + children: 'Activity 1', + }, + { + id: 'order1', + children: '1', + }, + { + id: 't1', + children: 'Task 1', + }, + { + id: 'dp1', + children: '[]', + }, + { + id: 'es1', + children: '0', + }, + { + id: 'rt1', + children: '0', + }, + { + id: 'allocated1', + children: '', + }, + { + id: 'materials1', + children: '0', + }, + { + id: 'startDate1', + children: '', + }, + { + id: 'endDate1', + children: '', + }, + { + id: 'taskControl1', + children: '', + }, + { + id: 'delete1', + children: 'the trash', + }, + ], + [ + { + id: 'wp-id-2', + children: 'WP 2', + rowSpan: 2, + }, + { + id: 'act-id-111', + children: 'ACT 2', + rowSpan: 2, + }, + { + id: 'order2', + children: '2', + }, + { + id: 't2', + children: + 'TASK 2 ssssssssssssssssssssssssssssssssssssssssssssssssssssss ddddddddddddddddddddddddddddddddddddddd', + }, + { + id: 'dp2', + children: '[]', + }, + { + id: 'est2', + children: '0', + }, + { + id: 'rt2', + children: '0', + }, + { + id: 'allocated2', + children: '', + }, + { + id: 'materials2', + children: '0', + }, + { + id: 'startDate2', + children: '', + }, + { + id: 'endDate2', + children: '', + }, + { + id: 'taskControl2', + children: '', + }, + { + id: 'delete2', + children: 'the trash', + }, + ], + [ + { + id: 'order3', + children: '3', + }, + { + id: 't3', + children: 'Task 3', + }, + { + id: 'dp3', + children: '[ 1,2 ]', + }, + { + id: 'est3', + children: '123', + }, + { + id: 'rt3', + children: '4', + }, + { + id: 'allocated3', + children: '', + }, + { + id: 'materials3', + children: '0', + }, + { + id: 'startDate3', + children: '', + }, + { + id: 'endDate3', + children: '', + }, + { + id: 'taskControl3', + children: '', + }, + { + id: 'delete3', + children: 'the trash', + }, + ], + ] - return { - data, - headerData, - } + return { + data, + headerData, + } } export const getRandomUseCase = () => { - return getSimpleData(20, 8) + return getSimpleData(20, 8) } /** @todo Children might need a row grouping but we can do that with a plugin to have rows that have groups **/ export const getFinancialUseCase = () => { - const headerData = [ - [ - { - children: "Test", - }, - { - children: "2020", - }, - { - children: "2019", - }, - { - children: "2018", - }, - { - children: "2017", - }, - ], - [ - { - children: "Ongoing", - }, - { - children: "Baseline", - }, - { - children: "Ongoing", - }, - { - children: "Baseline", - }, - { - children: "Ongoing", - }, - ], - ] + const headerData = [ + [ + { + children: 'Test', + }, + { + children: '2020', + }, + { + children: '2019', + }, + { + children: '2018', + }, + { + children: '2017', + }, + ], + [ + { + children: 'Ongoing', + }, + { + children: 'Baseline', + }, + { + children: 'Ongoing', + }, + { + children: 'Baseline', + }, + { + children: 'Ongoing', + }, + ], + ] - const data = [ - ...[ - [ - { - id: "1", - children: "My first del 1", - rowSpan: 3, - }, - { - children: "WP 1", - }, - { - children: "Activity 1", - }, - { - children: "1", - }, - { - children: "Task 1", - }, - { - children: "[]", - }, - { - children: "0", - }, - { - children: "0", - }, - ], - [ - { - id: "wp-id-2", - children: "WP 2", - rowSpan: 2, - }, - { - id: "act-id-111", - children: "ACT 2", - rowSpan: 2, - }, - { - children: "2", - }, - { - children: - "TASK 2 ssssssssssssssssssssssssssssssssssssssssssssssssssssss ddddddddddddddddddddddddddddddddddddddd eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", - }, - { - children: "[]", - }, - { - children: "0", - }, - { - children: "0", - }, - ], - [ - { - children: "3", - }, - { - children: "Task 3", - }, - { - children: "[ 1,2 ]", - }, - { - children: "123", - }, - { - children: "4", - }, - ], - ], - ] + const data = [ + ...[ + [ + { + id: '1', + children: 'My first del 1', + rowSpan: 3, + }, + { + children: 'WP 1', + }, + { + children: 'Activity 1', + }, + { + children: '1', + }, + { + children: 'Task 1', + }, + { + children: '[]', + }, + { + children: '0', + }, + { + children: '0', + }, + ], + [ + { + id: 'wp-id-2', + children: 'WP 2', + rowSpan: 2, + }, + { + id: 'act-id-111', + children: 'ACT 2', + rowSpan: 2, + }, + { + children: '2', + }, + { + children: + 'TASK 2 ssssssssssssssssssssssssssssssssssssssssssssssssssssss ddddddddddddddddddddddddddddddddddddddd eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', + }, + { + children: '[]', + }, + { + children: '0', + }, + { + children: '0', + }, + ], + [ + { + children: '3', + }, + { + children: 'Task 3', + }, + { + children: '[ 1,2 ]', + }, + { + children: '123', + }, + { + children: '4', + }, + ], + ], + ] - return { - data, - headerData, - } + return { + data, + headerData, + } } diff --git a/src/utils/generateData.ts b/src/stories/generateData.ts similarity index 91% rename from src/utils/generateData.ts rename to src/stories/generateData.ts index cd02aa8..2545750 100644 --- a/src/utils/generateData.ts +++ b/src/stories/generateData.ts @@ -1,6 +1,5 @@ -// @ts-ignore import faker from "faker" -import {Column, HeadersData} from "../column/types/header.type" +import {Column, HeadersData} from "../column-grid/types/header.type" import {GridData, GridRow} from "../types/row.interface" export const getSimpleData = (rows: number, columns: number) => { @@ -29,11 +28,11 @@ export const getSimpleData = (rows: number, columns: number) => { } } - headerData.push(row) + headerData.push(row as any) } for (let i = 0; i < columns; i += 1) { - headerData[0].push({ + (headerData[0] as any).push({ id: "col-" + Math.random(), title: `Column ${i + 1}`, accessor: "", diff --git a/src/utils/helpers.tsx b/src/utils/helpers.tsx deleted file mode 100644 index 9448d99..0000000 --- a/src/utils/helpers.tsx +++ /dev/null @@ -1,170 +0,0 @@ -/* eslint-disable react/prop-types */ -import React from "react" -import { Cell } from "../types/row.interface" -/** @todo Move this file and rename into the core */ - - -const generateDummy = ({ parent, index, totalDummies, dummyFor }) => { - const style: any = {} - const first = index === 0 - const last = index === totalDummies - 1 - - /** @todo Review this part, we might only provide a className with theme to indicate whether its the last - * column or last row or even for spanns, the user might want to do something about it **/ - if (dummyFor === "colSpan") { - style.borderLeft = 0 - if (!last) { - style.borderRight = 0 - } - } else { - style.borderTop = 0 - if (!last) { - style.borderBottom = 0 - } - } - - return { - ...parent, - colSpan: 1, - rowSpan: 1, - children: "", - dummy: true, - dummyFor, - first, - last, - style: { - ...parent.style, - ...style, - }, - parentRow: parent /** @todo We might only need the id in the future or the index */, - } -} - -/**@todo Re create this without being an object and also move into a different file this kind of process - * **/ -const dummyBuffer = { - init() { - // @ts-ignore - this.buffer = new Map() - }, - dispose() { - // @ts-ignore - this.buffer = null - }, - extract(index) { - // @ts-ignore - const { buffer } = this - const arr: any[] = [] - - if (!buffer.has(index) || buffer.get(index).length === 0) { - return arr - } - - buffer.get(index).forEach((item) => { - if (!item.remainingRows) { - return - } - - arr.push( - generateDummy({ - parent: item.parent, - totalDummies: item.parent.rowSpan - 1, - index: item.parent.rowSpan - 1 - item.remainingRows, - dummyFor: "rowSpan", - }) - ) - - // eslint-disable-next-line no-param-reassign - item.remainingRows -= 1 - }) - - return arr - }, - insert(key, arr) { - // @ts-ignore - if (this.buffer.has(key)) { - // @ts-ignore - this.buffer.get(key).push(...arr) - } else { - // @ts-ignore - this.buffer.set(key, arr) - } - }, -} - -/** - * Requires a re-write due to flatMap, unknown types and the data not being passed - * with interface and as row(with cells) - * @param data - */ -export function insertDummyCells(data: any[] = []) { - dummyBuffer.init() - - const transformedData = data.map((row: any) => { - let lastRowSpanI = -1 - let finalCellIndex = 0 - - const cells = row.flatMap((col, colIndex) => { - const arr: any[] = [] - - // consume from buffer - arr.push(...dummyBuffer.extract(finalCellIndex)) - - // add dummy cell data to buffer for future rows to extract - if (col.rowSpan > 1) { - const parentData = { - remainingRows: col.rowSpan - 1, - parent: col, - } - - let bufferKey = finalCellIndex - if (lastRowSpanI !== -1 && row[colIndex - 1].rowSpan > 1) { - bufferKey = lastRowSpanI - } else { - lastRowSpanI = finalCellIndex - } - - const dummiesToPush = col.colSpan || 1 - const dummiesArray: any[] = [] - - for (let i = 0; i < dummiesToPush; i += 1) { - dummiesArray.push({ ...parentData }) - } - - dummyBuffer.insert(bufferKey, dummiesArray) - } - - arr.push({ - ...col, - }) - - if (col.colSpan > 1) { - const totalDummies = col.colSpan - 1 - const dummies = [...Array(totalDummies).keys()].map((_, index) => - generateDummy({ - parent: col, - index, - totalDummies, - dummyFor: "colSpan", - }) - ) - - arr.push(...dummies) - } - - finalCellIndex += arr.length - - return arr - }) - - // buffer has data for next cell - cells.push(...dummyBuffer.extract(finalCellIndex)) - - return cells - }) - - //Release the used memory of the map - dummyBuffer.dispose() - - return transformedData -}