1.0.1 • Published 9 months ago

antd-sortable-table v1.0.1

Weekly downloads
-
License
ISC
Repository
github
Last release
9 months ago

antd-sortable-table

antd table with dnd an sortable table editor based on antd and dnd-kit

English | 简体中文

When To Use

  • Provides an editable table that implements common scenarios such as sorting, adding rows, copying rows, deleting rows, etc.

demo

https://lvyangxu.github.io/antd-sortable-table/

📦 Install

npm i antd-sortable-table -S

API

import { ColumnsType, TableProps } from 'antd/es/table'

export type RowKeyType = {
    /** row index of value */
    _rowIndex?: number
    /** sortKey for dnd, don't change it */
    _sortKey?: number
} & object

export interface TableEditorProps<T extends RowKeyType> {
    /** element id, useful for antd form item */
    id?: string
    /** locale config, auto sync with antd locale, only support zh-cn and en */
    locale?: Locale
    /** antd table datasource,auto add _rowIndex (row index number) and _sortKey (used by sort) for every row */
    value?: T[]
    /** datasource change event */
    onChange?: (v: TableEditorProps<T>['value']) => void
    /** table editor state,control table should show edit dom */
    isPreview: boolean
    /** default value for a new row */
    defaultRowValueFunc: (v: TableEditorProps<T>['value']) => T
    /** a function return antd table columns */
    columnsFunc: (v: Required<Pick<TableEditorProps<T & RowKeyType>, 'value' | 'onChange'>>) => ColumnsType<T & RowKeyType>
    /** table edit validator,only trigger when some row add or delete */
    onEditValidator?: (v: { type: 'add' | 'delete'; value: TableEditorProps<T>['value'] }) => void
    /** control table row can be sorted,default true */
    sortable?: boolean
    /** force sort on preview state, useful for sort table row without change column data ,default false */
    forceSortable?: boolean
    /** antd table props */
    tableProps?: Omit<TableProps<T>, 'dataSource' | 'columns' | 'title' | 'rowKey'>
    /** copy tranform function, happen before copy a row to a new row */
    copyTransform?: (v: T) => T
}

export interface Locale {
    locale: 'zh-cn' | 'en'
    data: {
        sortColumnTitle: string
        actionColumnTitle: string
        copyButtonText: string
        deleteButtonText: string
        addButtonText: string
        clearButtonText: string
    }
}
parameterdescriptiontyperequireddefaultexample
idelement id, convenient for form componentsstringno"my-editor"
localeInternationalization, the default is synchronized with antd's internationalization settings, currently only Simplified Chinese and EnglishobjectNo
valuethe current value of the table, the datasource of antd tablearrayno[]{"aaa":"xxx","bbb":"xxx"}
onChangecallback for value changefunctionno
isPreviewWhether it is a preview, with columns to customize the ui displaybooleanyesfalse
defaultRowValueFuncThe default value when adding a row, the parameter is the current value of the tablefunctionyes
columnsFuncgenerating function of table columns, the json parameter is the current value of the table and the change callbackfunctionis
onEditValidatorThe validation function when the table row changes, if it is illegal, it will throw an exception directly, it can be used to control the maximum and minimum number of rows, etc.functionNo
sortableWhether to enable sorting in the edit state, not in the preview statebooleanNotrue
forceSortableForced sorting, also valid in the preview statebooleannofalse
tablePropsTransparent transmission of antd table properties, except dataSource, columns, caption, rowKeyobjectNo
copyTransformBefore copying a column, transform the data of a new rowfunctionNo

🔨 code demo

demo