UNPKG

6.1 kBJavaScriptView Raw
1Object.defineProperty(exports, "__esModule", {
2 value: true
3});
4exports.withDirectionPropTypes = exports.DIRECTIONS = undefined;
5
6var _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; };
7
8var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
9
10exports['default'] = withDirection;
11
12var _react = require('react');
13
14var _react2 = _interopRequireDefault(_react);
15
16var _hoistNonReactStatics = require('hoist-non-react-statics');
17
18var _hoistNonReactStatics2 = _interopRequireDefault(_hoistNonReactStatics);
19
20var _deepmerge = require('deepmerge');
21
22var _deepmerge2 = _interopRequireDefault(_deepmerge);
23
24var _getComponentName = require('airbnb-prop-types/build/helpers/getComponentName');
25
26var _getComponentName2 = _interopRequireDefault(_getComponentName);
27
28var _constants = require('./constants');
29
30var _brcast = require('./proptypes/brcast');
31
32var _brcast2 = _interopRequireDefault(_brcast);
33
34var _direction = require('./proptypes/direction');
35
36var _direction2 = _interopRequireDefault(_direction);
37
38function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
39
40function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
41
42function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
43
44function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
45
46function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } /* eslint-disable react/forbid-foreign-prop-types */
47// This higher-order component consumes a string from React context that is
48// provided by the DirectionProvider component.
49// We can use this to conditionally switch layout/direction for right-to-left layouts.
50
51var contextTypes = _defineProperty({}, _constants.CHANNEL, _brcast2['default']);
52
53exports.DIRECTIONS = _constants.DIRECTIONS;
54
55// set a default direction so that a component wrapped with this HOC can be
56// used even without a DirectionProvider ancestor in its react tree.
57
58var defaultDirection = _constants.DIRECTIONS.LTR;
59
60// export for convenience, in order for components to spread these onto their propTypes
61var withDirectionPropTypes = exports.withDirectionPropTypes = {
62 direction: _direction2['default'].isRequired
63};
64
65function withDirection(WrappedComponent) {
66 var WithDirection = function (_React$Component) {
67 _inherits(WithDirection, _React$Component);
68
69 function WithDirection(props, context) {
70 _classCallCheck(this, WithDirection);
71
72 var _this = _possibleConstructorReturn(this, (WithDirection.__proto__ || Object.getPrototypeOf(WithDirection)).call(this, props, context));
73
74 _this.state = {
75 direction: context[_constants.CHANNEL] ? context[_constants.CHANNEL].getState() : defaultDirection
76 };
77 return _this;
78 }
79
80 _createClass(WithDirection, [{
81 key: 'componentDidMount',
82 value: function () {
83 function componentDidMount() {
84 var _this2 = this;
85
86 if (this.context[_constants.CHANNEL]) {
87 // subscribe to future direction changes
88 this.channelUnsubscribe = this.context[_constants.CHANNEL].subscribe(function (direction) {
89 _this2.setState({ direction: direction });
90 });
91 }
92 }
93
94 return componentDidMount;
95 }()
96 }, {
97 key: 'componentWillUnmount',
98 value: function () {
99 function componentWillUnmount() {
100 if (this.channelUnsubscribe) {
101 this.channelUnsubscribe();
102 }
103 }
104
105 return componentWillUnmount;
106 }()
107 }, {
108 key: 'render',
109 value: function () {
110 function render() {
111 var direction = this.state.direction;
112
113
114 return _react2['default'].createElement(WrappedComponent, _extends({}, this.props, {
115 direction: direction
116 }));
117 }
118
119 return render;
120 }()
121 }]);
122
123 return WithDirection;
124 }(_react2['default'].Component);
125
126 var wrappedComponentName = (0, _getComponentName2['default'])(WrappedComponent) || 'Component';
127
128 WithDirection.WrappedComponent = WrappedComponent;
129 WithDirection.contextTypes = contextTypes;
130 WithDirection.displayName = 'withDirection(' + String(wrappedComponentName) + ')';
131 if (WrappedComponent.propTypes) {
132 WithDirection.propTypes = (0, _deepmerge2['default'])({}, WrappedComponent.propTypes);
133 delete WithDirection.propTypes.direction;
134 }
135 if (WrappedComponent.defaultProps) {
136 WithDirection.defaultProps = (0, _deepmerge2['default'])({}, WrappedComponent.defaultProps);
137 }
138
139 return (0, _hoistNonReactStatics2['default'])(WithDirection, WrappedComponent);
140}
\No newline at end of file