UNPKG

1.71 kBJavaScriptView Raw
1import angular from 'angular';
2
3import {render, hydrate} from 'react-dom';
4import React from 'react';
5
6import {RerenderableSelect} from '../select/select';
7
8class SelectLazy {
9 constructor(container, props, ctrl, type) {
10 this.container = container;
11 this.ctrl = ctrl;
12 this.props = props || {};
13 this.type = type;
14 this.node = container;
15 this._popup = {
16 isVisible: angular.noop
17 };
18
19 this.attachEvents();
20 this.render();
21 }
22
23 onClick = () => {
24 this._clickHandler();
25 };
26
27 rerender(props = {}) {
28 for (const prop in props) {
29 if (props.hasOwnProperty(prop)) {
30 if (props[prop] == this.props[prop]) { //eslint-disable-line eqeqeq
31 break;
32 }
33
34 this.render(props);
35 return;
36 }
37 }
38 }
39
40 attachEvents() {
41 this.container.addEventListener('click', this.onClick);
42 }
43
44 detachEvents() {
45 this.container.removeEventListener('click', this.onClick);
46 }
47
48 render(props) {
49 this.reactSelect = <RerenderableSelect {...Object.assign({}, this.props, props || {})}/>;
50 this.props = this.reactSelect.props;
51
52 if (this.type !== 'dropdown') {
53 const ReactDOMServer = require('react-dom/server');
54 this.container.innerHTML = ReactDOMServer.renderToString(this.reactSelect);
55 }
56 }
57
58 _clickHandler() {
59 this.detachEvents();
60 if (this.type === 'dropdown') {
61 this.ctrl.selectInstance = render(this.reactSelect, this.container);
62 // In "dropdown" mode we don't click select itself, so need to force click handler
63 this.ctrl.selectInstance._clickHandler();
64 } else {
65 this.ctrl.selectInstance = hydrate(this.reactSelect, this.container);
66 }
67 }
68}
69
70export default SelectLazy;
71
\No newline at end of file