From 87de314c182a8e0ebd69cefa7ea188c4623c6fc5 Mon Sep 17 00:00:00 2001 From: Vlad Date: Mon, 29 Jan 2018 20:18:14 -0500 Subject: [PATCH 01/13] Adds option to query remote source for data --- assets/js/src/form/fields/selection.jsx | 38 +++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/assets/js/src/form/fields/selection.jsx b/assets/js/src/form/fields/selection.jsx index 54f3fcde4c..9f36f57887 100644 --- a/assets/js/src/form/fields/selection.jsx +++ b/assets/js/src/form/fields/selection.jsx @@ -55,7 +55,7 @@ define([ return; } - const select2 = jQuery(`#${this.refs.select.id}`).select2({ + let select2Options = { width: (this.props.width || ''), templateResult: function (item) { if (item.element && item.element.selected) { @@ -65,7 +65,41 @@ define([ } return item.text; }, - }); + }; + + const remoteQuery = this.props.field.remoteQuery || null; + if (remoteQuery) { + select2Options = Object.assign(select2Options, { + ajax: { + url: window.ajaxurl, + type: 'POST', + dataType: 'json', + data: function (params) { + return { + action: 'mailpoet', + api_version: window.mailpoet_api_version, + token: window.mailpoet_token, + endpoint: remoteQuery.endpoint, + method: remoteQuery.method, + data: Object.assign( + remoteQuery.data, + { query: params.term } + ), + }; + }, + processResults: function (response) { + return { + results: response.data.map(item => ( + { id: item.id || item.value, text: item.name || item.text } + )), + }; + }, + }, + minimumInputLength: remoteQuery.minimumInputLength || 2, + }); + } + + const select2 = jQuery(`#${this.refs.select.id}`).select2(select2Options); let hasRemoved = false; select2.on('select2:unselecting', () => { From fef8017134a8e5e542bb0c8abde3aee481b9f829 Mon Sep 17 00:00:00 2001 From: Vlad Date: Tue, 30 Jan 2018 21:19:19 -0500 Subject: [PATCH 02/13] Loads select items before render --- assets/js/src/form/fields/selection.jsx | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/assets/js/src/form/fields/selection.jsx b/assets/js/src/form/fields/selection.jsx index 9f36f57887..4cd06a42ec 100644 --- a/assets/js/src/form/fields/selection.jsx +++ b/assets/js/src/form/fields/selection.jsx @@ -12,13 +12,9 @@ define([ const Selection = React.createClass({ getInitialState: function () { return { - items: [], select2: false, }; }, - componentWillMount: function () { - this.loadCachedItems(); - }, allowMultipleValues: function () { return (this.props.field.multiple === true); }, @@ -130,7 +126,7 @@ define([ } return null; }, - loadCachedItems: function () { + loadItems: function () { let items; if (typeof (window[`mailpoet_${this.props.field.endpoint}`]) !== 'undefined') { items = window[`mailpoet_${this.props.field.endpoint}`]; @@ -142,11 +138,9 @@ define([ if (this.props.field.filter !== undefined) { items = items.filter(this.props.field.filter); } - - this.setState({ - items: items, - }); } + + return items; }, handleChange: function (e) { let value; @@ -201,7 +195,8 @@ define([ return undefined; }, render: function () { - const options = this.state.items.map((item, index) => { + const items = this.loadItems(this.props.field); + const options = items.map((item, index) => { const label = this.getLabel(item); const searchLabel = this.getSearchLabel(item); const value = this.getValue(item); From a0667adace3bdbbb8b9df263dcff1f14bdef75f7 Mon Sep 17 00:00:00 2001 From: Vlad Date: Wed, 31 Jan 2018 17:48:17 -0500 Subject: [PATCH 03/13] Prevents component from rerendering when Select2 is initialized --- assets/js/src/form/fields/selection.jsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/assets/js/src/form/fields/selection.jsx b/assets/js/src/form/fields/selection.jsx index 4cd06a42ec..3e76ad4066 100644 --- a/assets/js/src/form/fields/selection.jsx +++ b/assets/js/src/form/fields/selection.jsx @@ -44,6 +44,7 @@ define([ destroySelect2: function () { if (this.isSelect2Initialized()) { jQuery(`#${this.refs.select.id}`).select2('destroy'); + this.state.select2 = false; } }, setupSelect2: function () { @@ -110,7 +111,7 @@ define([ select2.on('change', this.handleChange); - this.setState({ select2: true }); + this.state.select2 = true; }, getSelectedValues: function () { if (this.props.field.selected !== undefined) { From b88dec06d94f22ef854b877a14b0c4c73e329f61 Mon Sep 17 00:00:00 2001 From: Vlad Date: Wed, 31 Jan 2018 17:52:04 -0500 Subject: [PATCH 04/13] Adds getter for field ID --- assets/js/src/form/fields/selection.jsx | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/assets/js/src/form/fields/selection.jsx b/assets/js/src/form/fields/selection.jsx index 3e76ad4066..9a13a22c2f 100644 --- a/assets/js/src/form/fields/selection.jsx +++ b/assets/js/src/form/fields/selection.jsx @@ -41,6 +41,10 @@ define([ this.destroySelect2(); } }, + getFieldId: function (data) { + const props = data || this.props; + return props.field.id || props.field.name; + }, destroySelect2: function () { if (this.isSelect2Initialized()) { jQuery(`#${this.refs.select.id}`).select2('destroy'); @@ -215,7 +219,7 @@ define([ return ( control. if (this.allowMultipleValues()) return undefined; - if (this.props.field.placeholder) return (