@sparklink-pro/apant
Version:
Apollo & Antd tools
32 lines • 1.44 kB
JavaScript
import { isArray, isFunction, isString, isObject } from 'lodash-es';
import { executeSearch, extractValue } from '../helpers/search';
import { defaultLabelProperty, useType } from './useType';
/**
* Extract object properties values from the given object and concatenate them.
*/
export const getObjectSearchValue = (object, properties) => properties.map((property) => extractValue(object, property)).join(' ');
/**
* Get a search callback for given type
*/
export const useTypeSearch = ({ type }) => {
const searchConfiguration = useType({ type }).getSearch();
const objectToSearchString = isFunction(searchConfiguration) ? searchConfiguration : false;
let properties = [defaultLabelProperty];
if (objectToSearchString === false) {
if (isString(searchConfiguration)) {
properties = [searchConfiguration];
}
else if (isArray(searchConfiguration)) {
properties = searchConfiguration;
}
else if (!isFunction(searchConfiguration) && isObject(searchConfiguration)) {
return searchConfiguration.custom;
}
}
return (object, search, context) => {
const searchValue = objectToSearchString ? objectToSearchString(object, context) : getObjectSearchValue(object, properties);
return searchValue ? executeSearch(search, searchValue) : false;
};
};
export default useTypeSearch;
//# sourceMappingURL=useTypeSearch.js.map