@roderickhsiao/react-i13n
Version:
[Experiment] React I13n provides a performant and scalable solution to application instrumentation.
96 lines (82 loc) • 4.04 kB
JavaScript
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
/**
* Copyright 2015 - Present, Yahoo Inc.
* Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
*/
import React, { useMemo } from 'react';
import hoistNonReactStatics from 'hoist-non-react-statics';
import getDisplayName from '../utils/getDisplayName';
import I13nContext from '../components/core/I13nContext';
import useI13nNode from '../hooks/useI13nNode';
import useReactI13nRoot from '../hooks/useReactI13nRoot';
var debugLib = require('debug');
var debug = debugLib('ReactI13n');
/**
* Create an app level component with i13n setup
* @param {Object} Component the root level component
*
* @param {Object} options passed into ReactI13n
* @param {Boolean} options.isViewportEnabled if enable viewport checking
* @param {Object} options.displayName display name of the wrapper component
* @param {Object} options.i13nNodeClass the i13nNode class, you can inherit it with your own functionalities
* @param {Object} options.rootModelData model data of root i13n node
* @param {Boolean} options.skipUtilFunctionsByProps true to prevent i13n util function to be passed via props.i13n
*
* @param {Array} plugins plugins
* @method setupI13n
*/
function setupI13n(Component) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var plugins = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
if (!plugins.length) {
debug('no plugins provided');
}
var {
displayName,
skipUtilFunctionsByProps
} = options;
var RootI13nComponent = props => {
var {
children,
i13nModel,
isLeafNode
} = props,
restProps = _objectWithoutProperties(props, ["children", "i13nModel", "isLeafNode"]);
var {
i13nInstance: reactI13n,
executeI13nEvent: executeEvent
} = useReactI13nRoot(options);
if (reactI13n) {
plugins.forEach(plugin => {
reactI13n.plug(plugin);
});
reactI13n.createRootI13nNode();
}
var parentI13nNode = reactI13n === null || reactI13n === void 0 ? void 0 : reactI13n.getRootI13nNode();
var {
i13nNode
} = useI13nNode({
i13nInstance: reactI13n,
i13nModel,
isLeafNode,
parentI13nNode
});
var contextValue = useMemo(() => ({
executeEvent,
i13nInstance: reactI13n,
i13nNode,
parentI13nNode
}), [executeEvent, reactI13n, i13nNode, parentI13nNode]);
return /*#__PURE__*/React.createElement(I13nContext.Provider, {
value: contextValue
}, /*#__PURE__*/React.createElement(Component, _extends({}, restProps, {
i13n: !skipUtilFunctionsByProps ? contextValue : undefined
}), children));
};
RootI13nComponent.displayName = displayName || "RootI13n".concat(getDisplayName(Component));
hoistNonReactStatics(RootI13nComponent, Component);
return RootI13nComponent;
}
export default setupI13n;