UNPKG

@sparklink-pro/apant

Version:

Apollo & Antd tools

36 lines 1.41 kB
import { get, isFunction, isString } from 'lodash-es'; import { useRegistry } from './useRegistry'; export const defaultLabelProperty = '_name'; /** * This hook is used to get the type and all its configuration/options ?? * It can be used to get the label, the search keywords, * Or for example the selectOptions if used as a select in a form. */ export function useType({ type }) { const registry = useRegistry(); const configuration = registry.getType(type); const { label, order, admin, forms, search, id: idProperty } = configuration; const getId = (object) => get(object, idProperty); return { getLabel: (object) => { if (isFunction(label)) { return label(object); } return isString(label) ? object[label] : `${type}#${getId(object)}`; }, getOrder: () => order || defaultLabelProperty, getId, getForm: (name) => (forms ? forms[name] : undefined), getAdmin: () => admin, getSearch: () => search, getSelectOptions: () => { var _a, _b; return ({ label: (_a = configuration.select) === null || _a === void 0 ? void 0 : _a.label, fragment: (_b = configuration.select) === null || _b === void 0 ? void 0 : _b.fragment, }); }, }; } export default useType; //# sourceMappingURL=useType.js.map