1 | Object.defineProperty(exports, "__esModule", {
|
2 | value: true
|
3 | });
|
4 | exports.withDirectionPropTypes = exports.DIRECTIONS = undefined;
|
5 |
|
6 | var _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 |
|
8 | var _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 |
|
10 | exports['default'] = withDirection;
|
11 |
|
12 | var _react = require('react');
|
13 |
|
14 | var _react2 = _interopRequireDefault(_react);
|
15 |
|
16 | var _hoistNonReactStatics = require('hoist-non-react-statics');
|
17 |
|
18 | var _hoistNonReactStatics2 = _interopRequireDefault(_hoistNonReactStatics);
|
19 |
|
20 | var _deepmerge = require('deepmerge');
|
21 |
|
22 | var _deepmerge2 = _interopRequireDefault(_deepmerge);
|
23 |
|
24 | var _getComponentName = require('airbnb-prop-types/build/helpers/getComponentName');
|
25 |
|
26 | var _getComponentName2 = _interopRequireDefault(_getComponentName);
|
27 |
|
28 | var _constants = require('./constants');
|
29 |
|
30 | var _brcast = require('./proptypes/brcast');
|
31 |
|
32 | var _brcast2 = _interopRequireDefault(_brcast);
|
33 |
|
34 | var _direction = require('./proptypes/direction');
|
35 |
|
36 | var _direction2 = _interopRequireDefault(_direction);
|
37 |
|
38 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
|
39 |
|
40 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
41 |
|
42 | function _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 |
|
44 | function _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 |
|
46 | function _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; }
|
47 |
|
48 |
|
49 |
|
50 |
|
51 | var contextTypes = _defineProperty({}, _constants.CHANNEL, _brcast2['default']);
|
52 |
|
53 | exports.DIRECTIONS = _constants.DIRECTIONS;
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | var defaultDirection = _constants.DIRECTIONS.LTR;
|
59 |
|
60 |
|
61 | var withDirectionPropTypes = exports.withDirectionPropTypes = {
|
62 | direction: _direction2['default'].isRequired
|
63 | };
|
64 |
|
65 | function 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 |
|
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 |