mirror of
				https://github.com/strapi/strapi.git
				synced 2025-11-04 03:43:34 +00:00 
			
		
		
		
	Expose custom fields in react context
This commit is contained in:
		
							parent
							
								
									49a8ab1f86
								
							
						
					
					
						commit
						eda74cb147
					
				@ -1,7 +1,7 @@
 | 
				
			|||||||
import React from 'react';
 | 
					import React from 'react';
 | 
				
			||||||
import PropTypes from 'prop-types';
 | 
					import PropTypes from 'prop-types';
 | 
				
			||||||
import { QueryClientProvider, QueryClient } from 'react-query';
 | 
					import { QueryClientProvider, QueryClient } from 'react-query';
 | 
				
			||||||
import { LibraryProvider, StrapiAppProvider } from '@strapi/helper-plugin';
 | 
					import { LibraryProvider, CustomFieldsProvider, StrapiAppProvider } from '@strapi/helper-plugin';
 | 
				
			||||||
import { Provider } from 'react-redux';
 | 
					import { Provider } from 'react-redux';
 | 
				
			||||||
import { AdminContext } from '../../contexts';
 | 
					import { AdminContext } from '../../contexts';
 | 
				
			||||||
import ConfigurationsProvider from '../ConfigurationsProvider';
 | 
					import ConfigurationsProvider from '../ConfigurationsProvider';
 | 
				
			||||||
@ -25,6 +25,7 @@ const Providers = ({
 | 
				
			|||||||
  authLogo,
 | 
					  authLogo,
 | 
				
			||||||
  children,
 | 
					  children,
 | 
				
			||||||
  components,
 | 
					  components,
 | 
				
			||||||
 | 
					  customFields,
 | 
				
			||||||
  fields,
 | 
					  fields,
 | 
				
			||||||
  getAdminInjectedComponents,
 | 
					  getAdminInjectedComponents,
 | 
				
			||||||
  getPlugin,
 | 
					  getPlugin,
 | 
				
			||||||
@ -64,15 +65,17 @@ const Providers = ({
 | 
				
			|||||||
                  settings={settings}
 | 
					                  settings={settings}
 | 
				
			||||||
                >
 | 
					                >
 | 
				
			||||||
                  <LibraryProvider components={components} fields={fields}>
 | 
					                  <LibraryProvider components={components} fields={fields}>
 | 
				
			||||||
                    <LanguageProvider messages={messages} localeNames={localeNames}>
 | 
					                    <CustomFieldsProvider customFields={customFields}>
 | 
				
			||||||
                      <AutoReloadOverlayBlockerProvider>
 | 
					                      <LanguageProvider messages={messages} localeNames={localeNames}>
 | 
				
			||||||
                        <OverlayBlocker>
 | 
					                        <AutoReloadOverlayBlockerProvider>
 | 
				
			||||||
                          <GuidedTour>
 | 
					                          <OverlayBlocker>
 | 
				
			||||||
                            <Notifications>{children}</Notifications>
 | 
					                            <GuidedTour>
 | 
				
			||||||
                          </GuidedTour>
 | 
					                              <Notifications>{children}</Notifications>
 | 
				
			||||||
                        </OverlayBlocker>
 | 
					                            </GuidedTour>
 | 
				
			||||||
                      </AutoReloadOverlayBlockerProvider>
 | 
					                          </OverlayBlocker>
 | 
				
			||||||
                    </LanguageProvider>
 | 
					                        </AutoReloadOverlayBlockerProvider>
 | 
				
			||||||
 | 
					                      </LanguageProvider>
 | 
				
			||||||
 | 
					                    </CustomFieldsProvider>
 | 
				
			||||||
                  </LibraryProvider>
 | 
					                  </LibraryProvider>
 | 
				
			||||||
                </StrapiAppProvider>
 | 
					                </StrapiAppProvider>
 | 
				
			||||||
              </ConfigurationsProvider>
 | 
					              </ConfigurationsProvider>
 | 
				
			||||||
@ -88,6 +91,7 @@ Providers.propTypes = {
 | 
				
			|||||||
  authLogo: PropTypes.oneOfType([PropTypes.string, PropTypes.any]).isRequired,
 | 
					  authLogo: PropTypes.oneOfType([PropTypes.string, PropTypes.any]).isRequired,
 | 
				
			||||||
  children: PropTypes.element.isRequired,
 | 
					  children: PropTypes.element.isRequired,
 | 
				
			||||||
  components: PropTypes.object.isRequired,
 | 
					  components: PropTypes.object.isRequired,
 | 
				
			||||||
 | 
					  customFields: PropTypes.object.isRequired,
 | 
				
			||||||
  fields: PropTypes.object.isRequired,
 | 
					  fields: PropTypes.object.isRequired,
 | 
				
			||||||
  getAdminInjectedComponents: PropTypes.func.isRequired,
 | 
					  getAdminInjectedComponents: PropTypes.func.isRequired,
 | 
				
			||||||
  getPlugin: PropTypes.func.isRequired,
 | 
					  getPlugin: PropTypes.func.isRequired,
 | 
				
			||||||
 | 
				
			|||||||
@ -10,6 +10,7 @@ import {
 | 
				
			|||||||
  LoadingIndicatorPage,
 | 
					  LoadingIndicatorPage,
 | 
				
			||||||
  useNotification,
 | 
					  useNotification,
 | 
				
			||||||
  useAppInfos,
 | 
					  useAppInfos,
 | 
				
			||||||
 | 
					  useCustomFields,
 | 
				
			||||||
} from '@strapi/helper-plugin';
 | 
					} from '@strapi/helper-plugin';
 | 
				
			||||||
import { Layout, ContentLayout } from '@strapi/design-system/Layout';
 | 
					import { Layout, ContentLayout } from '@strapi/design-system/Layout';
 | 
				
			||||||
import { Main } from '@strapi/design-system/Main';
 | 
					import { Main } from '@strapi/design-system/Main';
 | 
				
			||||||
@ -55,6 +56,9 @@ const MarketPlacePage = () => {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  useFocusWhenNavigate();
 | 
					  useFocusWhenNavigate();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  const { customFields } = useCustomFields();
 | 
				
			||||||
 | 
					  console.log('customFields', customFields);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const marketplaceTitle = formatMessage({
 | 
					  const marketplaceTitle = formatMessage({
 | 
				
			||||||
    id: 'global.marketplace',
 | 
					    id: 'global.marketplace',
 | 
				
			||||||
    defaultMessage: 'Marketplace',
 | 
					    defaultMessage: 'Marketplace',
 | 
				
			||||||
 | 
				
			|||||||
@ -0,0 +1,11 @@
 | 
				
			|||||||
 | 
					/**
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * CustomFieldsContext
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import { createContext } from 'react';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const CustomFieldsContext = createContext();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export default CustomFieldsContext;
 | 
				
			||||||
@ -0,0 +1,18 @@
 | 
				
			|||||||
 | 
					/**
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * useCustomFields
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import { useContext, useRef } from 'react';
 | 
				
			||||||
 | 
					import CustomFieldsContext from '../../contexts/CustomFieldsContext';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const useCustomFields = () => {
 | 
				
			||||||
 | 
					  const { customFields } = useContext(CustomFieldsContext);
 | 
				
			||||||
 | 
					  // Use a ref so we can safely add the custom fields to a hook dependencies array
 | 
				
			||||||
 | 
					  const customFieldsRef = useRef(customFields);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  return { customFields: customFieldsRef.current };
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export default useCustomFields;
 | 
				
			||||||
@ -15,6 +15,7 @@ export { default as useAppInfos } from './hooks/useAppInfos';
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
export { default as useQuery } from './hooks/useQuery';
 | 
					export { default as useQuery } from './hooks/useQuery';
 | 
				
			||||||
export { default as useLibrary } from './hooks/useLibrary';
 | 
					export { default as useLibrary } from './hooks/useLibrary';
 | 
				
			||||||
 | 
					export { default as useCustomFields } from './hooks/useCustomFields';
 | 
				
			||||||
export { default as useNotification } from './hooks/useNotification';
 | 
					export { default as useNotification } from './hooks/useNotification';
 | 
				
			||||||
export { default as useStrapiApp } from './hooks/useStrapiApp';
 | 
					export { default as useStrapiApp } from './hooks/useStrapiApp';
 | 
				
			||||||
export { default as useTracking } from './hooks/useTracking';
 | 
					export { default as useTracking } from './hooks/useTracking';
 | 
				
			||||||
@ -32,6 +33,7 @@ export { default as useLockScroll } from './hooks/useLockScroll';
 | 
				
			|||||||
// Providers
 | 
					// Providers
 | 
				
			||||||
export { default as GuidedTourProvider } from './providers/GuidedTourProvider';
 | 
					export { default as GuidedTourProvider } from './providers/GuidedTourProvider';
 | 
				
			||||||
export { default as LibraryProvider } from './providers/LibraryProvider';
 | 
					export { default as LibraryProvider } from './providers/LibraryProvider';
 | 
				
			||||||
 | 
					export { default as CustomFieldsProvider } from './providers/CustomFieldsProvider';
 | 
				
			||||||
export { default as NotificationsProvider } from './providers/NotificationsProvider';
 | 
					export { default as NotificationsProvider } from './providers/NotificationsProvider';
 | 
				
			||||||
export { default as StrapiAppProvider } from './providers/StrapiAppProvider';
 | 
					export { default as StrapiAppProvider } from './providers/StrapiAppProvider';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -0,0 +1,21 @@
 | 
				
			|||||||
 | 
					/**
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * CustomFieldsProvider
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					import React from 'react';
 | 
				
			||||||
 | 
					import PropTypes from 'prop-types';
 | 
				
			||||||
 | 
					import CustomFieldsContext from '../../contexts/CustomFieldsContext';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const CustomFieldsProvider = ({ children, customFields }) => {
 | 
				
			||||||
 | 
					  return (
 | 
				
			||||||
 | 
					    <CustomFieldsContext.Provider value={{ customFields }}>{children}</CustomFieldsContext.Provider>
 | 
				
			||||||
 | 
					  );
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					CustomFieldsProvider.propTypes = {
 | 
				
			||||||
 | 
					  children: PropTypes.node.isRequired,
 | 
				
			||||||
 | 
					  customFields: PropTypes.object.isRequired,
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export default CustomFieldsProvider;
 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user