Redesign form/fields/selection component
[MAILPOET-2787]
This commit is contained in:
@@ -1,8 +1,7 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import moment from 'moment';
|
import moment from 'moment';
|
||||||
import PropTypes from 'prop-types';
|
import PropTypes from 'prop-types';
|
||||||
import classnames from 'classnames';
|
import Select from '../../common/form/select/select';
|
||||||
import Select from "../../common/form/select/select";
|
|
||||||
|
|
||||||
function FormFieldDateYear(props) {
|
function FormFieldDateYear(props) {
|
||||||
const yearsRange = 100;
|
const yearsRange = 100;
|
||||||
|
@@ -2,7 +2,7 @@ import React from 'react';
|
|||||||
import jQuery from 'jquery';
|
import jQuery from 'jquery';
|
||||||
import _ from 'underscore';
|
import _ from 'underscore';
|
||||||
import 'react-dom';
|
import 'react-dom';
|
||||||
import 'select2';
|
import 'select2/dist/js/select2.full';
|
||||||
import PropTypes from 'prop-types';
|
import PropTypes from 'prop-types';
|
||||||
|
|
||||||
class Selection extends React.Component {
|
class Selection extends React.Component {
|
||||||
@@ -99,10 +99,35 @@ class Selection extends React.Component {
|
|||||||
return item.id;
|
return item.id;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
getCount = (item) => {
|
||||||
|
if (this.props.field.getCount !== undefined) {
|
||||||
|
return this.props.field.getCount(item, this.props.item);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
};
|
||||||
|
|
||||||
|
getTag = (item) => {
|
||||||
|
if (this.props.field.getTag !== undefined) {
|
||||||
|
return this.props.field.getTag(item, this.props.item);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
};
|
||||||
|
|
||||||
setupSelect2 = () => {
|
setupSelect2 = () => {
|
||||||
if (this.isSelect2Initialized()) {
|
if (this.isSelect2Initialized()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
const templateRendered = (option) => {
|
||||||
|
let tpl = '';
|
||||||
|
if (option.tag) {
|
||||||
|
tpl += `<span class="mailpoet-form-select2-tag">${option.tag}</span>`;
|
||||||
|
}
|
||||||
|
tpl += `<span class="mailpoet-form-select2-text"><span>${option.text}</span></span>`;
|
||||||
|
if (option.count) {
|
||||||
|
tpl += `<span class="mailpoet-form-select2-count">${option.count}</span>`;
|
||||||
|
}
|
||||||
|
return tpl;
|
||||||
|
};
|
||||||
|
|
||||||
let select2Options = {
|
let select2Options = {
|
||||||
disabled: this.props.disabled || false,
|
disabled: this.props.disabled || false,
|
||||||
@@ -111,15 +136,10 @@ class Selection extends React.Component {
|
|||||||
id: '', // the value of the option
|
id: '', // the value of the option
|
||||||
text: this.props.field.placeholder,
|
text: this.props.field.placeholder,
|
||||||
},
|
},
|
||||||
templateResult: function templateResult(item) {
|
dropdownCssClass: 'mailpoet-form-select2-dropdown',
|
||||||
if (item.element && item.element.selected) {
|
escapeMarkup: (markup) => markup,
|
||||||
return null;
|
templateResult: templateRendered,
|
||||||
}
|
templateSelection: templateRendered,
|
||||||
if (item.title) {
|
|
||||||
return item.title;
|
|
||||||
}
|
|
||||||
return item.text;
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const remoteQuery = this.props.field.remoteQuery || null;
|
const remoteQuery = this.props.field.remoteQuery || null;
|
||||||
@@ -158,6 +178,20 @@ class Selection extends React.Component {
|
|||||||
},
|
},
|
||||||
minimumInputLength: remoteQuery.minimumInputLength || 2,
|
minimumInputLength: remoteQuery.minimumInputLength || 2,
|
||||||
});
|
});
|
||||||
|
} else {
|
||||||
|
const items = this.getItems(this.props.field);
|
||||||
|
const selectedValues = this.getSelectedValues() || [];
|
||||||
|
const data = items.map((item) => {
|
||||||
|
const id = this.getValue(item);
|
||||||
|
return {
|
||||||
|
id,
|
||||||
|
tag: this.getTag(item),
|
||||||
|
text: this.getLabel(item),
|
||||||
|
count: this.getCount(item),
|
||||||
|
selected: selectedValues.indexOf(id) > -1,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
select2Options = Object.assign(select2Options, { data });
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.props.field.extendSelect2Options !== undefined) {
|
if (this.props.field.extendSelect2Options !== undefined) {
|
||||||
@@ -252,38 +286,21 @@ class Selection extends React.Component {
|
|||||||
};
|
};
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const items = this.getItems(this.props.field);
|
|
||||||
const selectedValues = this.getSelectedValues();
|
const selectedValues = this.getSelectedValues();
|
||||||
const options = items.map((item) => {
|
|
||||||
const label = this.getLabel(item);
|
|
||||||
const searchLabel = this.getSearchLabel(item);
|
|
||||||
const value = this.getValue(item);
|
|
||||||
|
|
||||||
return (
|
|
||||||
<option
|
|
||||||
key={`option-${item.id}`}
|
|
||||||
className="default"
|
|
||||||
value={value}
|
|
||||||
title={searchLabel}
|
|
||||||
>
|
|
||||||
{ label }
|
|
||||||
</option>
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<select
|
<div className="mailpoet-form-select mailpoet-form-input">
|
||||||
id={this.getFieldId()}
|
<select
|
||||||
ref={this.selectRef}
|
id={this.getFieldId()}
|
||||||
disabled={this.props.field.disabled}
|
ref={this.selectRef}
|
||||||
data-placeholder={this.props.field.placeholder}
|
disabled={this.props.field.disabled}
|
||||||
multiple={this.props.field.multiple}
|
data-placeholder={this.props.field.placeholder}
|
||||||
defaultValue={selectedValues}
|
multiple={this.props.field.multiple}
|
||||||
{...this.props.field.validation}// eslint-disable-line react/jsx-props-no-spreading
|
defaultValue={selectedValues}
|
||||||
>
|
{...this.props.field.validation}// eslint-disable-line react/jsx-props-no-spreading
|
||||||
{ this.insertEmptyOption() }
|
>
|
||||||
{ options }
|
{ this.insertEmptyOption() }
|
||||||
</select>
|
</select>
|
||||||
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -303,6 +320,8 @@ Selection.propTypes = {
|
|||||||
filter: PropTypes.func,
|
filter: PropTypes.func,
|
||||||
getSearchLabel: PropTypes.func,
|
getSearchLabel: PropTypes.func,
|
||||||
getValue: PropTypes.func,
|
getValue: PropTypes.func,
|
||||||
|
getCount: PropTypes.func,
|
||||||
|
getTag: PropTypes.func,
|
||||||
placeholder: PropTypes.string,
|
placeholder: PropTypes.string,
|
||||||
remoteQuery: PropTypes.object,
|
remoteQuery: PropTypes.object,
|
||||||
extendSelect2Options: PropTypes.object,
|
extendSelect2Options: PropTypes.object,
|
||||||
|
@@ -36,7 +36,10 @@ let fields = [
|
|||||||
return !segment.deleted_at;
|
return !segment.deleted_at;
|
||||||
},
|
},
|
||||||
getLabel: function getLabel(segment) {
|
getLabel: function getLabel(segment) {
|
||||||
return `${segment.name} (${parseInt(segment.subscribers, 10).toLocaleString()})`;
|
return segment.name;
|
||||||
|
},
|
||||||
|
getCount: function getCount(segment) {
|
||||||
|
return parseInt(segment.subscribers, 10).toLocaleString();
|
||||||
},
|
},
|
||||||
transformChangedValue: function transformChangedValue(segmentIds) {
|
transformChangedValue: function transformChangedValue(segmentIds) {
|
||||||
const allSegments = this.getItems();
|
const allSegments = this.getItems();
|
||||||
|
@@ -134,7 +134,10 @@ let fields = [
|
|||||||
return !segment.deleted_at;
|
return !segment.deleted_at;
|
||||||
},
|
},
|
||||||
getLabel: function getLabel(segment) {
|
getLabel: function getLabel(segment) {
|
||||||
return `${segment.name} (${parseInt(segment.subscribers, 10).toLocaleString()})`;
|
return segment.name;
|
||||||
|
},
|
||||||
|
getCount: function getCount(segment) {
|
||||||
|
return parseInt(segment.subscribers, 10).toLocaleString();
|
||||||
},
|
},
|
||||||
transformChangedValue: function transformChangedValue(segmentIds) {
|
transformChangedValue: function transformChangedValue(segmentIds) {
|
||||||
const allSegments = this.getItems();
|
const allSegments = this.getItems();
|
||||||
|
@@ -67,7 +67,10 @@ const fields = [
|
|||||||
return (!segment.deleted_at && segment.type === 'default');
|
return (!segment.deleted_at && segment.type === 'default');
|
||||||
},
|
},
|
||||||
getLabel: function getLabel(segment) {
|
getLabel: function getLabel(segment) {
|
||||||
return `${segment.name} (${segment.subscribers})`;
|
return segment.name;
|
||||||
|
},
|
||||||
|
getCount: function getCount(segment) {
|
||||||
|
return segment.subscribers;
|
||||||
},
|
},
|
||||||
getSearchLabel: function getSearchLabel(segment, subscriber) {
|
getSearchLabel: function getSearchLabel(segment, subscriber) {
|
||||||
let label = '';
|
let label = '';
|
||||||
|
Reference in New Issue
Block a user