UNPKG

8.64 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
8
9var _templateObject = _taggedTemplateLiteral(['\n font-family: ', ';\n\n .wrapper {\n position: relative;\n padding: 1.125rem 1rem;\n border-radius: 3px;\n border: 1px solid ', ';\n background: ', ';\n box-shadow: 0px 2px 0px 0px rgba(0,0,0,0.16);\n }\n\n .inner-wrapper {\n display: flex;\n flex-wrap: wrap;\n align-items: center;\n }\n\n .tag {\n font-weight: 300;\n font-size: 0.875em;\n text-decoration: none;\n background: ', '\n color: ', '\n padding: 0.85em 1.07em 0.78em 1.07em;\n cursor: pointer;\n transition: all 0.2s ease-in;\n border-radius: 3px;\n box-shadow: 0px 2px 0px 0px rgba(0,0,0,0.20);\n margin-right: 0.25em;\n margin-bottom: 0.25em;\n\n &:last-of-type { margin-right: 0.5em }\n }\n\n .input {\n font-family: ', ';\n font-weight: 300;\n line-height: 1;\n font-size: 1.125em;\n border: 0;\n outline: 0;\n }\n\n .delete {\n display: inline-block;\n margin-left: 0.5em;\n }\n\n .suggestions {\n position: absolute;\n z-index: 99;\n\n ul {\n font-family: ', ';\n font-weight: 300;\n line-height: 1;\n font-size: 1.125em;\n list-style-type: none;\n border: 1px solid ', ';\n border-radius: 3px;\n background: ', ';\n box-shadow: 0px 2px 0px 0px rgba(0,0,0,0.20);\n margin-top: 1em;\n padding: 0;\n }\n\n li {\n display: block;\n padding: 1.25em;\n\n mark {\n background: none;\n font-weight: 400;\n }\n }\n\n li.active {\n background: ', ';\n color: ', ';\n mark { color: ', ' }\n }\n }\n'], ['\n font-family: ', ';\n\n .wrapper {\n position: relative;\n padding: 1.125rem 1rem;\n border-radius: 3px;\n border: 1px solid ', ';\n background: ', ';\n box-shadow: 0px 2px 0px 0px rgba(0,0,0,0.16);\n }\n\n .inner-wrapper {\n display: flex;\n flex-wrap: wrap;\n align-items: center;\n }\n\n .tag {\n font-weight: 300;\n font-size: 0.875em;\n text-decoration: none;\n background: ', '\n color: ', '\n padding: 0.85em 1.07em 0.78em 1.07em;\n cursor: pointer;\n transition: all 0.2s ease-in;\n border-radius: 3px;\n box-shadow: 0px 2px 0px 0px rgba(0,0,0,0.20);\n margin-right: 0.25em;\n margin-bottom: 0.25em;\n\n &:last-of-type { margin-right: 0.5em }\n }\n\n .input {\n font-family: ', ';\n font-weight: 300;\n line-height: 1;\n font-size: 1.125em;\n border: 0;\n outline: 0;\n }\n\n .delete {\n display: inline-block;\n margin-left: 0.5em;\n }\n\n .suggestions {\n position: absolute;\n z-index: 99;\n\n ul {\n font-family: ', ';\n font-weight: 300;\n line-height: 1;\n font-size: 1.125em;\n list-style-type: none;\n border: 1px solid ', ';\n border-radius: 3px;\n background: ', ';\n box-shadow: 0px 2px 0px 0px rgba(0,0,0,0.20);\n margin-top: 1em;\n padding: 0;\n }\n\n li {\n display: block;\n padding: 1.25em;\n\n mark {\n background: none;\n font-weight: 400;\n }\n }\n\n li.active {\n background: ', ';\n color: ', ';\n mark { color: ', ' }\n }\n }\n']);
10
11var _styledComponents = require('styled-components');
12
13var _styledComponents2 = _interopRequireDefault(_styledComponents);
14
15var _react = require('react');
16
17var _react2 = _interopRequireDefault(_react);
18
19var _reactTagInput = require('react-tag-input');
20
21function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
22
23function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
24
25function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
26
27function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
28
29function _taggedTemplateLiteral(strings, raw) { return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }
30
31var Container = _styledComponents2.default.div(_templateObject, function (props) {
32 return props.theme.fontBase;
33}, function (props) {
34 return props.theme.light;
35}, function (props) {
36 return props.theme.blank;
37}, function (props) {
38 return props.theme.grey;
39}, function (props) {
40 return props.theme.blank;
41}, function (props) {
42 return props.theme.fontBase;
43}, function (props) {
44 return props.theme.fontBase;
45}, function (props) {
46 return props.theme.light;
47}, function (props) {
48 return props.theme.blank;
49}, function (props) {
50 return props.theme.dark;
51}, function (props) {
52 return props.theme.blank;
53}, function (props) {
54 return props.theme.blank;
55});
56
57var TagInput = function (_Component) {
58 _inherits(TagInput, _Component);
59
60 function TagInput() {
61 var _ref;
62
63 var _temp, _this, _ret;
64
65 _classCallCheck(this, TagInput);
66
67 for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
68 args[_key] = arguments[_key];
69 }
70
71 return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = TagInput.__proto__ || Object.getPrototypeOf(TagInput)).call.apply(_ref, [this].concat(args))), _this), _this.state = {
72 tags: _this.props.value
73 }, _this.getTags = function () {
74 return _this.props.value ? _this.props.value : _this.state.tags;
75 }, _this.handleDelete = function (index) {
76 var tags = _this.getTags();
77 tags.splice(index, 1);
78 _this.flushChanges(tags);
79 }, _this.handleAddition = function (tag) {
80 var tags = _this.getTags();
81 tags.push({
82 id: tags.length + 1,
83 text: tag
84 });
85 _this.flushChanges(tags);
86 }, _this.handleDrag = function (tag, currentPosition, newPosition) {
87 var tags = _this.getTags();
88 tags.splice(currentPosition, 1);
89 tags.splice(newPosition, 0, tag);
90 _this.flushChanges(tags);
91 }, _this.flushChanges = function (tags) {
92 return _this.props.onChange ? _this.props.onChange(tags) : _this.setState({ tags: tags });
93 }, _temp), _possibleConstructorReturn(_this, _ret);
94 }
95
96 /**
97 * Get the tags from either the value passed as prop or
98 * it's own internal state.
99 *
100 * @return {Array}
101 */
102
103
104 /**
105 * Flush the changes to the tags to either the onChange passed as prop
106 * or it's internal state.
107 *
108 * @param {Array} tags
109 * @return {void}
110 */
111
112
113 _createClass(TagInput, [{
114 key: 'render',
115 value: function render() {
116 return _react2.default.createElement(
117 Container,
118 null,
119 _react2.default.createElement(_reactTagInput.WithContext, {
120 tags: this.getTags(),
121 suggestions: this.props.suggestions,
122 handleDelete: this.handleDelete,
123 handleAddition: this.handleAddition,
124 handleDrag: this.handleDrag,
125 classNames: {
126 tags: 'wrapper',
127 tagInput: 'tagInputClass',
128 tagInputField: 'input',
129 selected: 'inner-wrapper',
130 tag: 'tag',
131 remove: 'delete',
132 suggestions: 'suggestions'
133 }
134 })
135 );
136 }
137 }]);
138
139 return TagInput;
140}(_react.Component);
141
142TagInput.propTypes = {
143 value: _react.PropTypes.array,
144 onChange: _react.PropTypes.func,
145 suggestions: _react.PropTypes.array
146};
147TagInput.defaultProps = {
148 value: [],
149 suggestions: []
150};
151exports.default = TagInput;
\No newline at end of file