@antmjs/vantui
Version:
一套适用于Taro3及React的vantui组件库
77 lines • 4.1 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
var _excluded = ["checked", "loading", "disabled", "activeColor", "inactiveColor", "size", "activeValue", "inactiveValue", "onChange", "style", "className"];
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
import { useCallback, useState } from 'react';
import { View } from '@tarojs/components';
import * as utils from '../wxs/utils';
import VanLoading from '../loading/index';
import { get } from '../default-props';
import * as computed from './wxs';
import { jsx as _jsx } from "react/jsx-runtime";
export function Switch(props) {
var _useState = useState(get().Switch),
_useState2 = _slicedToArray(_useState, 1),
d = _useState2[0];
var _d$props = _objectSpread(_objectSpread({}, d), props),
_d$props$checked = _d$props.checked,
checked = _d$props$checked === void 0 ? false : _d$props$checked,
_d$props$loading = _d$props.loading,
loading = _d$props$loading === void 0 ? false : _d$props$loading,
_d$props$disabled = _d$props.disabled,
disabled = _d$props$disabled === void 0 ? false : _d$props$disabled,
_d$props$activeColor = _d$props.activeColor,
activeColor = _d$props$activeColor === void 0 ? '' : _d$props$activeColor,
_d$props$inactiveColo = _d$props.inactiveColor,
inactiveColor = _d$props$inactiveColo === void 0 ? '' : _d$props$inactiveColo,
_d$props$size = _d$props.size,
size = _d$props$size === void 0 ? '' : _d$props$size,
_d$props$activeValue = _d$props.activeValue,
activeValue = _d$props$activeValue === void 0 ? true : _d$props$activeValue,
_d$props$inactiveValu = _d$props.inactiveValue,
inactiveValue = _d$props$inactiveValu === void 0 ? false : _d$props$inactiveValu,
onChange = _d$props.onChange,
style = _d$props.style,
className = _d$props.className,
others = _objectWithoutProperties(_d$props, _excluded);
var onClick = useCallback(function (event) {
if (disabled || loading) {
return;
}
var value = checked === activeValue ? inactiveValue : activeValue;
Object.defineProperty(event, 'detail', {
value: value
});
onChange === null || onChange === void 0 ? void 0 : onChange(event);
}, [activeValue, checked, disabled, inactiveValue, loading, onChange]);
return /*#__PURE__*/_jsx(View, _objectSpread(_objectSpread({
className: utils.bem('switch', {
on: checked === activeValue,
disabled: disabled
}) + " ".concat(className),
style: utils.style([computed.rootStyle({
size: size,
checked: checked,
activeColor: activeColor,
inactiveColor: inactiveColor,
activeValue: activeValue
}), style])
}, others), {}, {
onClick: onClick,
children: /*#__PURE__*/_jsx(View, {
className: "van-switch__node node-class",
children: loading && /*#__PURE__*/_jsx(VanLoading, {
color: computed.loadingColor({
checked: checked,
activeColor: activeColor,
inactiveColor: inactiveColor,
activeValue: activeValue
}),
className: "van-switch__loading"
})
})
}));
}
export default Switch;