Skip to content

Commit 3d950f2

Browse files
authored
Added csv edit modal (#3351)
* added csv edit modal * Automatic frontend build * update icon * Automatic frontend build * refined `CsvImportButton` * Apply eslint-fixer changes * removed not used prop * Automatic frontend build --------- Co-authored-by: Corepex <16717695+Corepex@users.noreply.github.com>
1 parent c794407 commit 3d950f2

752 files changed

Lines changed: 30128 additions & 1 deletion

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* This source file is available under the terms of the
3+
* Pimcore Open Core License (POCL)
4+
* Full copyright and license information is available in
5+
* LICENSE.md which is distributed with this source code.
6+
*
7+
* @copyright Copyright (c) Pimcore GmbH (https://www.pimcore.com)
8+
* @license Pimcore Open Core License (POCL)
9+
*/
10+
11+
import React, { useState } from 'react'
12+
import { useTranslation } from 'react-i18next'
13+
import { IconButton } from '@Pimcore/components/icon-button/icon-button'
14+
import { CsvImportModal } from '../csv-import-modal/csv-import-modal'
15+
import { useOperationalGridContext } from '../provider/operational-grid-provider'
16+
17+
export const CsvImportButton = (): React.JSX.Element => {
18+
const { t } = useTranslation()
19+
const { value, columns, onChange } = useOperationalGridContext()
20+
const [isOpen, setIsOpen] = useState(false)
21+
22+
const handleConfirm = (newRows: any[]): void => {
23+
onChange?.(newRows)
24+
setIsOpen(false)
25+
}
26+
27+
return (
28+
<>
29+
<IconButton
30+
icon={ { value: 'edit-pen' } }
31+
onClick={ () => { setIsOpen(true) } }
32+
tooltip={ { title: t('operational-grid.csv-import.title') } }
33+
type="default"
34+
/>
35+
36+
<CsvImportModal
37+
columns={ columns }
38+
onCancel={ () => { setIsOpen(false) } }
39+
onConfirm={ handleConfirm }
40+
open={ isOpen }
41+
value={ value }
42+
/>
43+
</>
44+
)
45+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/**
2+
* This source file is available under the terms of the
3+
* Pimcore Open Core License (POCL)
4+
* Full copyright and license information is available in
5+
* LICENSE.md which is distributed with this source code.
6+
*
7+
* @copyright Copyright (c) Pimcore GmbH (https://www.pimcore.com)
8+
* @license Pimcore Open Core License (POCL)
9+
*/
10+
11+
import React, { useState, useEffect } from 'react'
12+
import { Modal } from '@Pimcore/components/modal/modal'
13+
import { Input } from 'antd'
14+
import { type ColumnDef } from '@tanstack/react-table'
15+
import { useTranslation } from 'react-i18next'
16+
17+
export interface CsvImportModalProps {
18+
open: boolean
19+
columns: Array<ColumnDef<any>>
20+
value: any[]
21+
onConfirm: (newRows: any[]) => void
22+
onCancel: () => void
23+
}
24+
25+
const getColumnKeys = (columns: Array<ColumnDef<any>>): string[] => {
26+
return columns
27+
.filter(col => 'accessorKey' in col)
28+
.map(col => ('accessorKey' in col ? (col.accessorKey as string) : ''))
29+
.filter(key => key !== '')
30+
}
31+
32+
const rowsToCsv = (rows: any[], colKeys: string[]): string => {
33+
return rows.map(row => colKeys.map(k => row[k] ?? '').join(',')).join('\n')
34+
}
35+
36+
const csvToRows = (csv: string, colKeys: string[]): any[] => {
37+
return csv.split('\n')
38+
.filter(line => line.trim() !== '')
39+
.map(line => {
40+
const cells = line.split(',')
41+
return Object.fromEntries(colKeys.map((k, i) => [k, cells[i]?.trim() ?? '']))
42+
})
43+
}
44+
45+
export const CsvImportModal = (props: CsvImportModalProps): React.JSX.Element => {
46+
const { open, columns, value, onConfirm, onCancel } = props
47+
const { t } = useTranslation()
48+
const [csvText, setCsvText] = useState('')
49+
50+
const colKeys = getColumnKeys(columns)
51+
52+
useEffect(() => {
53+
if (open) {
54+
setCsvText(rowsToCsv(value, colKeys))
55+
}
56+
}, [open])
57+
58+
const handleOk = (): void => {
59+
const newRows = csvToRows(csvText, colKeys)
60+
onConfirm(newRows)
61+
}
62+
63+
return (
64+
<Modal
65+
cancelText={ t('operational-grid.csv-import.cancel') }
66+
okText={ t('operational-grid.csv-import.ok') }
67+
onCancel={ onCancel }
68+
onOk={ handleOk }
69+
open={ open }
70+
title={ t('operational-grid.csv-import.title') }
71+
>
72+
<p>{t('operational-grid.csv-import.description')}</p>
73+
<Input.TextArea
74+
onChange={ (e) => { setCsvText(e.target.value) } }
75+
rows={ 10 }
76+
value={ csvText }
77+
/>
78+
</Modal>
79+
)
80+
}

assets/js/src/core/components/operational-grid/operational-grid.stories.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { createColumnHelper, type RowSelectionState, type ColumnDef } from '@tan
1515
import { DefaultCell } from '../grid/columns/default-cell'
1616
import { Space } from 'antd'
1717
import { IconButton } from '../icon-button/icon-button'
18+
import { CsvImportButton } from './csv-import-button/csv-import-button'
1819
import { type DragEndEvent } from '@dnd-kit/core'
1920

2021
const config: Meta = {
@@ -82,6 +83,7 @@ const config: Meta = {
8283
</IconButton>
8384
</>
8485
)}
86+
<CsvImportButton />
8587
</Space>
8688
)
8789
}}
@@ -322,6 +324,7 @@ export const WithColumnOperations = {
322324
</IconButton>
323325
</Space>
324326
</div>
327+
<CsvImportButton />
325328
</Space>
326329
)
327330
}}
@@ -437,6 +440,7 @@ export const WithDragAndDrop = {
437440
Reset Order
438441
</IconButton>
439442
</div>
443+
<CsvImportButton />
440444
</Space>
441445
)
442446
}}

assets/js/src/core/modules/select-option/components/select-option-editor/select-option-entries-grid.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* @license Pimcore Open Core License (POCL)
99
*/
1010

11-
import { Box, ButtonGroup, IconButton, OperationalGrid, Space } from '@sdk/components'
11+
import { Box, ButtonGroup, CsvImportButton, IconButton, OperationalGrid, Space } from '@sdk/components'
1212
import { type ColumnDef, createColumnHelper } from '@tanstack/react-table'
1313
import React, { useMemo } from 'react'
1414
import { useTranslation } from 'react-i18next'
@@ -138,6 +138,7 @@ export const SelectOptionEntriesGrid = ({ value = [], onChange }: SelectOptionEn
138138
tooltip={ { title: t('add') } }
139139
type="default"
140140
/>
141+
<CsvImportButton />
141142
</Space>
142143
)
143144
}}

assets/js/src/sdk/components/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ export * from '@Pimcore/components/vertical-timeline/vertical-timeline'
197197
export * from '@Pimcore/components/predefined-layouts/config/config-layout'
198198
export * from '@Pimcore/components/tabpanel/tabpanel'
199199
export * from '@Pimcore/components/operational-grid/operational-grid'
200+
export * from '@Pimcore/components/operational-grid/csv-import-button/csv-import-button'
200201
export * from '@Pimcore/components/operational-grid/hooks/use-operations'
201202
export * from '@Pimcore/components/url-link/url-link'
202203

public/build/5e46c027-81ac-46ad-b5e2-d7214590a003/documentEditorIframe.html

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/build/5e46c027-81ac-46ad-b5e2-d7214590a003/entrypoints.json

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/build/5e46c027-81ac-46ad-b5e2-d7214590a003/exposeRemote.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/build/5e46c027-81ac-46ad-b5e2-d7214590a003/main.html

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/build/5e46c027-81ac-46ad-b5e2-d7214590a003/manifest.json

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)