ukelli-ui
Version:
Base on React's UI lib. Make frontend's dev simpler and faster.
70 lines (69 loc) • 2.69 kB
JavaScript
/* eslint-disable react/no-array-index-key */
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
import React, { PureComponent } from 'react';
import { HasValue } from 'basic-helper';
/**
* 开关
*
* @export
* @class Switch
* @extends {PureComponent}
*/
var Switch = /** @class */ (function (_super) {
__extends(Switch, _super);
function Switch(props) {
var _this = _super.call(this, props) || this;
_this.getValue = function () { return (_this.isControl ? _this.props.checked : _this.state.checked); };
_this.onChange = function (nextValue) {
var onChange = _this.props.onChange;
if (!_this.isControl) {
_this.setState({
checked: nextValue
});
}
onChange && onChange(nextValue);
};
_this.isControl = HasValue(props.checked);
_this.state = {
checked: _this.isControl ? props.checked : props.defaultChecked
};
return _this;
}
Switch.prototype.render = function () {
var _this = this;
var _a = this.props, disabled = _a.disabled, _b = _a.tips, tips = _b === void 0 ? [] : _b, _c = _a.outputs, outputs = _c === void 0 ? [] : _c;
var checked = this.getValue();
var text = tips[checked ? 0 : 1];
var switchBtnGroup = (React.createElement("span", { className: "__switch" + (checked ? ' checked' : '') + (disabled ? ' disabled' : ''), onClick: function (e) {
var nextChecked = !checked;
var emitVal = outputs[nextChecked ? 0 : 1];
if (!disabled) {
_this.onChange(emitVal);
}
} },
React.createElement("span", { className: "switch-btn" + (disabled ? ' disabled' : '') }, text),
React.createElement("span", { className: "indicator" })));
return switchBtnGroup;
};
Switch.defaultProps = {
tips: ['', ''],
// checked: true,
disabled: false,
outputs: [true, false],
defaultChecked: false,
};
return Switch;
}(PureComponent));
export default Switch;