refactor: remoteSelect fieldName value (#2457)

* refactor: remoteSelect fieldName value

* refactor: code improve
This commit is contained in:
katherinehhh 2023-08-15 20:17:34 +08:00 committed by GitHub
parent 16ed7f2916
commit d8d01befdc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,14 +1,12 @@
import { LoadingOutlined } from '@ant-design/icons'; import { LoadingOutlined } from '@ant-design/icons';
import { connect, mapProps, mapReadPretty, useField, useFieldSchema, useForm } from '@formily/react'; import { connect, mapProps, mapReadPretty, useFieldSchema } from '@formily/react';
import dayjs from 'dayjs'; import dayjs from 'dayjs';
import { Divider, SelectProps, Tag } from 'antd'; import { Divider, SelectProps, Tag } from 'antd';
import flat from 'flat';
import _, { uniqBy } from 'lodash'; import _, { uniqBy } from 'lodash';
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'; import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { ResourceActionOptions, useRequest } from '../../../api-client'; import { ResourceActionOptions, useRequest } from '../../../api-client';
import { mergeFilter } from '../../../block-provider/SharedFilterProvider'; import { mergeFilter } from '../../../block-provider/SharedFilterProvider';
import { useCollection, useCollectionManager } from '../../../collection-manager'; import { useCollection, useCollectionManager } from '../../../collection-manager';
import { getInnermostKeyAndValue } from '../../common/utils/uitls';
import { useCompile } from '../../hooks'; import { useCompile } from '../../hooks';
import { Select, defaultFieldNames } from '../select'; import { Select, defaultFieldNames } from '../select';
import { ReadPretty } from './ReadPretty'; import { ReadPretty } from './ReadPretty';
@ -42,11 +40,9 @@ const InternalRemoteSelect = connect(
...others ...others
} = props; } = props;
const [open, setOpen] = useState(false); const [open, setOpen] = useState(false);
const form = useForm();
const firstRun = useRef(false); const firstRun = useRef(false);
const fieldSchema = useFieldSchema(); const fieldSchema = useFieldSchema();
const isQuickAdd = fieldSchema['x-component-props']?.addMode === 'quickAdd'; const isQuickAdd = fieldSchema['x-component-props']?.addMode === 'quickAdd';
const field = useField();
const { getField } = useCollection(); const { getField } = useCollection();
const searchData = useRef(null); const searchData = useRef(null);
const { getCollectionJoinField, getInterface } = useCollectionManager(); const { getCollectionJoinField, getInterface } = useCollectionManager();
@ -69,6 +65,7 @@ const InternalRemoteSelect = connect(
(options) => { (options) => {
try { try {
return options return options
.filter((v) => ['number', 'string'].includes(typeof v[fieldNames.value]))
.map((option) => { .map((option) => {
let label = compile(option[fieldNames.label]); let label = compile(option[fieldNames.label]);
@ -95,7 +92,6 @@ const InternalRemoteSelect = connect(
} }
} }
} }
if (targetField?.type === 'date') { if (targetField?.type === 'date') {
label = dayjs(label).format('YYYY-MM-DD'); label = dayjs(label).format('YYYY-MM-DD');
} }
@ -190,7 +186,6 @@ const InternalRemoteSelect = connect(
(v != null && (Array.isArray(v) ? v : [{ ...v, [fieldNames.value]: v[fieldNames.value] || v }])) || []; (v != null && (Array.isArray(v) ? v : [{ ...v, [fieldNames.value]: v[fieldNames.value] || v }])) || [];
return uniqBy(data?.data?.concat(valueOptions) || [], fieldNames.value); return uniqBy(data?.data?.concat(valueOptions) || [], fieldNames.value);
}, [value, defaultValue, data?.data, fieldNames.value]); }, [value, defaultValue, data?.data, fieldNames.value]);
const onDropdownVisibleChange = (visible) => { const onDropdownVisibleChange = (visible) => {
setOpen(visible); setOpen(visible);
searchData.current = null; searchData.current = null;
@ -199,7 +194,6 @@ const InternalRemoteSelect = connect(
} }
firstRun.current = true; firstRun.current = true;
}; };
return ( return (
<Select <Select
open={open} open={open}
@ -257,7 +251,7 @@ const InternalRemoteSelect = connect(
mapReadPretty(ReadPretty), mapReadPretty(ReadPretty),
); );
export const RemoteSelect = (InternalRemoteSelect as unknown) as typeof InternalRemoteSelect & { export const RemoteSelect = InternalRemoteSelect as unknown as typeof InternalRemoteSelect & {
ReadPretty: typeof ReadPretty; ReadPretty: typeof ReadPretty;
}; };