import { computed, ComputedRef, reactive } from 'vue' const useSelection = ({ type = 'checkbox' }): Required<{ option: Required<{ rows: unknown[] keys: string[] }> count: ComputedRef rowSelection: object exposeMethod: object }> => { /** * 批量操作的数据缓存 */ const option = reactive<{ rows: unknown[] keys: string[] }>({ rows: [], keys: [] }) /** * 批量操作的事件 */ const rowSelection = { type, onChange: (selectedRowKeys: (string | number)[], selectedRows: unknown[]) => { if (option.keys.length > 0) { option.keys.splice(0, option.keys.length) } if (option.rows.length > 0) { option.rows.splice(0, option.rows.length) } option.keys.push(...(selectedRowKeys as string[])) option.rows.push(...selectedRows) } } /** * 选中内容的数量 */ const count = computed(() => option.keys.length || 0) const exposeMethod = { /** * 选中的源数据列表 */ selectRecordList: option.rows, /** * 选中的源数据rowKey列表 */ selectRecordKeyList: option.keys } return { option, count, rowSelection, exposeMethod } } export default useSelection