ra-core
Version:
Core components of react-admin, a frontend Framework for building admin applications on top of REST services, using ES6, React
83 lines • 3.91 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useChoices = void 0;
const React = __importStar(require("react"));
const react_1 = require("react");
const get_js_1 = __importDefault(require("lodash/get.js"));
const i18n_1 = require("../../i18n/index.cjs");
const controller_1 = require("../../controller/index.cjs");
/*
* 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
*/
const useChoices = ({ optionText = 'name', optionValue = 'id', disableValue = 'disabled', translateChoice = true, createValue = '@@ra-create', createHintValue = '@@ra-create-hint', }) => {
const translate = (0, i18n_1.useTranslate)();
const getChoiceText = (0, react_1.useCallback)(choice => {
if (choice?.id === createValue || choice?.id === createHintValue) {
return (0, get_js_1.default)(choice, typeof optionText === 'string' ? optionText : 'name');
}
if ((0, react_1.isValidElement)(optionText)) {
return (React.createElement(controller_1.RecordContextProvider, { value: choice }, optionText));
}
const choiceName = typeof optionText === 'function'
? optionText(choice)
: (0, get_js_1.default)(choice, optionText);
return (0, react_1.isValidElement)(choiceName)
? choiceName
: translateChoice
? translate(String(choiceName), { _: choiceName })
: String(choiceName);
}, [createHintValue, createValue, optionText, translate, translateChoice]);
const getChoiceValue = (0, react_1.useCallback)(choice => (0, get_js_1.default)(choice, optionValue, (0, get_js_1.default)(choice, 'id')), [optionValue]);
const getDisableValue = (0, react_1.useCallback)(choice => (0, get_js_1.default)(choice, disableValue), [disableValue]);
return {
getChoiceText,
getChoiceValue,
getDisableValue,
};
};
exports.useChoices = useChoices;
//# sourceMappingURL=useChoices.js.map