-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathindex.js
More file actions
129 lines (113 loc) · 3.11 KB
/
index.js
File metadata and controls
129 lines (113 loc) · 3.11 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
'use strict';
// setup jQuery
var $;
if (typeof window !== 'undefined' && window) {
if (window.jQuery) {
$ = window.jQuery;
} else {
$ = require('jquery');
window.jQuery = $;
}
} else {
$ = require('jquery');
}
var React = require('react');
var objectAssign = require('object-assign');
var getOptions = require('./get-options.js');
var bsMultiselect = require('./bootstrap-multiselect.js');
var bsDropdown;
// make it play nice when we already have bootstrap dropdown loaded.
try {
var BS = require('bootstrap');
if (typeof BS.dropdown !== 'undefined') {
bsDropdown = BS.dropdown;
}
} catch (e) {
}
if (!bsDropdown) {
bsDropdown = require('./bootstrap-dropdown.js');
}
$ = bsDropdown.init($);
$ = bsMultiselect.init($);
/* this is our exported React class */
class MultiSelect extends React.Component {
constructor(props) {
super(props);
this.$multiselect = null;
this.options = getOptions();
}
syncData() {
// this function is meant to be called from parent component
// in case selected values are changed outside of this component
// and need to be synced
// this function can not be called every time on this.render, because
// dropdown would close after selecting first item
if(this.$multiselect !== null){
this.$multiselect.multiselect('dataprovider', this.props.data || []);
}
}
getOptionsFromProps() {
var currentOptions = {}, $this = this;
$this.options.forEach(function (option) {
if ($this.props.hasOwnProperty(option)) {
currentOptions[option] = $this.props[option];
}
});
return currentOptions;
}
setOptionsFromProps() {
var currentOptions = this.getOptionsFromProps();
if (this.$multiselect) {
if (Object.keys(currentOptions).length) {
this.$multiselect.multiselect('setOptions', currentOptions);
this.$multiselect.multiselect('buildDropdown');
}
}
}
componentDidMount() {
var $this = this;
// initialize
$this.$multiselect = $($this.selectRef);
$this.$multiselect.multiselect($this.getOptionsFromProps());
$this.setOptionsFromProps();
$this.$multiselect.multiselect('dataprovider', $this.props.data || []);
if ($this.props.disabled) {
$this.$multiselect.multiselect('disable');
}
}
componentWillUnmount() {
if (this.$multiselect) {
this.$multiselect.multiselect('destroy');
}
this.$multiselect = null;
}
componentWillReceiveProps(nextProps) {
if (nextProps.options !== this.props.options) {
this.setOptionsFromProps();
}
if(nextProps.data !== this.props.data) {
this.$multiselect.multiselect('dataprovider', nextProps.data || []);
}
if (nextProps.disabled) {
this.$multiselect.multiselect('disable');
} else {
this.$multiselect.multiselect('enable');
}
}
render() {
//this.setOptionsFromProps();
var options = getOptions();
var props = {};
for (var key in this.props) {
if (this.props.hasOwnProperty(key) && options.indexOf(key) === -1) {
props[key] = this.props[key];
}
}
return React.createElement('select',
objectAssign({}, props, {
ref: (select) => this.selectRef = select
}));
}
}
MultiSelect.displayName = 'MultiSelect';
module.exports = MultiSelect;