Skip to content
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* This source file is available under the terms of the
* Pimcore Open Core License (POCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (https://www.pimcore.com)
* @license Pimcore Open Core License (POCL)
*/

import React, { useState } from 'react'
import { useTranslation } from 'react-i18next'
import { IconButton } from '@Pimcore/components/icon-button/icon-button'
import { CsvImportModal } from '../csv-import-modal/csv-import-modal'
import { useOperationalGridContext } from '../provider/operational-grid-provider'

export const CsvImportButton = (): React.JSX.Element => {
const { t } = useTranslation()
const { value, columns, onChange } = useOperationalGridContext()
const [isOpen, setIsOpen] = useState(false)

const handleConfirm = (newRows: any[]): void => {
onChange?.(newRows)
setIsOpen(false)
}

return (
<>
<IconButton
icon={ { value: 'edit-pen' } }
onClick={ () => { setIsOpen(true) } }
tooltip={ { title: t('operational-grid.csv-import.title') } }
type="default"
/>

<CsvImportModal
columns={ columns }
onCancel={ () => { setIsOpen(false) } }
onConfirm={ handleConfirm }
open={ isOpen }
value={ value }
/>
</>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* This source file is available under the terms of the
* Pimcore Open Core License (POCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (https://www.pimcore.com)
* @license Pimcore Open Core License (POCL)
*/

import React, { useState, useEffect } from 'react'
import { Modal } from '@Pimcore/components/modal/modal'
import { Input } from 'antd'
import { type ColumnDef } from '@tanstack/react-table'
import { useTranslation } from 'react-i18next'

export interface CsvImportModalProps {
open: boolean
columns: Array<ColumnDef<any>>
value: any[]
onConfirm: (newRows: any[]) => void
onCancel: () => void
}

const getColumnKeys = (columns: Array<ColumnDef<any>>): string[] => {
return columns
.filter(col => 'accessorKey' in col)
.map(col => ('accessorKey' in col ? (col.accessorKey as string) : ''))
.filter(key => key !== '')
}

const rowsToCsv = (rows: any[], colKeys: string[]): string => {
return rows.map(row => colKeys.map(k => row[k] ?? '').join(',')).join('\n')
}

const csvToRows = (csv: string, colKeys: string[]): any[] => {
return csv.split('\n')
.filter(line => line.trim() !== '')
.map(line => {
const cells = line.split(',')
return Object.fromEntries(colKeys.map((k, i) => [k, cells[i]?.trim() ?? '']))
})
}

export const CsvImportModal = (props: CsvImportModalProps): React.JSX.Element => {
const { open, columns, value, onConfirm, onCancel } = props
const { t } = useTranslation()
const [csvText, setCsvText] = useState('')

const colKeys = getColumnKeys(columns)

useEffect(() => {
if (open) {
setCsvText(rowsToCsv(value, colKeys))
}
}, [open])

const handleOk = (): void => {
const newRows = csvToRows(csvText, colKeys)
onConfirm(newRows)
}

return (
<Modal
cancelText={ t('operational-grid.csv-import.cancel') }
okText={ t('operational-grid.csv-import.ok') }
onCancel={ onCancel }
onOk={ handleOk }
open={ open }
title={ t('operational-grid.csv-import.title') }
>
<p>{t('operational-grid.csv-import.description')}</p>
<Input.TextArea
onChange={ (e) => { setCsvText(e.target.value) } }
rows={ 10 }
value={ csvText }
/>
</Modal>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { createColumnHelper, type RowSelectionState, type ColumnDef } from '@tan
import { DefaultCell } from '../grid/columns/default-cell'
import { Space } from 'antd'
import { IconButton } from '../icon-button/icon-button'
import { CsvImportButton } from './csv-import-button/csv-import-button'
import { type DragEndEvent } from '@dnd-kit/core'

const config: Meta = {
Expand Down Expand Up @@ -82,6 +83,7 @@ const config: Meta = {
</IconButton>
</>
)}
<CsvImportButton />
</Space>
)
}}
Expand Down Expand Up @@ -322,6 +324,7 @@ export const WithColumnOperations = {
</IconButton>
</Space>
</div>
<CsvImportButton />
</Space>
)
}}
Expand Down Expand Up @@ -437,6 +440,7 @@ export const WithDragAndDrop = {
Reset Order
</IconButton>
</div>
<CsvImportButton />
</Space>
)
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @license Pimcore Open Core License (POCL)
*/

import { Box, ButtonGroup, IconButton, OperationalGrid, Space } from '@sdk/components'
import { Box, ButtonGroup, CsvImportButton, IconButton, OperationalGrid, Space } from '@sdk/components'
import { type ColumnDef, createColumnHelper } from '@tanstack/react-table'
import React, { useMemo } from 'react'
import { useTranslation } from 'react-i18next'
Expand Down Expand Up @@ -138,6 +138,7 @@ export const SelectOptionEntriesGrid = ({ value = [], onChange }: SelectOptionEn
tooltip={ { title: t('add') } }
type="default"
/>
<CsvImportButton />
</Space>
)
}}
Expand Down
1 change: 1 addition & 0 deletions assets/js/src/sdk/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ export * from '@Pimcore/components/vertical-timeline/vertical-timeline'
export * from '@Pimcore/components/predefined-layouts/config/config-layout'
export * from '@Pimcore/components/tabpanel/tabpanel'
export * from '@Pimcore/components/operational-grid/operational-grid'
export * from '@Pimcore/components/operational-grid/csv-import-button/csv-import-button'
export * from '@Pimcore/components/operational-grid/hooks/use-operations'
export * from '@Pimcore/components/url-link/url-link'

Expand Down
22 changes: 0 additions & 22 deletions public/build/1bbc463c-5b2c-499b-ac61-f4eeb3381bdf/entrypoints.json

This file was deleted.

33 changes: 0 additions & 33 deletions public/build/1bbc463c-5b2c-499b-ac61-f4eeb3381bdf/manifest.json

This file was deleted.

23 changes: 0 additions & 23 deletions public/build/303cdcf1-58ee-4ae4-8040-42836066132b/entrypoints.json

This file was deleted.

Loading
Loading