pass entity reference in autocomplete widget (#14423)

This commit is contained in:
karanh37 2024-01-02 13:41:57 +05:30 committed by GitHub
parent 78bd8f98b5
commit d3a75652cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 64 additions and 24 deletions

View File

@ -21,6 +21,13 @@ jest.mock('../../rest/searchAPI');
jest.mock('../../utils/TableUtils'); jest.mock('../../utils/TableUtils');
jest.mock('../../utils/EntityUtils', () => ({ jest.mock('../../utils/EntityUtils', () => ({
getEntityName: jest.fn().mockReturnValue('Test'), getEntityName: jest.fn().mockReturnValue('Test'),
getEntityReferenceFromEntity: jest
.fn()
.mockImplementation((entity, type) => ({
...entity,
entityType: undefined,
type: type,
})),
})); }));
jest.mock('../common/ProfilePicture/ProfilePicture', () => { jest.mock('../common/ProfilePicture/ProfilePicture', () => {
return jest return jest
@ -68,6 +75,9 @@ const mockSearchAPIResponse = {
name: 'test 1', name: 'test 1',
fullyQualifiedName: 'test-1', fullyQualifiedName: 'test-1',
entityType: 'table', entityType: 'table',
href: '',
description: '',
deleted: false,
}, },
}, },
{ {
@ -76,6 +86,9 @@ const mockSearchAPIResponse = {
name: 'test 2', name: 'test 2',
fullyQualifiedName: 'test-2', fullyQualifiedName: 'test-2',
entityType: 'table', entityType: 'table',
href: '',
description: '',
deleted: false,
}, },
}, },
], ],
@ -135,7 +148,10 @@ describe('DataAssetAsyncSelectList', () => {
}); });
const { container } = render( const { container } = render(
<DataAssetAsyncSelectList searchIndex={SearchIndex.USER} /> <DataAssetAsyncSelectList
mode="multiple"
searchIndex={SearchIndex.USER}
/>
); );
await act(async () => { await act(async () => {
@ -153,8 +169,16 @@ describe('DataAssetAsyncSelectList', () => {
{ {
displayName: 'Test', displayName: 'Test',
label: 'Test', label: 'Test',
reference: { id: '1', type: 'table' }, reference: {
value: '1', id: '1',
type: 'table',
name: 'test 1',
deleted: false,
description: '',
fullyQualifiedName: 'test-1',
href: '',
},
value: 'test-1',
}, },
]; ];
@ -180,7 +204,7 @@ describe('DataAssetAsyncSelectList', () => {
}); });
}); });
const option = screen.getByTestId('option-1'); const option = screen.getByTestId('option-test-1');
await act(async () => { await act(async () => {
fireEvent.click(option); fireEvent.click(option);

View File

@ -16,10 +16,15 @@ import { debounce } from 'lodash';
import React, { FC, useCallback, useMemo, useRef, useState } from 'react'; import React, { FC, useCallback, useMemo, useRef, useState } from 'react';
import Loader from '../../components/Loader/Loader'; import Loader from '../../components/Loader/Loader';
import { PAGE_SIZE } from '../../constants/constants'; import { PAGE_SIZE } from '../../constants/constants';
import { EntityType } from '../../enums/entity.enum';
import { SearchIndex } from '../../enums/search.enum'; import { SearchIndex } from '../../enums/search.enum';
import { EntityReference } from '../../generated/entity/type';
import { usePaging } from '../../hooks/paging/usePaging'; import { usePaging } from '../../hooks/paging/usePaging';
import { searchQuery } from '../../rest/searchAPI'; import { searchQuery } from '../../rest/searchAPI';
import { getEntityName } from '../../utils/EntityUtils'; import {
getEntityName,
getEntityReferenceFromEntity,
} from '../../utils/EntityUtils';
import { getEntityIcon } from '../../utils/TableUtils'; import { getEntityIcon } from '../../utils/TableUtils';
import { showErrorToast } from '../../utils/ToastUtils'; import { showErrorToast } from '../../utils/ToastUtils';
import ProfilePicture from '../common/ProfilePicture/ProfilePicture'; import ProfilePicture from '../common/ProfilePicture/ProfilePicture';
@ -70,13 +75,16 @@ const DataAssetAsyncSelectList: FC<DataAssetAsyncSelectListProps> = ({
const dataAssets = hits.map(({ _source }) => { const dataAssets = hits.map(({ _source }) => {
const entityName = getEntityName(_source); const entityName = getEntityName(_source);
const entityRef = getEntityReferenceFromEntity(
_source as EntityReference,
_source.entityType as EntityType
);
return { return {
label: entityName, label: entityName,
value: _source.id, value: _source.fullyQualifiedName,
reference: { reference: {
id: _source.id ?? '', ...entityRef,
type: _source.entityType,
}, },
displayName: entityName, displayName: entityName,
name: _source.name, name: _source.name,
@ -184,19 +192,23 @@ const DataAssetAsyncSelectList: FC<DataAssetAsyncSelectListProps> = ({
); );
const handleChange: SelectProps['onChange'] = (values: string[], options) => { const handleChange: SelectProps['onChange'] = (values: string[], options) => {
const selectedOptions = (options as DataAssetOption[]).reduce( if (mode) {
(acc, option) => { const selectedOptions = (options as DataAssetOption[]).reduce(
if (values.includes(option.value as string)) { (acc, option) => {
acc.push({ ...option, label: option.displayName }); if (values.includes(option.value as string)) {
} acc.push({ ...option, label: option.displayName });
}
return acc; return acc;
}, },
[] as DataAssetOption[] [] as DataAssetOption[]
); );
selectedDataAssetsRef.current = selectedOptions; selectedDataAssetsRef.current = selectedOptions;
onChange?.(selectedOptions); onChange?.(selectedOptions);
} else {
onChange?.(options as DataAssetOption);
}
}; };
return ( return (

View File

@ -16,16 +16,20 @@ import { SearchIndex } from '../../enums/search.enum';
import DataAssetAsyncSelectList from '../DataAssetAsyncSelectList/DataAssetAsyncSelectList'; import DataAssetAsyncSelectList from '../DataAssetAsyncSelectList/DataAssetAsyncSelectList';
import { DataAssetOption } from '../DataAssetAsyncSelectList/DataAssetAsyncSelectList.interface'; import { DataAssetOption } from '../DataAssetAsyncSelectList/DataAssetAsyncSelectList.interface';
const AsyncSelectWidget = ({ onChange, schema }: WidgetProps) => { const AsyncSelectWidget = ({ onChange, schema, ...props }: WidgetProps) => {
const handleChange = (value: DataAssetOption | DataAssetOption[]) => { const handleChange = (value: DataAssetOption | DataAssetOption[]) => {
const data = value.map((item: DataAssetOption) => item.reference); if (Array.isArray(value)) {
onChange(data); const data = value.map((item: DataAssetOption) => item.reference);
onChange(data);
} else {
const data = value.reference;
onChange(data);
}
}; };
return ( return (
<DataAssetAsyncSelectList <DataAssetAsyncSelectList
defaultValue={schema.value} defaultValue={props.value.fullyQualifiedName ?? ''}
mode="multiple"
placeholder={schema.placeholder ?? ''} placeholder={schema.placeholder ?? ''}
searchIndex={schema?.autoCompleteType ?? SearchIndex.TABLE} searchIndex={schema?.autoCompleteType ?? SearchIndex.TABLE}
onChange={handleChange} onChange={handleChange}