UNPKG

ra-core

Version:

Core components of react-admin, a frontend Framework for building admin applications on top of REST services, using ES6, React

43 lines 2.09 kB
import * as React from 'react'; import { isValidElement, useCallback } from 'react'; import get from 'lodash/get.js'; import { useTranslate } from "../../i18n/index.js"; import { RecordContextProvider } from "../../controller/index.js"; /* * Returns helper functions for choices handling. * * @param optionText Either a string defining the property to use to get the choice text, a function or a React element * @param optionValue The property to use to get the choice value * @param translateChoice A boolean indicating whether to option text should be translated * * @returns An object with helper functions: * - getChoiceText: Returns the choice text or a React element * - getChoiceValue: Returns the choice value */ export const useChoices = ({ optionText = 'name', optionValue = 'id', disableValue = 'disabled', translateChoice = true, createValue = '@@ra-create', createHintValue = '@@ra-create-hint', }) => { const translate = useTranslate(); const getChoiceText = useCallback(choice => { if (choice?.id === createValue || choice?.id === createHintValue) { return get(choice, typeof optionText === 'string' ? optionText : 'name'); } if (isValidElement(optionText)) { return (React.createElement(RecordContextProvider, { value: choice }, optionText)); } const choiceName = typeof optionText === 'function' ? optionText(choice) : get(choice, optionText); return isValidElement(choiceName) ? choiceName : translateChoice ? translate(String(choiceName), { _: choiceName }) : String(choiceName); }, [createHintValue, createValue, optionText, translate, translateChoice]); const getChoiceValue = useCallback(choice => get(choice, optionValue, get(choice, 'id')), [optionValue]); const getDisableValue = useCallback(choice => get(choice, disableValue), [disableValue]); return { getChoiceText, getChoiceValue, getDisableValue, }; }; //# sourceMappingURL=useChoices.js.map