-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add structured table data object data type (#761)
- Loading branch information
1 parent
6c7cd2a
commit f1748df
Showing
34 changed files
with
2,817 additions
and
1 deletion.
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
22 changes: 22 additions & 0 deletions
22
...s/element/dynamic-types/defintinitions/grid-cell/components/number/number-cell.styles.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,22 @@ | ||
/** | ||
* 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 { createStyles } from 'antd-style' | ||
|
||
export const useStyle = createStyles(({ token, css }) => { | ||
return { | ||
'number-cell': css` | ||
padding: 4px; | ||
` | ||
} | ||
}) |
78 changes: 78 additions & 0 deletions
78
.../modules/element/dynamic-types/defintinitions/grid-cell/components/number/number-cell.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,78 @@ | ||
/** | ||
* 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, useRef, useState } from 'react' | ||
import { InputNumber } from 'antd' | ||
import { useStyle } from './number-cell.styles' | ||
import { type DefaultCellProps } from '@Pimcore/components/grid/columns/default-cell' | ||
import { useEditMode } from '@Pimcore/components/grid/edit-mode/use-edit-mode' | ||
import cn from 'classnames' | ||
import type { InputNumberRef } from 'rc-input-number' | ||
|
||
export interface NumberCellProps extends DefaultCellProps {} | ||
|
||
export const NumberCell = (props: NumberCellProps): React.JSX.Element => { | ||
const { isInEditMode, disableEditMode, fireOnUpdateCellDataEvent } = useEditMode(props) | ||
const [value, setValue] = useState<number | null>(props.getValue() as number) | ||
const { styles } = useStyle() | ||
const element = useRef<InputNumberRef>(null) | ||
|
||
useEffect(() => { | ||
if (isInEditMode) { | ||
element.current?.focus() | ||
} | ||
}, [isInEditMode]) | ||
|
||
function saveValue (): void { | ||
fireOnUpdateCellDataEvent(value) | ||
disableEditMode() | ||
} | ||
|
||
function onKeyDown (event: React.KeyboardEvent<HTMLInputElement>): void { | ||
if (event.key === 'Enter') { | ||
saveValue() | ||
} | ||
} | ||
|
||
function onBlur (): void { | ||
saveValue() | ||
} | ||
|
||
function getCellContent (): React.JSX.Element { | ||
if (!isInEditMode) { | ||
return ( | ||
<> | ||
{ props.getValue() } | ||
</> | ||
) | ||
} | ||
|
||
return ( | ||
<InputNumber | ||
className="w-full" | ||
defaultValue={ props.getValue() } | ||
onBlur={ onBlur } | ||
onChange={ setValue } | ||
onKeyDown={ onKeyDown } | ||
ref={ element } | ||
value={ value } | ||
/> | ||
) | ||
} | ||
|
||
return ( | ||
<div className={ cn(styles['number-cell'], 'default-cell__content') }> | ||
{ getCellContent() } | ||
</div> | ||
) | ||
} |
26 changes: 26 additions & 0 deletions
26
...ent/dynamic-types/defintinitions/grid-cell/types/number/dynamic-type-grid-cell-number.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,26 @@ | ||
/** | ||
* 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, { type ReactElement } from 'react' | ||
import { type AbstractGridCellDefinition, DynamicTypeGridCellAbstract } from '../../dynamic-type-grid-cell-abstract' | ||
import { NumberCell } from '../../components/number/number-cell' | ||
import { injectable } from 'inversify' | ||
|
||
@injectable() | ||
export class DynamicTypeGridCellNumber extends DynamicTypeGridCellAbstract { | ||
readonly id = 'number' | ||
|
||
getGridCellComponent (props: AbstractGridCellDefinition): ReactElement<AbstractGridCellDefinition> { | ||
return <NumberCell { ...props } /> | ||
} | ||
} |
104 changes: 104 additions & 0 deletions
104
.../defintinitions/objects/data-related/components/structured-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,104 @@ | ||
/** | ||
* 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 StructuredTableCol, | ||
type StructuredTableColType, type StructuredTableColumnValue, | ||
type StructuredTableRow, | ||
type StructuredTableValue | ||
} from '@Pimcore/modules/element/dynamic-types/defintinitions/objects/data-related/components/structured-table/structured-table' | ||
import { useTranslation } from 'react-i18next' | ||
import _ from 'lodash' | ||
|
||
interface StructuredTableGridProps { | ||
castColumnValue: (value: StructuredTableColumnValue, key: string) => StructuredTableColumnValue | ||
cols: StructuredTableCol[] | ||
rows: StructuredTableRow[] | ||
labelWidth: number | null | ||
labelFirstCell: string | null | ||
value: StructuredTableValue | null | ||
onChange?: (value: StructuredTableValue | null) => void | ||
} | ||
|
||
export const StructuredTableGrid = (props: StructuredTableGridProps): React.JSX.Element => { | ||
const columnHelper = createColumnHelper() | ||
const { t } = useTranslation() | ||
|
||
const mapColType = (type: StructuredTableColType): string => { | ||
switch (type) { | ||
case 'text': | ||
return 'text' | ||
case 'bool': | ||
return 'checkbox' | ||
case 'number': | ||
return 'number' | ||
default: | ||
return 'text' | ||
} | ||
} | ||
|
||
const applyColWidth = (colWidth?: number | null): number => { | ||
return !_.isEmpty(colWidth) && colWidth! > 0 ? colWidth! : 100 | ||
} | ||
|
||
const columns: Array<ColumnDef<any>> = [ | ||
columnHelper.accessor('rowLabel', { | ||
header: !_.isEmpty(props.labelFirstCell) ? t(props.labelFirstCell!) : '', | ||
size: applyColWidth(props.labelWidth) | ||
}) | ||
] | ||
props.cols.forEach((col) => { | ||
columns.push( | ||
columnHelper.accessor(col.key, { | ||
header: t(col.label), | ||
size: applyColWidth(col.width), | ||
meta: { | ||
type: mapColType(col.type), | ||
editable: true | ||
} | ||
}) | ||
) | ||
}) | ||
|
||
const rows = props.rows.map((row) => { | ||
const rowData = { | ||
rowLabel: t(row.label) | ||
} | ||
props.cols.forEach((col) => { | ||
rowData[col.key] = props.castColumnValue(props.value?.[row.key]?.[col.key] ?? null, col.key) | ||
}) | ||
return rowData | ||
}) | ||
|
||
return ( | ||
<Grid | ||
columns={ columns } | ||
data={ rows } | ||
onUpdateCellData={ (data) => { | ||
const newValue = { | ||
...props.value, | ||
[props.rows[data.rowIndex].key]: { | ||
...props.value?.[props.rows[data.rowIndex].key], | ||
[data.columnId]: props.castColumnValue(data.value as StructuredTableColumnValue, data.columnId) | ||
} | ||
} | ||
|
||
props.onChange?.(newValue) | ||
} } | ||
resizable | ||
/> | ||
) | ||
} |
Oops, something went wrong.