-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9533 from marmelab/source-context
Introduce SourceContext
- Loading branch information
Showing
33 changed files
with
595 additions
and
334 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { createContext, useContext } from 'react'; | ||
|
||
export type SourceContextValue = { | ||
/* | ||
* Returns the source for a field or input, modified according to the context. | ||
*/ | ||
getSource: (source: string) => string; | ||
/* | ||
* Returns the label for a field or input, modified according to the context. Returns a translation key. | ||
*/ | ||
getLabel: (source: string) => string; | ||
}; | ||
|
||
/** | ||
* Context that provides a function that accept a source and return a modified source (prefixed, suffixed, etc.) for fields and inputs. | ||
* | ||
* @example | ||
* const sourceContext = { | ||
* getSource: source => `coordinates.${source}`, | ||
* getLabel: source => `resources.posts.fields.${source}`, | ||
* } | ||
* const CoordinatesInput = () => { | ||
* return ( | ||
* <SouceContextProvider value={sourceContext}> | ||
* <TextInput source="lat" /> | ||
* <TextInput source="lng" /> | ||
* </SouceContextProvider> | ||
* ); | ||
* }; | ||
*/ | ||
export const SourceContext = createContext<SourceContextValue>(null); | ||
|
||
export const SourceContextProvider = SourceContext.Provider; | ||
|
||
export const useSourceContext = () => useContext(SourceContext); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { useSourceContext } from './SourceContext'; | ||
|
||
/** | ||
* Get the source prop for a field or input by checking if a source context is available. | ||
* @param {string} source The original source prop | ||
* @returns {string} The source prop, either the original one or the one modified by the SourceContext. | ||
* @example | ||
* const MyInput = ({ source, ...props }) => { | ||
* const finalSource = useWrappedSource(source); | ||
* return <input name={finalSource} {...props} />; | ||
* }; | ||
*/ | ||
export const useWrappedSource = (source: string) => { | ||
const sourceContext = useSourceContext(); | ||
return sourceContext?.getSource(source) ?? source; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.