@instructure/ui-react-utils
Version:
A React utility library made by Instructure Inc.
81 lines (76 loc) • 4.11 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.withDeterministicId = exports.default = void 0;
var _react = require("react");
var _hoistNonReactStatics = _interopRequireDefault(require("hoist-non-react-statics"));
var _DeterministicIdContext = require("./DeterministicIdContext");
var _decorator = require("@instructure/ui-decorator/lib/decorator.js");
var _generateId = require("@instructure/ui-utils/lib/generateId.js");
var _console = require("@instructure/console");
var _jsxRuntime = require("@emotion/react/jsx-runtime");
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 - present Instructure, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/**
* This decorator is used to enable the decorated class to use the `DeterministicIdContext` which is needed
* for deterministic id generation.
*
* The context is there for the users to pass an `instanceCounterMap` Map which is then used
* in the child components to deterministically create ids for them based on the `instanceCounterMap`.
* Read more about it here: [SSR guide](https://instructure.design/#server-side-rendering)
*/
const withDeterministicId = exports.withDeterministicId = (0, _decorator.decorator)(ComposedComponent => {
const WithDeterministicId = /*#__PURE__*/(0, _react.forwardRef)((props, ref) => {
const componentName = ComposedComponent.componentId || ComposedComponent.displayName || ComposedComponent.name;
const instanceCounterMap = (0, _react.useContext)(_DeterministicIdContext.DeterministicIdContext);
const deterministicId = (instanceName = componentName) => (0, _generateId.generateId)(instanceName, instanceCounterMap);
if (props.deterministicId) {
(0, _console.warn)(false, `Manually passing the "deterministicId" property is not allowed on the ${componentName} component.\n`, props.deterministicId);
}
return (0, _jsxRuntime.jsx)(ComposedComponent, {
ref: ref,
deterministicId: deterministicId,
...props
});
});
(0, _hoistNonReactStatics.default)(WithDeterministicId, ComposedComponent);
// we have to pass these on, because sometimes users
// access propTypes of the component in other components
// eslint-disable-next-line react/forbid-foreign-prop-types
WithDeterministicId.propTypes = ComposedComponent.propTypes;
WithDeterministicId.defaultProps = ComposedComponent.defaultProps;
// These static fields exist on InstUI components
//@ts-expect-error fix this
WithDeterministicId.allowedProps = ComposedComponent.allowedProps;
// added so it can be tested with ReactTestUtils
// more info: https://github.com/facebook/react/issues/13455
WithDeterministicId.originalType = ComposedComponent.originalType || ComposedComponent;
if (process.env.NODE_ENV !== 'production') {
WithDeterministicId.displayName = `WithDeterministicId(${ComposedComponent.displayName})`;
}
return WithDeterministicId;
});
var _default = exports.default = withDeterministicId;