芝麻web文件管理V1.00
编辑当前文件:/home/freeclou/app.optimyar.com/backend/node_modules/react-with-styles/lib/withStyles.js
"use strict"; var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard"); var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.withStyles = withStyles; Object.defineProperty(exports, "withStylesPropTypes", { enumerable: true, get: function get() { return _withStylesPropTypes.withStylesPropTypes; } }); exports.css = exports["default"] = void 0; var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties")); var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose")); var _react = _interopRequireDefault(require("react")); var _hoistNonReactStatics = _interopRequireDefault(require("hoist-non-react-statics")); var _getComponentName = _interopRequireDefault(require("airbnb-prop-types/build/helpers/getComponentName")); var _ref3 = _interopRequireDefault(require("airbnb-prop-types/build/ref")); var _emptyStylesFn = _interopRequireDefault(require("./utils/emptyStylesFn")); var _perf = _interopRequireDefault(require("./utils/perf")); var _WithStylesContext = _interopRequireWildcard(require("./WithStylesContext")); var _ThemedStyleSheet = _interopRequireWildcard(require("./ThemedStyleSheet")); var _withStylesPropTypes = require("./withStylesPropTypes"); function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } /** * A higher order function that returns a higher order class component that injects * CSS-in-JS props derived from the react-with-styles theme, interface, and * direction provided through the WithStylesContext provider. * * The function should be used as follows: * `withStyles((theme) => styles, options)(Component)` * * Options can be used to rename the injected props, memoize the component, and flush * the styles to the styles tag (or whatever the interface implements as flush) before * rendering. * * @export * @param {Function|null|undefined} [stylesFn=EMPTY_STYLES_FN] * @param {Object} [{ * stylesPropName = 'styles', * themePropName = 'theme', * cssPropName = 'css', * flushBefore = false, * pureComponent = false, * }={}] * @returns a higher order component that wraps the provided component and injects * the react-with-styles css, styles, and theme props. */ function withStyles() { var stylesFn = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _emptyStylesFn["default"]; var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}, _ref$stylesPropName = _ref.stylesPropName, stylesPropName = _ref$stylesPropName === void 0 ? 'styles' : _ref$stylesPropName, _ref$themePropName = _ref.themePropName, themePropName = _ref$themePropName === void 0 ? 'theme' : _ref$themePropName, _ref$cssPropName = _ref.cssPropName, cssPropName = _ref$cssPropName === void 0 ? 'css' : _ref$cssPropName, _ref$flushBefore = _ref.flushBefore, flushBefore = _ref$flushBefore === void 0 ? false : _ref$flushBefore, _ref$pureComponent = _ref.pureComponent, pureComponent = _ref$pureComponent === void 0 ? false : _ref$pureComponent; stylesFn = stylesFn || _emptyStylesFn["default"]; var BaseClass = pureComponent ? _react["default"].PureComponent : _react["default"].Component; /** Cache for storing the result of stylesFn(theme) for all themes. */ var stylesFnResultCacheMap = typeof WeakMap === 'undefined' ? new Map() : new WeakMap(); function getOrCreateStylesFnResultCache(theme) { // Get and store the result in the stylesFnResultsCache for the component // -- not the instance -- so we only apply the theme to the stylesFn // once per theme for this component. var cachedResultForTheme = stylesFnResultCacheMap.get(theme); var stylesFnResult = cachedResultForTheme || stylesFn(theme) || {}; stylesFnResultCacheMap.set(theme, stylesFnResult); // cache the result of stylesFn(theme) return stylesFnResult; } /** * Cache for storing the results of computations: * `WeakMap
>` * Falling back to `Map` whenever `WeakMap` is not supported */ var withStylesCache = typeof WeakMap === 'undefined' ? new Map() : new WeakMap(); function getComponentCache(theme, component, direction) { var themeCache = withStylesCache.get(theme); if (!themeCache) { return null; } var componentCache = themeCache.get(component); if (!componentCache) { return null; } return componentCache[direction]; } function updateComponentCache(theme, component, direction, results) { var themeCache = withStylesCache.get(theme); if (!themeCache) { themeCache = typeof WeakMap === 'undefined' ? new Map() : new WeakMap(); withStylesCache.set(theme, themeCache); } var componentCache = themeCache.get(component); if (!componentCache) { componentCache = { ltr: {}, rtl: {} }; themeCache.set(component, componentCache); } componentCache[direction] = results; } /** Derive the create function from the interface and direction */ function makeCreateFn(direction, stylesInterface) { var directionSelector = direction === _WithStylesContext.DIRECTIONS.RTL ? 'RTL' : 'LTR'; var create = stylesInterface["create".concat(directionSelector)] || stylesInterface.create; var original = create; if (process.env.NODE_ENV !== 'production') { create = (0, _perf["default"])('create')(create); } return { create: create, original: original }; } /** Derive the resolve function from the interface and direction */ function makeResolveFn(direction, stylesInterface) { var directionSelector = direction === _WithStylesContext.DIRECTIONS.RTL ? 'RTL' : 'LTR'; var resolve = stylesInterface["resolve".concat(directionSelector)] || stylesInterface.resolve; var original = resolve; if (process.env.NODE_ENV !== 'production') { resolve = (0, _perf["default"])('resolve')(resolve); } return { resolve: resolve, original: original }; } // The function that wraps the provided component in a wrapper // component that injects the withStyles props return function withStylesHOC(WrappedComponent) { var wrappedComponentName = (0, _getComponentName["default"])(WrappedComponent); // The wrapper component that injects the withStyles props var WithStyles = /*#__PURE__*/function (_BaseClass) { (0, _inheritsLoose2["default"])(WithStyles, _BaseClass); function WithStyles() { return _BaseClass.apply(this, arguments) || this; } var _proto = WithStyles.prototype; _proto.getCurrentInterface = function getCurrentInterface() { // Fallback to the singleton implementation return this.context && this.context.stylesInterface || (0, _ThemedStyleSheet._getInterface)(); }; _proto.getCurrentTheme = function getCurrentTheme() { // Fallback to the singleton implementation return this.context && this.context.stylesTheme || (0, _ThemedStyleSheet._getTheme)(); }; _proto.getCurrentDirection = function getCurrentDirection() { return this.context && this.context.direction || _WithStylesContext.DIRECTIONS.LTR; }; _proto.getProps = function getProps() { // Get the styles interface, theme, and direction from context var stylesInterface = this.getCurrentInterface(); var theme = this.getCurrentTheme(); var direction = this.getCurrentDirection(); // Use a cache to store the interface methods and created styles by direction. // This way, if the theme and the interface don't change, we do not recalculate // styles or any other interface derivations. They are effectively only calculated // once per direction, until the theme or interface change. // Assume: always an object. var componentCache = getComponentCache(theme, WithStyles, direction); // Determine what's changed var interfaceChanged = !componentCache || !componentCache.stylesInterface || stylesInterface && componentCache.stylesInterface !== stylesInterface; var themeChanged = !componentCache || componentCache.theme !== theme; // If the interface and theme haven't changed for this direction, // we return the cached props immediately. if (!interfaceChanged && !themeChanged) { return componentCache.props; } // If the theme or the interface changed, then there are some values // we need to recalculate. We avoid recalculating the ones we already // calculated in the past if the objects they're derived from have not // changed. var createFn = interfaceChanged && makeCreateFn(direction, stylesInterface) || componentCache.create; var resolveFn = interfaceChanged && makeResolveFn(direction, stylesInterface) || componentCache.resolve; var create = createFn.create; var resolve = resolveFn.resolve; // Determine if create or resolve functions have changed, which will then // determine if we need to create new styles or css props var createChanged = !componentCache || !componentCache.create || createFn.original !== componentCache.create.original; var resolveChanged = !componentCache || !componentCache.resolve || resolveFn.original !== componentCache.resolve.original; // Derive the css function prop: recalculate it if resolve changed var css = resolveChanged && function () { for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return resolve(args); } || componentCache.props.css; // Get or calculate the themed styles from the stylesFn: // Uses a separate cache at the component level, not at the instance level, // to only apply the theme to the stylesFn once per component class per theme. var stylesFnResult = getOrCreateStylesFnResultCache(theme); // Derive the styles prop: recalculate it if create changed, or stylesFnResult changed var styles = (createChanged || stylesFnResult !== componentCache.stylesFnResult) && create(stylesFnResult) || componentCache.props.styles; // Put the new props together var props = { css: css, styles: styles, theme: theme }; // Update the cache with all the new values updateComponentCache(theme, WithStyles, direction, { stylesInterface: stylesInterface, theme: theme, create: createFn, resolve: resolveFn, stylesFnResult: stylesFnResult, props: props }); return props; }; _proto.flush = function flush() { var stylesInterface = this.getCurrentInterface(); if (stylesInterface && stylesInterface.flush) { stylesInterface.flush(); } }; _proto.render = function render() { var _ref2; // We only want to re-render if the theme, stylesInterface, or direction change. // These values are in context so we're listening for their updates. // this.getProps() derives the props from the theme, stylesInterface, and direction in // context, and memoizes them on the instance per direction. var _this$getProps = this.getProps(), theme = _this$getProps.theme, styles = _this$getProps.styles, css = _this$getProps.css; // Flush if specified if (flushBefore) { this.flush(); } var _this$props = this.props, forwardedRef = _this$props.forwardedRef, rest = (0, _objectWithoutProperties2["default"])(_this$props, ["forwardedRef"]); return /*#__PURE__*/_react["default"].createElement(WrappedComponent // TODO: remove conditional once breaking change to only support React 16.3+ // ref: https://github.com/airbnb/react-with-styles/pull/240#discussion_r533497857 , (0, _extends2["default"])({ ref: typeof _react["default"].forwardRef === 'undefined' ? undefined : forwardedRef }, typeof _react["default"].forwardRef === 'undefined' ? this.props : rest, (_ref2 = {}, (0, _defineProperty2["default"])(_ref2, themePropName, theme), (0, _defineProperty2["default"])(_ref2, stylesPropName, styles), (0, _defineProperty2["default"])(_ref2, cssPropName, css), _ref2))); }; return WithStyles; }(BaseClass); // TODO: remove conditional once breaking change to only support React 16.3+ // ref: https://github.com/airbnb/react-with-styles/pull/240#discussion_r533497857 if (typeof _react["default"].forwardRef !== 'undefined') { WithStyles.propTypes = { forwardedRef: (0, _ref3["default"])() }; } // TODO: remove conditional once breaking change to only support React 16.3+ // ref: https://github.com/airbnb/react-with-styles/pull/240#discussion_r533497857 var ForwardedWithStyles = typeof _react["default"].forwardRef === 'undefined' ? WithStyles : /*#__PURE__*/_react["default"].forwardRef(function (props, forwardedRef) { return /*#__PURE__*/_react["default"].createElement(WithStyles, (0, _extends2["default"])({}, props, { forwardedRef: forwardedRef })); }); // Copy the wrapped component's prop types and default props on WithStyles if (WrappedComponent.propTypes) { ForwardedWithStyles.propTypes = _objectSpread({}, WrappedComponent.propTypes); delete ForwardedWithStyles.propTypes[stylesPropName]; delete ForwardedWithStyles.propTypes[themePropName]; delete ForwardedWithStyles.propTypes[cssPropName]; } if (WrappedComponent.defaultProps) { ForwardedWithStyles.defaultProps = _objectSpread({}, WrappedComponent.defaultProps); } WithStyles.contextType = _WithStylesContext["default"]; ForwardedWithStyles.WrappedComponent = WrappedComponent; ForwardedWithStyles.displayName = "withStyles(".concat(wrappedComponentName, ")"); return (0, _hoistNonReactStatics["default"])(ForwardedWithStyles, WrappedComponent); }; } var _default = withStyles; /** * Deprecated: Do not use directly. Please wrap your component in `withStyles` and use the `css` * prop injected via props instead. */ exports["default"] = _default; var css = _ThemedStyleSheet["default"].resolveLTR; exports.css = css;