fix: record picker cannot select from different pages (#623)

related issue #563
This commit is contained in:
金昶 2022-07-15 11:23:54 +08:00 committed by GitHub
parent c51c6c097f
commit a521231a25
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,4 +1,5 @@
import { RecursionField, useFieldSchema } from '@formily/react';
import { ArrayField } from '@formily/core';
import { RecursionField, useField, useFieldSchema } from '@formily/react';
import { Select } from 'antd';
import React, { createContext, useContext, useEffect, useState } from 'react';
import { useTableSelectorProps as useTsp } from '../../../block-provider/TableSelectorProvider';
@ -7,23 +8,36 @@ import { FormProvider, SchemaComponentOptions } from '../../core';
import { useCompile } from '../../hooks';
import { ActionContext, useActionContext } from '../action';
import { useFieldNames } from './useFieldNames';
import { differenceBy, unionBy } from 'lodash';
const RecordPickerContext = createContext(null);
const useTableSelectorProps = () => {
const { multiple, value, setSelectedRows, selectedRows } = useContext(RecordPickerContext);
const field = useField<ArrayField>();
const { multiple, value, setSelectedRows, selectedRows: rcSelectRows } = useContext(RecordPickerContext);
const { onRowSelectionChange, rowKey, ...others } = useTsp();
console.log('useTableSelectorProps', field.value, value);
return {
...others,
rowKey,
rowSelection: {
type: multiple ? 'checkbox' : 'radio',
defaultSelectedRowKeys: selectedRows?.map((item) => item[rowKey||'id']),
selectedRowKeys: selectedRows?.map((item) => item[rowKey||'id']),
// defaultSelectedRowKeys: selectedRows?.map((item) => item[rowKey||'id']),
selectedRowKeys: rcSelectRows?.map((item) => item[rowKey||'id']),
},
onRowSelectionChange(selectedRowKeys, selectedRows) {
onRowSelectionChange?.(selectedRowKeys, selectedRows);
setSelectedRows?.(selectedRows);
if (multiple) {
const scopeRows = field.value || [];
const allSelectedRows = rcSelectRows || [];
const otherRows = differenceBy(allSelectedRows, scopeRows, rowKey||'id');
const unionSelectedRows = unionBy(otherRows, selectedRows, rowKey||'id');
const unionSelectedRowKeys = unionSelectedRows.map((item) => item[rowKey||'id'])
setSelectedRows?.(unionSelectedRows);
onRowSelectionChange?.(unionSelectedRowKeys, unionSelectedRows);
} else {
setSelectedRows?.(selectedRows);
onRowSelectionChange?.(selectedRowKeys, selectedRows);
}
},
};
};
@ -78,8 +92,6 @@ export const InputRecordPicker: React.FC<any> = (props) => {
}
}, [value])
useEffect(() => {})
const getValue = () => {
if (multiple == null) return null;