1 |
|
2 |
|
3 |
|
4 |
|
5 | import PropTypes from 'prop-types';
|
6 | import React from 'react';
|
7 | import { forbidExtraProps } from 'airbnb-prop-types';
|
8 | import brcast from 'brcast';
|
9 | import brcastShape from './proptypes/brcast';
|
10 | import directionPropType from './proptypes/direction';
|
11 | import { DIRECTIONS, CHANNEL } from './constants';
|
12 |
|
13 | const propTypes = forbidExtraProps({
|
14 | children: PropTypes.node.isRequired,
|
15 | direction: directionPropType.isRequired,
|
16 | });
|
17 |
|
18 | const childContextTypes = {
|
19 | [CHANNEL]: brcastShape,
|
20 | };
|
21 |
|
22 | export { DIRECTIONS };
|
23 |
|
24 | export default class DirectionProvider extends React.Component {
|
25 | constructor(props) {
|
26 | super(props);
|
27 | this.broadcast = brcast(props.direction);
|
28 | }
|
29 |
|
30 | getChildContext() {
|
31 | return {
|
32 | [CHANNEL]: this.broadcast,
|
33 | };
|
34 | }
|
35 |
|
36 | componentWillReceiveProps(nextProps) {
|
37 | if (this.props.direction !== nextProps.direction) {
|
38 | this.broadcast.setState(nextProps.direction);
|
39 | }
|
40 | }
|
41 |
|
42 | render() {
|
43 | const { children, direction } = this.props;
|
44 | return (
|
45 | <div dir={direction}>
|
46 | {React.Children.only(children)}
|
47 | </div>
|
48 | );
|
49 | }
|
50 | }
|
51 |
|
52 | DirectionProvider.propTypes = propTypes;
|
53 | DirectionProvider.childContextTypes = childContextTypes;
|