UNPKG

6.24 kBJavaScriptView Raw
1var _class, _temp;
2
3function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
4
5function _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; }
6
7function _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; }
8
9import React, { Component } from 'react';
10import PropTypes from 'prop-types';
11import { createPortal } from 'react-dom';
12import Instance from './instance.js';
13
14if (!window._seeds_lang) window._seeds_lang = {}; // 国际化数据
15
16var Picker = (_temp = _class = function (_Component) {
17 _inherits(Picker, _Component);
18
19 function Picker(props) {
20 _classCallCheck(this, Picker);
21
22 var _this = _possibleConstructorReturn(this, _Component.call(this, props));
23
24 _this.componentDidMount = function () {
25 _this.initInstance();
26 };
27
28 _this.shouldComponentUpdate = function (nextProps) {
29 if (nextProps.show === _this.props.show) return false;
30 return true;
31 };
32
33 _this.componentDidUpdate = function () {
34 if (_this.instance) {
35 if (_this.props.show) {
36 _this.setDefault();
37 _this.instance.show();
38 } else _this.instance.hide();
39 } else {
40 if (_this.props.list.length > 0) {
41 _this.initInstance();
42 }
43 }
44 };
45
46 _this.setDefault = function () {
47 var _this$props = _this.props,
48 valueForKey = _this$props.valueForKey,
49 list = _this$props.list;
50
51 var key = valueForKey || '';
52 if (!key) {
53 var defaultOpt = _this.getDefaults();
54 if (defaultOpt && defaultOpt.key) key = defaultOpt.key;
55 }
56 _this.instance.clearSlots();
57 _this.instance.addSlot(list, key || '', _this.props.slotClassName); // 添加列,参数:数据,默认key,样式(lock样式为锁定列)
58 };
59
60 _this.getDefaults = function () {
61 var _this$props2 = _this.props,
62 list = _this$props2.list,
63 valueForKey = _this$props2.valueForKey,
64 value = _this$props2.value;
65
66 if (!valueForKey && !value) {
67 if (list && list[0]) return list[0];
68 return [{ key: '', value: '' }];
69 }
70 var values = list.filter(function (item) {
71 if (valueForKey) {
72 if (valueForKey === item.key) return true;
73 } else if (value) {
74 if (item.value === value) return true;
75 }
76 return false;
77 });
78 return values[0];
79 };
80
81 _this.initInstance = function () {
82 var list = _this.props.list;
83
84 if (!list || list.length === 0 || _this.instance) return;
85 // render数据
86 var instance = new Instance({
87 mask: _this.$el,
88 onClickMask: function onClickMask(e) {
89 if (_this.props.onClickMask) _this.props.onClickMask(e);
90 },
91 onClickCancel: function onClickCancel(e) {
92 // e.hide()
93 if (_this.props.onClickCancel) _this.props.onClickCancel(e);
94 },
95 onClickSubmit: function onClickSubmit(e) {
96 // e.hide()
97 if (_this.props.onClickSubmit) _this.props.onClickSubmit(e);
98 },
99 onHid: function onHid(e) {}
100 });
101 // 默认项
102 var defaultOpt = _this.getDefaults();
103 var key = '';
104 if (defaultOpt && defaultOpt.key) key = defaultOpt.key;
105 instance.addSlot(list, key, _this.props.slotClassName);
106 if (_this.props.show && instance) {
107 instance.show();
108 }
109 _this.instance = instance;
110 };
111
112 return _this;
113 }
114
115 Picker.prototype.render = function render() {
116 var _this2 = this;
117
118 var _props = this.props,
119 maskClassName = _props.maskClassName,
120 maskStyle = _props.maskStyle,
121 className = _props.className,
122 style = _props.style;
123
124 return createPortal(React.createElement(
125 'div',
126 { className: 'mask picker-mask' + (maskClassName ? ' ' + maskClassName : ''), style: maskStyle, ref: function ref(el) {
127 _this2.$el = el;
128 } },
129 React.createElement(
130 'div',
131 { className: 'picker' + (className ? ' ' + className : ''), style: style },
132 React.createElement(
133 'div',
134 { className: 'picker-header' },
135 React.createElement(
136 'a',
137 { className: 'picker-cancel' },
138 window._seeds_lang['cancel'] || '取消'
139 ),
140 React.createElement(
141 'a',
142 { className: 'picker-submit' },
143 window._seeds_lang['finish'] || '完成'
144 )
145 ),
146 React.createElement(
147 'div',
148 { className: 'picker-wrapper' },
149 React.createElement(
150 'div',
151 { className: 'picker-layer' },
152 React.createElement('div', { className: 'picker-layer-frame' })
153 ),
154 React.createElement('div', { className: 'picker-slotbox' })
155 )
156 )
157 ), this.props.portal || document.getElementById('root'));
158 };
159
160 return Picker;
161}(Component), _class.defaultProps = {
162 slotClassName: 'text-center'
163}, _temp);
164export { Picker as default };
165Picker.propTypes = process.env.NODE_ENV !== "production" ? {
166 portal: PropTypes.object,
167 list: PropTypes.array, // [{key: '', value: ''}]
168
169 maskClassName: PropTypes.string,
170 maskStyle: PropTypes.object,
171 className: PropTypes.string,
172 style: PropTypes.object,
173
174 slotClassName: PropTypes.string,
175 value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
176 valueForKey: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
177 show: PropTypes.bool,
178 onClickMask: PropTypes.func,
179 onClickCancel: PropTypes.func,
180 onClickSubmit: PropTypes.func
181} : {};
\No newline at end of file