ra-core
Version:
Core components of react-admin, a frontend Framework for building admin applications on top of REST services, using ES6, React
36 lines • 1.19 kB
JavaScript
import { createContext, useContext } from '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>
* );
* };
*/
export const SourceContext = createContext(undefined);
SourceContext.displayName = 'SourceContext';
const defaultContextValue = {
getSource: (source) => source,
getLabel: (source) => source,
};
export const SourceContextProvider = SourceContext.Provider;
export const useSourceContext = () => {
const context = useContext(SourceContext);
if (!context) {
return defaultContextValue;
}
return context;
};
export const useOptionalSourceContext = () => useContext(SourceContext);
//# sourceMappingURL=SourceContext.js.map