Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

rc-switch

Package Overview
Dependencies
Maintainers
3
Versions
34
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rc-switch - npm Package Compare versions

Comparing version 1.4.4 to 1.5.1

es/index.js

5

HISTORY.md

@@ -0,1 +1,6 @@

## 1.5.0
- Add `onClick` and `tabIndex`.
- Add `es` library and module field in package.json.
## 1.4.0

@@ -2,0 +7,0 @@

210

lib/Switch.js

@@ -7,26 +7,40 @@ 'use strict';

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 _extends2 = require('babel-runtime/helpers/extends');
var _react = require('react');
var _extends3 = _interopRequireDefault(_extends2);
var _react2 = _interopRequireDefault(_react);
var _defineProperty2 = require('babel-runtime/helpers/defineProperty');
var _propTypes = require('prop-types');
var _defineProperty3 = _interopRequireDefault(_defineProperty2);
var _propTypes2 = _interopRequireDefault(_propTypes);
var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);
function _defaults(obj, defaults) { var keys = Object.getOwnPropertyNames(defaults); for (var i = 0; i < keys.length; i++) { var key = keys[i]; var value = Object.getOwnPropertyDescriptor(defaults, key); if (value && value.configurable && obj[key] === undefined) { Object.defineProperty(obj, key, value); } } return obj; }
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
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; }
var _createClass2 = require('babel-runtime/helpers/createClass');
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var _createClass3 = _interopRequireDefault(_createClass2);
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; }
var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
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) : _defaults(subClass, superClass); }
var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
var _inherits2 = require('babel-runtime/helpers/inherits');
var _inherits3 = _interopRequireDefault(_inherits2);
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _propTypes = require('prop-types');
var _propTypes2 = _interopRequireDefault(_propTypes);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
var classNames = require('classnames');

@@ -37,9 +51,11 @@

var Switch = function (_Component) {
_inherits(Switch, _Component);
(0, _inherits3['default'])(Switch, _Component);
function Switch(props) {
_classCallCheck(this, Switch);
(0, _classCallCheck3['default'])(this, Switch);
var _this = _possibleConstructorReturn(this, _Component.call(this, props));
var _this = (0, _possibleConstructorReturn3['default'])(this, (Switch.__proto__ || Object.getPrototypeOf(Switch)).call(this, props));
_initialiseProps.call(_this);
var checked = false;

@@ -55,89 +71,108 @@ if ('checked' in props) {

Switch.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
if ('checked' in nextProps) {
this.setState({
checked: !!nextProps.checked
});
(0, _createClass3['default'])(Switch, [{
key: 'componentWillReceiveProps',
value: function componentWillReceiveProps(nextProps) {
if ('checked' in nextProps) {
this.setState({
checked: !!nextProps.checked
});
}
}
};
}, {
key: 'setChecked',
value: function setChecked(checked) {
if (!('checked' in this.props)) {
this.setState({
checked: checked
});
}
this.props.onChange(checked);
}
Switch.prototype.setChecked = function setChecked(checked) {
if (!('checked' in this.props)) {
this.setState({
checked: checked
});
// Handle auto focus when click switch in Chrome
}, {
key: 'render',
value: function render() {
var _classNames;
var _props = this.props,
className = _props.className,
prefixCls = _props.prefixCls,
disabled = _props.disabled,
checkedChildren = _props.checkedChildren,
tabIndex = _props.tabIndex,
unCheckedChildren = _props.unCheckedChildren,
restProps = (0, _objectWithoutProperties3['default'])(_props, ['className', 'prefixCls', 'disabled', 'checkedChildren', 'tabIndex', 'unCheckedChildren']);
var checked = this.state.checked;
var switchTabIndex = disabled ? -1 : tabIndex || 0;
var switchClassName = classNames((_classNames = {}, (0, _defineProperty3['default'])(_classNames, className, !!className), (0, _defineProperty3['default'])(_classNames, prefixCls, true), (0, _defineProperty3['default'])(_classNames, prefixCls + '-checked', checked), (0, _defineProperty3['default'])(_classNames, prefixCls + '-disabled', disabled), _classNames));
return _react2['default'].createElement(
'span',
(0, _extends3['default'])({}, restProps, {
className: switchClassName,
tabIndex: switchTabIndex,
ref: 'node',
onKeyDown: this.handleKeyDown,
onClick: this.toggle,
onMouseUp: this.handleMouseUp
}),
_react2['default'].createElement(
'span',
{ className: prefixCls + '-inner' },
checked ? checkedChildren : unCheckedChildren
)
);
}
this.props.onChange(checked);
};
}]);
return Switch;
}(_react.Component);
Switch.prototype.toggle = function toggle() {
var checked = !this.state.checked;
this.setChecked(checked);
var _initialiseProps = function _initialiseProps() {
var _this2 = this;
this.toggle = function () {
var _props2 = _this2.props,
disabled = _props2.disabled,
onClick = _props2.onClick;
var checked = !_this2.state.checked;
if (!disabled) {
_this2.setChecked(checked);
}
onClick(checked);
};
Switch.prototype.handleKeyDown = function handleKeyDown(e) {
this.handleKeyDown = function (e) {
if (e.keyCode === 37) {
this.setChecked(false);
_this2.setChecked(false);
}
if (e.keyCode === 39) {
this.setChecked(true);
_this2.setChecked(true);
}
};
// Handle auto focus when click switch in Chrome
Switch.prototype.handleMouseUp = function handleMouseUp(e) {
if (this.refs.node) {
this.refs.node.blur();
this.handleMouseUp = function (e) {
if (_this2.refs.node) {
_this2.refs.node.blur();
}
if (this.props.onMouseUp) {
this.props.onMouseUp(e);
if (_this2.props.onMouseUp) {
_this2.props.onMouseUp(e);
}
};
};
Switch.prototype.render = function render() {
var _classNames;
var _props = this.props,
className = _props.className,
prefixCls = _props.prefixCls,
disabled = _props.disabled,
checkedChildren = _props.checkedChildren,
unCheckedChildren = _props.unCheckedChildren,
restProps = _objectWithoutProperties(_props, ['className', 'prefixCls', 'disabled', 'checkedChildren', 'unCheckedChildren']);
var checked = this.state.checked;
var switchClassName = classNames((_classNames = {}, _defineProperty(_classNames, className, !!className), _defineProperty(_classNames, prefixCls, true), _defineProperty(_classNames, prefixCls + '-checked', checked), _defineProperty(_classNames, prefixCls + '-disabled', disabled), _classNames));
return _react2["default"].createElement(
'span',
_extends({}, restProps, {
className: switchClassName,
tabIndex: disabled ? -1 : 0,
ref: 'node',
onKeyDown: this.handleKeyDown.bind(this),
onClick: disabled ? noop : this.toggle.bind(this),
onMouseUp: this.handleMouseUp.bind(this)
}),
_react2["default"].createElement(
'span',
{ className: prefixCls + '-inner' },
checked ? checkedChildren : unCheckedChildren
)
);
};
return Switch;
}(_react.Component);
Switch.propTypes = {
className: _propTypes2["default"].string,
prefixCls: _propTypes2["default"].string,
disabled: _propTypes2["default"].bool,
checkedChildren: _propTypes2["default"].any,
unCheckedChildren: _propTypes2["default"].any,
onChange: _propTypes2["default"].func,
onMouseUp: _propTypes2["default"].func,
checked: _propTypes2["default"].bool,
defaultChecked: _propTypes2["default"].bool
className: _propTypes2['default'].string,
prefixCls: _propTypes2['default'].string,
disabled: _propTypes2['default'].bool,
checkedChildren: _propTypes2['default'].any,
unCheckedChildren: _propTypes2['default'].any,
onChange: _propTypes2['default'].func,
onMouseUp: _propTypes2['default'].func,
onClick: _propTypes2['default'].func,
tabIndex: _propTypes2['default'].number,
checked: _propTypes2['default'].bool,
defaultChecked: _propTypes2['default'].bool
};

@@ -151,6 +186,7 @@

defaultChecked: false,
onChange: noop
onChange: noop,
onClick: noop
};
exports["default"] = Switch;
exports['default'] = Switch;
module.exports = exports['default'];
{
"name": "rc-switch",
"version": "1.4.4",
"version": "1.5.1",
"description": "switch ui component for react",

@@ -12,2 +12,8 @@ "keywords": [

"main": "lib/index",
"module": "./es/index",
"files": [
"assets/*.css",
"es",
"lib"
],
"homepage": "http://github.com/react-component/switch",

@@ -27,20 +33,32 @@ "repository": {

"build": "rc-tools run build",
"compile": "rc-tools run compile --babel-runtime",
"gh-pages": "rc-tools run gh-pages",
"start": "rc-tools run server",
"pub": "rc-tools run pub",
"pub": "rc-tools run pub --babel-runtime",
"lint": "rc-tools run lint",
"lint:fix": "rc-tools run lint --fix",
"karma": "rc-tools run karma",
"saucelabs": "rc-tools run saucelabs",
"test": "rc-tools run test",
"chrome-test": "rc-tools run chrome-test",
"coverage": "rc-tools run coverage"
"test": "jest",
"prepublish": "rc-tools run guard",
"coverage": "jest --coverage && cat ./coverage/lcov.info | coveralls"
},
"jest": {
"collectCoverageFrom": [
"src/**/*"
],
"snapshotSerializers": [
"enzyme-to-json/serializer"
],
"transform": {
"\\.jsx?$": "./node_modules/rc-tools/scripts/jestPreprocessor.js"
}
},
"devDependencies": {
"expect.js": "~0.3.1",
"coveralls": "^2.11.15",
"enzyme": "^2.6.0",
"enzyme-to-json": "^1.5.0",
"jest": "^19.0.2",
"pre-commit": "1.x",
"rc-tools": "5.x",
"rc-tools": "6.x",
"react": "15.x",
"react-addons-test-utils": "15.x",
"react-dom": "~15.x"
"react-dom": "15.x",
"react-test-renderer": "^15.3.2"
},

@@ -51,2 +69,3 @@ "pre-commit": [

"dependencies": {
"babel-runtime": "^6.23.0",
"classnames": "^2.2.1",

@@ -53,0 +72,0 @@ "prop-types": "^15.5.6"

@@ -86,2 +86,14 @@ # rc-switch

</tr>
<tr>
<td>tabIndex</td>
<td>number</td>
<td></td>
<td>tab-index of switch node</td>
</tr>
<tr>
<td>onClick</td>
<td>Function(checked)</td>
<td></td>
<td>called when switch is clicked</td>
</tr>
</tbody>

@@ -88,0 +100,0 @@ </table>

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc