UNPKG

ra-core

Version:

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

41 lines 1.52 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useOptionalSourceContext = exports.useSourceContext = exports.SourceContextProvider = exports.SourceContext = void 0; const react_1 = require("react"); /** * Context that provides a function that accept a source and return getters for the modified source and label. * * This allows some special inputs to prefix or suffix the source of their children. * * @example * const sourceContext = { * getSource: source => `coordinates.${source}`, * getLabel: source => `resources.posts.fields.${source}`, * } * const CoordinatesInput = () => { * return ( * <SourceContextProvider value={sourceContext}> * <TextInput source="lat" /> * <TextInput source="lng" /> * </SourceContextProvider> * ); * }; */ exports.SourceContext = (0, react_1.createContext)(undefined); exports.SourceContext.displayName = 'SourceContext'; const defaultContextValue = { getSource: (source) => source, getLabel: (source) => source, }; exports.SourceContextProvider = exports.SourceContext.Provider; const useSourceContext = () => { const context = (0, react_1.useContext)(exports.SourceContext); if (!context) { return defaultContextValue; } return context; }; exports.useSourceContext = useSourceContext; const useOptionalSourceContext = () => (0, react_1.useContext)(exports.SourceContext); exports.useOptionalSourceContext = useOptionalSourceContext; //# sourceMappingURL=SourceContext.js.map