yylib-quick-mobile
Version:
yylib-quick-mobile
172 lines (136 loc) • 7.42 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
var _list = require('antd-mobile/lib/list');
var _list2 = _interopRequireDefault(_list);
var _switch = require('antd-mobile/lib/switch');
var _switch2 = _interopRequireDefault(_switch);
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _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; }; }();
require('antd-mobile/lib/list/style/css');
require('antd-mobile/lib/switch/style/css');
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _FunctionUtil = require('../../utils/FunctionUtil');
var _classnames = require('classnames');
var _classnames2 = _interopRequireDefault(_classnames);
var _YYIcon = require('../icon/YYIcon');
var _YYIcon2 = _interopRequireDefault(_YYIcon);
require('./YYSwitch.less');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _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; }
function _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; }
var YYSwitch = function (_React$Component) {
_inherits(YYSwitch, _React$Component);
function YYSwitch(props) {
_classCallCheck(this, YYSwitch);
var _this = _possibleConstructorReturn(this, (YYSwitch.__proto__ || Object.getPrototypeOf(YYSwitch)).call(this, props));
_this.onChange = function (checked) {
var fields = {};
fields[_this.props.field] = {
dirty: true,
errors: undefined,
name: _this.props.field,
touched: true,
validating: true,
value: checked
};
_this.setState({
checked: checked
});
_this.props.form.setFields(fields);
if ((0, _FunctionUtil.isFunction)(_this.props.onChange)) _this.props.onChange(checked);
};
_this.onClick = function (checked) {
_this.setState({
checked: checked
});
if ((0, _FunctionUtil.isFunction)(_this.props.onClick)) _this.props.onClick(checked);
};
_this.componentWillReceiveProps = function (nextProps) {
var getFieldValue = _this.props.form.getFieldValue;
var field = _this.props.field;
var valueField = getFieldValue(field);
_this.setState({
checked: Boolean(valueField)
});
};
_this.state = {
checked: props.checked
};
return _this;
}
_createClass(YYSwitch, [{
key: 'render',
value: function render() {
var _props = this.props,
field = _props.field,
form = _props.form,
label = _props.label,
checkedText = _props.checkedText,
uncheckedText = _props.uncheckedText,
disabled = _props.disabled,
onClick = _props.onClick,
color = _props.color,
platform = _props.platform,
showIcon = _props.showIcon,
icon = _props.icon,
iconColor = _props.iconColor,
required = _props.required,
visible = _props.visible,
RunInDesign = _props.RunInDesign,
restProps = _objectWithoutProperties(_props, ['field', 'form', 'label', 'checkedText', 'uncheckedText', 'disabled', 'onClick', 'color', 'platform', 'showIcon', 'icon', 'iconColor', 'required', 'visible', 'RunInDesign']);
var getFieldProps = form ? form.getFieldProps : null;
var requiredMsg = '必填项' + label + '未填写';
var wrapClz = (0, _classnames2.default)('yy-switch', !visible && 'hidden', this.props.className);
var checked = this.state.checked;
return _react2.default.createElement(
_list2.default.Item,
_extends({}, restProps, {
className: wrapClz,
extra: _react2.default.createElement(_switch2.default, _extends({}, (0, _FunctionUtil.isFunction)(getFieldProps) ? getFieldProps(field, {
initialValue: checked,
rules: [{ required: required, message: requiredMsg }],
valuePropName: 'checked'
}) : null, {
disabled: disabled,
color: color,
platform: platform,
onClick: this.onClick,
name: field })) }),
showIcon && _react2.default.createElement(_YYIcon2.default, { type: icon, color: iconColor, style: { float: 'left', paddingRight: '8px' }, size: 'xs' }),
required ? _react2.default.createElement(
'span',
{ className: 'yy-label-required' },
label
) : label,
_react2.default.createElement(
'div',
{ style: { position: 'absolute', right: '23vw', display: 'inline-block', fontSize: '16px', color: '#7D8291' } },
checked ? checkedText : uncheckedText
)
);
}
}]);
return YYSwitch;
}(_react2.default.Component);
YYSwitch.defaultProps = {
field: 'switch',
label: '滑动开关',
checked: false,
checkedText: '',
uncheckedText: '',
disabled: false,
color: '#3BC1FF',
platform: 'ios',
showIcon: false,
icon: "wage",
iconColor: "red",
visible: true,
required: false,
RunInDesign: false
};
exports.default = YYSwitch;
;