@gizwits/vantui
Version:
机智云组件库
161 lines • 6.99 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 = ["name", "disabled", "checkedColor", "labelPosition", "labelDisabled", "shape", "iconSize", "renderIcon", "style", "className", "children"];
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 { useState, useEffect, useCallback, useContext } from 'react';
import { View } from '@tarojs/components';
import * as utils from '../wxs/utils';
import VanIcon from '../icon/index';
import CheckboxGroupContext from '../checkbox-group/context';
import { isEmptyObject } from '../utils/type';
import * as computed from './wxs';
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
export function Checkbox(props) {
var _useState = useState({
value: undefined,
parentDisabled: false,
direction: 'vertical'
}),
_useState2 = _slicedToArray(_useState, 2),
state = _useState2[0],
setState = _useState2[1];
var name = props.name,
disabled = props.disabled,
_props$checkedColor = props.checkedColor,
checkedColor = _props$checkedColor === void 0 ? '#1989fa' : _props$checkedColor,
_props$labelPosition = props.labelPosition,
labelPosition = _props$labelPosition === void 0 ? 'right' : _props$labelPosition,
labelDisabled = props.labelDisabled,
_props$shape = props.shape,
shape = _props$shape === void 0 ? 'round' : _props$shape,
_props$iconSize = props.iconSize,
iconSize = _props$iconSize === void 0 ? '20px' : _props$iconSize,
renderIcon = props.renderIcon,
style = props.style,
className = props.className,
children = props.children,
others = _objectWithoutProperties(props, _excluded);
var parentData = useContext(CheckboxGroupContext);
var onChange = useCallback(function (event) {
var _props$onChange;
if (parentData.onChange) {
parentData.onChange(event);
return;
}
props === null || props === void 0 ? void 0 : (_props$onChange = props.onChange) === null || _props$onChange === void 0 ? void 0 : _props$onChange.call(props, event);
},
// eslint-disable-next-line
[parentData.onChange, props.onChange]);
useEffect(function () {
setState(function (state) {
var value = props.value;
return _objectSpread(_objectSpread({}, state), {}, {
value: value
});
});
}, [props.value]);
useEffect(function () {
if (!isEmptyObject(parentData)) {
var parentValue = parentData.value,
direction = parentData.direction,
parentDisabled = parentData.disabled;
var value = (parentValue === null || parentValue === void 0 ? void 0 : parentValue.indexOf("".concat(props.name))) > -1;
setState(function (state) {
return _objectSpread(_objectSpread({}, state), {}, {
value: value,
direction: direction,
parentDisabled: parentDisabled
});
});
}
}, [props, parentData]);
var setParentValue = useCallback(function (parent, event) {
var value = event.detail;
var max = parent.max,
parentValue_ = parent.value;
var parentValue = parentValue_ ? [].concat(parentValue_) : parentValue_;
if (value) {
if (max && parentValue.length >= max) {
return;
}
if (parentValue.indexOf(name) === -1) {
parentValue.push(name);
event.detail = parentValue;
onChange === null || onChange === void 0 ? void 0 : onChange(event);
}
} else {
var index = parentValue.indexOf(name);
if (index !== -1) {
parentValue.splice(index, 1);
event.detail = parentValue;
onChange === null || onChange === void 0 ? void 0 : onChange(event);
}
}
}, [name, onChange]);
var emitChange = useCallback(function (event) {
if (!isEmptyObject(parentData)) {
setParentValue(parentData, event);
} else {
onChange === null || onChange === void 0 ? void 0 : onChange(event);
}
}, [parentData, onChange, setParentValue]);
var toggle = useCallback(function (event) {
if (!disabled && !state.parentDisabled) {
Object.defineProperty(event, 'detail', {
value: !state.value,
writable: true
});
emitChange(event);
}
}, [disabled, emitChange, state.parentDisabled, state.value]);
var onClickLabel = useCallback(function (event) {
if (!disabled && !labelDisabled && !state.parentDisabled) {
Object.defineProperty(event, 'detail', {
value: !state.value,
writable: true
});
emitChange(event);
}
}, [disabled, emitChange, labelDisabled, state.parentDisabled, state.value]);
return /*#__PURE__*/_jsxs(View, _objectSpread(_objectSpread({
className: utils.bem('checkbox', [{
horizontal: state.direction === 'horizontal'
}]) + " ".concat(className),
style: style
}, others), {}, {
children: [labelPosition === 'left' && /*#__PURE__*/_jsx(View, {
className: 'label-class ' + utils.bem('checkbox__label', [labelPosition, {
disabled: disabled || state.parentDisabled
}]),
onClick: onClickLabel,
children: children
}), /*#__PURE__*/_jsx(View, {
className: "van-checkbox__icon-wrap",
onClick: toggle,
children: renderIcon || /*#__PURE__*/_jsx(VanIcon, {
name: "success",
className: utils.bem('checkbox__icon', [shape, {
disabled: disabled || state.parentDisabled,
checked: state.value
}]) + ' icon-class',
style: computed.iconStyle({
checkedColor: checkedColor,
value: state.value,
disabled: disabled,
parentDisabled: state.parentDisabled,
iconSize: iconSize
}) + ';line-height:1.25em;'
})
}), labelPosition === 'right' && /*#__PURE__*/_jsx(View, {
className: 'label-class ' + utils.bem('checkbox__label', [labelPosition, {
disabled: disabled || state.parentDisabled
}]),
onClick: onClickLabel,
children: children
})]
}));
}
export default Checkbox;