-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0824a7d
commit b370f58
Showing
8 changed files
with
247 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
...namic-types/defintinitions/objects/data-related/components/table/components/grid/grid.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/** | ||
* Pimcore | ||
* | ||
* This source file is available under two different licenses: | ||
* - Pimcore Open Core License (POCL) | ||
* - Pimcore Commercial License (PCL) | ||
* Full copyright and license information is available in | ||
* LICENSE.md which is distributed with this source code. | ||
* | ||
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org) | ||
* @license https://github.com/pimcore/studio-ui-bundle/blob/1.x/LICENSE.md POCL and PCL | ||
*/ | ||
|
||
import React from 'react' | ||
import { Grid } from '@Pimcore/components/grid/grid' | ||
import { type ColumnDef, createColumnHelper } from '@tanstack/react-table' | ||
import { | ||
type TableValue | ||
} from '@Pimcore/modules/element/dynamic-types/defintinitions/objects/data-related/components/table/table' | ||
|
||
interface TableGridProps { | ||
cols: number | null | ||
rows: number | null | ||
value: TableValue | null | ||
onChange?: (value: TableValue | null) => void | ||
} | ||
|
||
export const TableGrid = (props: TableGridProps): React.JSX.Element => { | ||
const columnHelper = createColumnHelper() | ||
|
||
const cols = props.cols ?? 5 | ||
const rows = props.rows ?? 7 | ||
|
||
const columns: Array<ColumnDef<any>> = [] | ||
for (let i = 0; i < cols; i++) { | ||
columns.push( | ||
columnHelper.accessor('col-' + i, { | ||
meta: { | ||
type: 'text', | ||
editable: true | ||
} | ||
}) | ||
) | ||
} | ||
|
||
const dataRows: Array<Record<string, string>> = [] | ||
for (let i = 0; i < rows; i++) { | ||
const rowValues = {} | ||
for (let j = 0; j < cols; j++) { | ||
rowValues['col-' + j] = props.value?.[i]?.[j] ?? '' | ||
} | ||
dataRows.push(rowValues) | ||
} | ||
|
||
return ( | ||
<Grid | ||
|
||
columns={ columns } | ||
data={ dataRows } | ||
hideColumnHeaders | ||
onUpdateCellData={ (data) => { | ||
const newDataRows = { | ||
...dataRows, | ||
[data.rowIndex]: { | ||
...dataRows?.[data.rowIndex], | ||
[data.columnId]: data.value | ||
} | ||
} | ||
|
||
const newValue = Object.values(newDataRows).map(row => Object.values(row)) | ||
console.log('new value', newValue, newDataRows) | ||
props.onChange?.(newValue) | ||
} } | ||
resizable | ||
/> | ||
) | ||
} |
80 changes: 80 additions & 0 deletions
80
...ules/element/dynamic-types/defintinitions/objects/data-related/components/table/table.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/** | ||
* Pimcore | ||
* | ||
* This source file is available under two different licenses: | ||
* - Pimcore Open Core License (POCL) | ||
* - Pimcore Commercial License (PCL) | ||
* Full copyright and license information is available in | ||
* LICENSE.md which is distributed with this source code. | ||
* | ||
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org) | ||
* @license https://github.com/pimcore/studio-ui-bundle/blob/1.x/LICENSE.md POCL and PCL | ||
*/ | ||
|
||
import React, { useEffect, useState } from 'react' | ||
import { TableGrid } from './components/grid/grid' | ||
import { Box } from '@Pimcore/components/box/box' | ||
import { IconButton } from '@Pimcore/components/icon-button/icon-button' | ||
import { Tooltip } from 'antd' | ||
import { useTranslation } from 'react-i18next' | ||
import { useFormModal } from '@Pimcore/components/modal/form-modal/hooks/use-form-modal' | ||
|
||
export interface TableProps { | ||
rows: number | null | ||
cols: number | null | ||
value?: TableValue | null | ||
onChange?: (value: TableValue | null) => void | ||
} | ||
|
||
export type TableValue = string[][] | ||
|
||
export const Table = (props: TableProps): React.JSX.Element => { | ||
const [value, setValue] = useState<TableValue | null>(props.value ?? null) | ||
const [key, setKey] = useState<number>(0) | ||
const { t } = useTranslation() | ||
const { confirm } = useFormModal() | ||
|
||
const onChange = (value: TableValue | null): void => { | ||
setValue(value) | ||
} | ||
|
||
useEffect(() => { | ||
if (props.onChange !== undefined) { | ||
props.onChange(value) | ||
} | ||
}, [value]) | ||
|
||
const emptyValue = (): void => { | ||
if (value !== null) { | ||
setValue([]) | ||
setKey(key + 1) // force re-render | ||
} | ||
} | ||
|
||
return ( | ||
<> | ||
<TableGrid | ||
cols={ props.cols } | ||
key={ key } | ||
onChange={ onChange } | ||
rows={ props.rows } | ||
value={ value } | ||
/> | ||
<Box padding="extra-small"> | ||
<Tooltip title={ t('empty') }> | ||
<IconButton | ||
icon={ { value: 'trash' } } | ||
onClick={ () => { | ||
confirm({ | ||
title: t('empty'), | ||
content: t('structured-table.empty.confirm'), | ||
onOk: emptyValue | ||
}) | ||
} } | ||
type="default" | ||
/> | ||
</Tooltip> | ||
</Box> | ||
</> | ||
) | ||
} |
32 changes: 32 additions & 0 deletions
32
...ynamic-types/defintinitions/objects/data-related/types/dynamic-type-object-data-table.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* Pimcore | ||
* | ||
* This source file is available under two different licenses: | ||
* - Pimcore Open Core License (POCL) | ||
* - Pimcore Commercial License (PCL) | ||
* Full copyright and license information is available in | ||
* LICENSE.md which is distributed with this source code. | ||
* | ||
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org) | ||
* @license https://github.com/pimcore/studio-ui-bundle/blob/1.x/LICENSE.md POCL and PCL | ||
*/ | ||
|
||
import React from 'react' | ||
import { | ||
type AbstractObjectDataDefinition, DynamicTypeObjectDataAbstract | ||
} from '@Pimcore/modules/element/dynamic-types/defintinitions/objects/data-related/dynamic-type-object-data-abstract' | ||
import { | ||
Table, type TableProps | ||
} from '@Pimcore/modules/element/dynamic-types/defintinitions/objects/data-related/components/table/table' | ||
|
||
export type TableObjectDataDefinition = AbstractObjectDataDefinition & TableProps | ||
|
||
export class DynamicTypeObjectDataTable extends DynamicTypeObjectDataAbstract { | ||
id: string = 'table' | ||
|
||
getObjectDataComponent (props: TableObjectDataDefinition): React.ReactElement<AbstractObjectDataDefinition> { | ||
return ( | ||
<Table { ...props } /> | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters