From ff97cc324512deb47a859cab6611bffadecddb4b Mon Sep 17 00:00:00 2001 From: Martin Bohal Date: Fri, 20 Dec 2024 07:02:30 +0100 Subject: [PATCH 1/3] Split `RUIProvider` into two This commit splits `RUIProvider` into `TranslationsProvider` and `GlobalPropsProvider`. The reasons fo this change are: * Improve performance by reducing rerenders in cases when the context value change dynamically. * Make code more readable by splitting isolated functionality into isolated prividers. --- src/components/Alert/Alert.jsx | 8 +- src/components/Badge/Badge.jsx | 2 +- src/components/Button/Button.jsx | 2 +- src/components/ButtonGroup/ButtonGroup.jsx | 2 +- src/components/Card/Card.jsx | 2 +- src/components/Card/CardBody.jsx | 2 +- src/components/Card/CardFooter.jsx | 2 +- .../CheckboxField/CheckboxField.jsx | 2 +- .../FileInputField/FileInputField.jsx | 2 +- src/components/FormLayout/FormLayout.jsx | 2 +- .../FormLayout/FormLayoutCustomField.jsx | 2 +- src/components/Grid/Grid.jsx | 2 +- src/components/Grid/GridSpan.jsx | 2 +- src/components/InputGroup/InputGroup.jsx | 4 +- src/components/Modal/Modal.jsx | 2 +- src/components/Modal/ModalBody.jsx | 2 +- src/components/Modal/ModalCloseButton.jsx | 8 +- src/components/Modal/ModalContent.jsx | 2 +- src/components/Modal/ModalFooter.jsx | 2 +- src/components/Modal/ModalHeader.jsx | 2 +- src/components/Modal/ModalTitle.jsx | 2 +- src/components/Modal/README.md | 36 ++-- src/components/Paper/Paper.jsx | 2 +- src/components/Popover/Popover.jsx | 2 +- src/components/Popover/PopoverWrapper.jsx | 2 +- src/components/Radio/Radio.jsx | 2 +- src/components/ScrollView/ScrollView.jsx | 8 +- src/components/SelectField/SelectField.jsx | 2 +- src/components/Table/Table.jsx | 2 +- src/components/Tabs/Tabs.jsx | 2 +- src/components/Tabs/TabsItem.jsx | 2 +- src/components/Text/Text.jsx | 2 +- src/components/TextArea/TextArea.jsx | 2 +- src/components/TextField/TextField.jsx | 2 +- src/components/TextLink/TextLink.jsx | 2 +- src/components/Toggle/Toggle.jsx | 2 +- src/components/Toolbar/Toolbar.jsx | 2 +- src/components/Toolbar/ToolbarGroup.jsx | 2 +- src/components/Toolbar/ToolbarItem.jsx | 2 +- src/docs/customize/global-props.md | 159 ++++++------------ src/docs/customize/translations.md | 62 ++++--- src/index.js | 3 +- src/provider/RUIContext.jsx | 9 - src/provider/RUIProvider.jsx | 42 ----- src/provider/index.js | 3 - .../globalProps/GlobalPropsContext.jsx | 5 + .../globalProps/GlobalPropsProvider.jsx | 33 ++++ .../__tests__/GlobalPropsProvider.test.jsx} | 54 +++--- src/providers/globalProps/index.js | 3 + .../globalProps}/withGlobalProps.jsx | 32 ++-- .../translations/TranslationsContext.jsx | 6 + .../translations/TranslationsProvider.jsx | 34 ++++ .../__tests__/TranslationsProvider.test.jsx | 30 ++++ src/providers/translations/index.js | 2 + 54 files changed, 310 insertions(+), 299 deletions(-) delete mode 100644 src/provider/RUIContext.jsx delete mode 100644 src/provider/RUIProvider.jsx delete mode 100644 src/provider/index.js create mode 100644 src/providers/globalProps/GlobalPropsContext.jsx create mode 100644 src/providers/globalProps/GlobalPropsProvider.jsx rename src/{provider/__tests__/RUIProvider.test.jsx => providers/globalProps/__tests__/GlobalPropsProvider.test.jsx} (70%) create mode 100644 src/providers/globalProps/index.js rename src/{provider => providers/globalProps}/withGlobalProps.jsx (58%) create mode 100644 src/providers/translations/TranslationsContext.jsx create mode 100644 src/providers/translations/TranslationsProvider.jsx create mode 100644 src/providers/translations/__tests__/TranslationsProvider.test.jsx create mode 100644 src/providers/translations/index.js diff --git a/src/components/Alert/Alert.jsx b/src/components/Alert/Alert.jsx index 34bf8623..3411eccc 100644 --- a/src/components/Alert/Alert.jsx +++ b/src/components/Alert/Alert.jsx @@ -1,9 +1,7 @@ import PropTypes from 'prop-types'; import React, { useContext } from 'react'; -import { - RUIContext, - withGlobalProps, -} from '../../provider'; +import { TranslationsContext } from '../../providers/translations'; +import { withGlobalProps } from '../../providers/globalProps'; import { classNames } from '../../utils/classNames'; import { transferProps } from '../../utils/transferProps'; import { getRootColorClassName } from '../_helpers/getRootColorClassName'; @@ -17,7 +15,7 @@ export const Alert = ({ onClose, ...restProps }) => { - const { translations } = useContext(RUIContext); + const translations = useContext(TranslationsContext); return (
{ ...restProps } = props; - const { translations } = useContext(RUIContext); + const translations = useContext(TranslationsContext); return (
- + ); }); ``` @@ -126,7 +126,7 @@ React.createElement(() => { React UI docs. You may not need it in your application. */} return ( -