From 47c1764ac3cd58d3a1b4b012ba7f1b75d1c36a6f Mon Sep 17 00:00:00 2001 From: lyf-coder <58352715+lyf-coder@users.noreply.github.com> Date: Tue, 11 Oct 2022 22:43:27 +0800 Subject: [PATCH] fix(client/record-picker): support record-picker show format DataPicker (#888) * fix(client/record-picker): support record-picker show format DataPicker * fix(client/record-picker): undefined judgment and when change field's label refresh format in time (cherry picked from commit 381e71b1f72a8dd092c9e6ad0c36926102b8a7e2) --- .../antd/record-picker/InputRecordPicker.tsx | 24 +++++++++-------- .../record-picker/ReadPrettyRecordPicker.tsx | 9 +++++-- .../antd/record-picker/util.ts | 27 +++++++++++++++++++ 3 files changed, 47 insertions(+), 13 deletions(-) create mode 100644 packages/core/client/src/schema-component/antd/record-picker/util.ts diff --git a/packages/core/client/src/schema-component/antd/record-picker/InputRecordPicker.tsx b/packages/core/client/src/schema-component/antd/record-picker/InputRecordPicker.tsx index b6046802d..320f8353a 100644 --- a/packages/core/client/src/schema-component/antd/record-picker/InputRecordPicker.tsx +++ b/packages/core/client/src/schema-component/antd/record-picker/InputRecordPicker.tsx @@ -9,6 +9,7 @@ import { FormProvider, SchemaComponentOptions } from '../../core'; import { useCompile } from '../../hooks'; import { ActionContext, useActionContext } from '../action'; import { useFieldNames } from './useFieldNames'; +import { getLabelFormatValue, useLabelUiSchema } from './util'; const RecordPickerContext = createContext(null); @@ -23,15 +24,15 @@ const useTableSelectorProps = () => { rowSelection: { type: multiple ? 'checkbox' : 'radio', // defaultSelectedRowKeys: rcSelectRows?.map((item) => item[rowKey||'id']), - selectedRowKeys: rcSelectRows?.map((item) => item[rowKey||'id']), + selectedRowKeys: rcSelectRows?.map((item) => item[rowKey || 'id']), }, onRowSelectionChange(selectedRowKeys, 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']) + 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 { @@ -74,30 +75,31 @@ export const InputRecordPicker: React.FC = (props) => { const fieldSchema = useFieldSchema(); const collectionField = useAssociation(props); const compile = useCompile(); - + const labelUiSchema = useLabelUiSchema(collectionField, fieldNames?.label || 'label'); + const [selectedRows, setSelectedRows] = useState([]); const [options, setOptions] = useState([]); useEffect(() => { if (value) { - const opts = (Array.isArray(value) ? value : value ? [value] : []).map(option => { + const opts = (Array.isArray(value) ? value : value ? [value] : []).map((option) => { const label = option[fieldNames.label]; return { ...option, - [fieldNames.label]: compile(label), + [fieldNames.label]: getLabelFormatValue(labelUiSchema, compile(label)), }; }); setOptions(opts); setSelectedRows(opts); } - }, [value]) + }, [value,fieldNames?.label]); const getValue = () => { if (multiple == null) return null; // console.log('getValue', multiple, value, Array.isArray(value)); - - return Array.isArray(value) ? value?.map(v => v[fieldNames.value]) : value?.[fieldNames.value]; - } + + return Array.isArray(value) ? value?.map((v) => v[fieldNames.value]) : value?.[fieldNames.value]; + }; return (