This repository was archived by the owner on Mar 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 313
Expand file tree
/
Copy pathElement.js
More file actions
138 lines (115 loc) · 3.39 KB
/
Element.js
File metadata and controls
138 lines (115 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// @flow
import React from 'react';
import PropTypes from 'prop-types';
import isEqual from '../utils/isEqual';
import {type ElementContext, elementContextTypes} from './Elements';
type Props = {
id?: string,
className?: string,
onChange: Function,
onBlur: Function,
onFocus: Function,
onReady: Function,
};
const noop = () => {};
const _extractOptions = (props: Props): Object => {
const {id, className, onChange, onFocus, onBlur, onReady, ...options} = props;
return options;
};
const capitalized = (str: string) => {
return str.charAt(0).toUpperCase() + str.slice(1);
};
const Element = (
type: string,
hocOptions: {impliedTokenType?: string, impliedSourceType?: string} = {}
) =>
class extends React.Component<Props> {
static propTypes = {
id: PropTypes.string,
className: PropTypes.string,
onChange: PropTypes.func,
onBlur: PropTypes.func,
onFocus: PropTypes.func,
onReady: PropTypes.func,
};
static defaultProps = {
id: undefined,
className: undefined,
onChange: noop,
onBlur: noop,
onFocus: noop,
onReady: noop,
};
static contextTypes = elementContextTypes;
static displayName = `${capitalized(type)}Element`;
constructor(props: Props, context: ElementContext) {
super(props, context);
this._element = null;
const options = _extractOptions(this.props);
// We keep track of the extracted options on this._options to avoid re-rendering.
// (We would unnecessarily re-render if we were tracking them with state.)
this._options = options;
}
componentDidMount() {
this.context.addElementsLoadListener((elements: ElementsShape) => {
const element = elements.create(type, this._options);
this._element = element;
this._setupEventListeners(element);
element.mount(this._ref);
// Register Element for automatic token / source creation
if (hocOptions.impliedTokenType || hocOptions.impliedSourceType) {
this.context.registerElement(
element,
hocOptions.impliedTokenType,
hocOptions.impliedSourceType
);
}
});
}
componentDidUpdate() {
const options = _extractOptions(this.props);
if (
Object.keys(options).length !== 0 &&
!isEqual(options, this._options)
) {
this._options = options;
if (this._element) {
this._element.update(options);
}
}
}
componentWillUnmount() {
if (this._element) {
const element = this._element;
element.destroy();
this.context.unregisterElement(element);
}
}
context: ElementContext;
_element: ElementShape | null;
_ref: ?HTMLElement;
_options: Object;
_setupEventListeners(element: ElementShape) {
element.on('ready', () => {
this.props.onReady(this._element);
});
element.on('change', (change) => {
this.props.onChange(change);
});
element.on('blur', (...args) => this.props.onBlur(...args));
element.on('focus', (...args) => this.props.onFocus(...args));
}
handleRef = (ref: ?HTMLElement) => {
this._ref = ref;
};
render() {
return (
<div
id={this.props.id}
className={this.props.className}
ref={this.handleRef}
/>
);
}
};
export default Element;