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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions docs/formily/tableSelection.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
Consumer,
ButtonGroup,
createListActions,
createAsyncListActions,
ListLifeCycleTypes,
ListSelection
} from '@alist/antd-components'
Expand All @@ -32,7 +33,8 @@ import { Button } from 'antd'
import Printer from '@formily/printer'
import 'antd/dist/antd.css'

const listActions = createListActions()
// const listActions = createListActions()
const listActions = createAsyncListActions()
const actions = createFormActions()

const App = () => {
Expand Down Expand Up @@ -140,6 +142,7 @@ import {
Consumer,
ButtonGroup,
createListActions,
createAsyncListActions,
ListLifeCycleTypes,
ListSelection
} from '@alist/next-components'
Expand All @@ -160,7 +163,8 @@ import { Icon, Button } from '@alifd/next'
import Printer from '@formily/printer'
import '@alifd/next/dist/next.css'

const listActions = createListActions()
// const listActions = createListActions()
const listActions = createAsyncListActions()
const actions = createFormActions()

const App = () => {
Expand Down
104 changes: 55 additions & 49 deletions packages/antd-components/src/hooks/useAntdList.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useEva, useList, ListLifeCycleTypes, ITableProps, ITableHook, IListSelectionConfig, ListContext } from '@alist/react'
import { useRef, useContext } from 'react'
import { createAntdListActions, setSelectionsByInstance } from '../shared'
import { createAntdListActions, setSelectionsByInstance, actionsRetHandler } from '../shared'

const useAntdList = (props: ITableProps = {}): ITableHook => {
const actionsRef = useRef<any>(null)
Expand All @@ -15,18 +15,18 @@ const useAntdList = (props: ITableProps = {}): ITableHook => {

implementActions({
setSelections: (ids, records) => {
setSelectionsByInstance(actionsRef.current, ids, records)
setSelectionsByInstance(actionsRef.current, ids, records)
},
disableRowSelection: () => {
const { className = '' } = actionsRef.current.getTableProps()
actionsRef.current.setSelectionConfig(null)
actionsRef.current.setSelectionConfig(null)
actionsRef.current.setTableProps({ // 刷新
className: className.replace(` ${hasRowSelectionCls}`, ''),
rowSelection: undefined
})
},
getRowSelection: () => {
const selectionConfig = actionsRef.current.getSelectionConfig()
const selectionConfig = actionsRef.current.getSelectionConfig()
let config = null
if (selectionConfig) {
const dataSource = actionsRef.current.getPaginationDataSource()
Expand All @@ -44,53 +44,59 @@ const useAntdList = (props: ITableProps = {}): ITableHook => {
},
setRowSelection: (selectionConfig: IListSelectionConfig) => {
actionsRef.current.setSelectionConfig(selectionConfig)
const config = actionsRef.current.getSelectionConfig()
const { className = '' } = actionsRef.current.getTableProps()
if (config) {
const { mode, ids, primaryKey, getProps, ...others } = config
actionsRef.current.setTableProps({ // 刷新
className: className.indexOf(hasRowSelectionCls) !== -1 ? className : `${className} ${hasRowSelectionCls}`,
rowSelection: {
...others,
type: mode === 'multiple' ? 'checkbox' : 'radio',
selectedRowKeys: ids,
key: primaryKey,
onSelect: (record, selected, records) => {
actionsRef.current.notify(ListLifeCycleTypes.ON_LIST_SELECT, {
selected, record, records
})
},
onSelectAll: (selected, records) => {
actionsRef.current.notify(ListLifeCycleTypes.ON_LIST_SELECT_ALL, {
selected, records
})
},
onChange: (changeIds: string[], records: any[]) => {
actionsRef.current.setSelectionConfig({
ids: changeIds,
records,
})
actionsRef.current.notify(ListLifeCycleTypes.ON_LIST_SELECT_CHANGE, {
ids: changeIds, records
})
const { rowSelection } = actionsRef.current.getTableProps()
actionsRef.current.setTableProps({
rowSelection: {
...rowSelection,
selectedRowKeys: changeIds,
selections: records,
}
})
},
getCheckboxProps: getProps,
const getSelectionConfigRet = actionsRef.current.getSelectionConfig()
actionsRetHandler(getSelectionConfigRet, (config) => {
const getTablePropsRet = actionsRef.current.getTableProps()
actionsRetHandler(getTablePropsRet, ({ className = '' }) => {
if (config) {
const { mode, ids, primaryKey, getProps, ...others } = config
actionsRef.current.setTableProps({ // 刷新
className: className.indexOf(hasRowSelectionCls) !== -1 ? className : `${className} ${hasRowSelectionCls}`,
rowSelection: {
...others,
type: mode === 'multiple' ? 'checkbox' : 'radio',
selectedRowKeys: ids,
key: primaryKey,
onSelect: (record, selected, records) => {
actionsRef.current.notify(ListLifeCycleTypes.ON_LIST_SELECT, {
selected, record, records
})
},
onSelectAll: (selected, records) => {
actionsRef.current.notify(ListLifeCycleTypes.ON_LIST_SELECT_ALL, {
selected, records
})
},
onChange: (changeIds: string[], records: any[]) => {
actionsRef.current.setSelectionConfig({
ids: changeIds,
records,
})
actionsRef.current.notify(ListLifeCycleTypes.ON_LIST_SELECT_CHANGE, {
ids: changeIds, records
})
const getTablePropsRet = actionsRef.current.getTableProps()
actionsRetHandler(getTablePropsRet, ({ rowSelection }) => {
actionsRef.current.setTableProps({
rowSelection: {
...rowSelection,
selectedRowKeys: changeIds,
selections: records,
}
})
})
},
getCheckboxProps: getProps,
}
})
} else {
actionsRef.current.setTableProps({ // 刷新
className: className.replace(` ${hasRowSelectionCls}`, ''),
rowSelection: undefined
})
}
})
} else {
actionsRef.current.setTableProps({ // 刷新
className: className.replace(` ${hasRowSelectionCls}`, ''),
rowSelection: undefined
})
}
});
}
})
const { effects } = props
Expand Down
12 changes: 12 additions & 0 deletions packages/antd-components/src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,15 @@ export const setSelectionsByInstance = (instance, ids, records) => {
records
})
}

export function actionsRetHandler(ret, callback, asyncErrCallback = console.warn) {
if (ret instanceof Promise) {
ret.then((...t) => {
typeof callback === 'function' && callback(...t);
}).catch((err) => {
typeof asyncErrCallback === 'function' && asyncErrCallback(err)
});
} else {
callback(ret);
}
}
99 changes: 53 additions & 46 deletions packages/next-components/src/hooks/useNextList.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useEva, useList, ListLifeCycleTypes, ITableProps, ITableHook, IListSelectionConfig, ListContext } from '@alist/react'
import { useRef, useContext } from 'react'
import { createNextListActions, setSelectionsByInstance } from '../shared'
import { createNextListActions, setSelectionsByInstance, actionsRetHandler } from '../shared'

const useNextList = (props: ITableProps = {}): ITableHook => {
const actionsRef = useRef<any>(null)
Expand All @@ -26,7 +26,7 @@ const useNextList = (props: ITableProps = {}): ITableHook => {
})
},
getRowSelection: () => {
const selectionConfig = actionsRef.current.getSelectionConfig()
const selectionConfig = actionsRef.current.getSelectionConfig()
let config = null
if (selectionConfig) {
const dataSource = actionsRef.current.getPaginationDataSource()
Expand All @@ -44,52 +44,59 @@ const useNextList = (props: ITableProps = {}): ITableHook => {
},
setRowSelection: (selectionConfig: IListSelectionConfig) => {
actionsRef.current.setSelectionConfig(selectionConfig)
const config = actionsRef.current.getSelectionConfig()
const { className = '', primaryKey: defaultPrimaryKey } = actionsRef.current.getTableProps()
if (config) {
const { mode, ids, primaryKey = defaultPrimaryKey, getProps, ...others } = config
actionsRef.current.setTableProps({ // 刷新
className: className.indexOf(hasRowSelectionCls) !== -1 ? className : `${className} ${hasRowSelectionCls}`,
rowSelection: {
...others,
mode,
selectedRowKeys: ids,
primaryKey,
onSelect: (selected, record, records) => {
actionsRef.current.notify(ListLifeCycleTypes.ON_LIST_SELECT, {
selected, record, records
})
},
onSelectAll: (selected, records) => {
actionsRef.current.notify(ListLifeCycleTypes.ON_LIST_SELECT_ALL, {
selected, records
})
},
onChange: (changeIds: string[], records: any[]) => {
actionsRef.current.setSelectionConfig({
ids: changeIds,
records,
})
actionsRef.current.notify(ListLifeCycleTypes.ON_LIST_SELECT_CHANGE, {
ids: changeIds, records
})
const { rowSelection } = actionsRef.current.getTableProps()
actionsRef.current.setTableProps({
rowSelection: {
...rowSelection,
selectedRowKeys: changeIds,
}
})
},
getProps,
const getSelectionConfigRet = actionsRef.current.getSelectionConfig()
actionsRetHandler(getSelectionConfigRet, (config) => {
const getTablePropsRet = actionsRef.current.getTableProps()
actionsRetHandler(getTablePropsRet, ({ className = '', primaryKey: defaultPrimaryKey }) => {
if (config) {
const { mode, ids, primaryKey = defaultPrimaryKey, getProps, ...others } = config
actionsRef.current.setTableProps({ // 刷新
className: className.indexOf(hasRowSelectionCls) !== -1 ? className : `${className} ${hasRowSelectionCls}`,
rowSelection: {
...others,
mode,
selectedRowKeys: ids,
primaryKey,
onSelect: (selected, record, records) => {
actionsRef.current.notify(ListLifeCycleTypes.ON_LIST_SELECT, {
selected, record, records
})
},
onSelectAll: (selected, records) => {
actionsRef.current.notify(ListLifeCycleTypes.ON_LIST_SELECT_ALL, {
selected, records
})
},
onChange: (changeIds: string[], records: any[]) => {
actionsRef.current.setSelectionConfig({
ids: changeIds,
records,
})
actionsRef.current.notify(ListLifeCycleTypes.ON_LIST_SELECT_CHANGE, {
ids: changeIds, records
})
const getTablePropsRet = actionsRef.current.getTableProps()
actionsRetHandler(getTablePropsRet, ({ rowSelection }) => {
actionsRef.current.setTableProps({
rowSelection: {
...rowSelection,
selectedRowKeys: changeIds,
}
})
})
},
getProps,
}
})
} else {
actionsRef.current.setTableProps({ // 刷新
className: className.replace(` ${hasRowSelectionCls}`, ''),
rowSelection: undefined
})
}
})
} else {
actionsRef.current.setTableProps({ // 刷新
className: className.replace(` ${hasRowSelectionCls}`, ''),
rowSelection: undefined
})
}

});
}
})
const { effects } = props
Expand Down
12 changes: 12 additions & 0 deletions packages/next-components/src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,15 @@ export const setSelectionsByInstance = (instance, ids, records) => {
records
})
}

export function actionsRetHandler(ret, callback, asyncErrCallback = console.warn) {
if (ret instanceof Promise) {
ret.then((...t) => {
typeof callback === 'function' && callback(...t);
}).catch((err) => {
typeof asyncErrCallback === 'function' && asyncErrCallback(err)
});
} else {
callback(ret);
}
}