@fruits-chain/react-native-xiaoshu
Version:
🌈 React Native UI library
66 lines (60 loc) • 1.95 kB
JavaScript
import isNil from 'lodash/isNil';
import React, { memo } from 'react';
import { View, Text } from 'react-native';
import { useControllableValue } from '../hooks';
import Theme from '../theme';
import CheckboxIcon from './checkbox-icon';
import { varCreator, styleCreator } from './style';
function Checkbox(_ref) {
let {
labelTextStyle,
iconStyle,
activeValue = true,
inactiveValue = false,
label,
labelDisabled = false,
labelPosition = 'right',
iconSize = 20,
renderIcon,
disabled,
activeColor,
style,
children,
...restProps
} = _ref;
if (disabled) {
labelDisabled = disabled;
}
const [value, onChange] = useControllableValue(restProps, {
defaultValue: inactiveValue
});
const TOKENS = Theme.useThemeTokens();
const CV = Theme.createVar(TOKENS, varCreator);
const STYLES = Theme.createStyle(CV, styleCreator);
const active = value === activeValue;
const onChangeValue = () => {
const newValue = active ? inactiveValue : activeValue;
onChange(newValue);
};
const labelJSX = !isNil(label) ? /*#__PURE__*/React.createElement(Text, {
suppressHighlighting: true,
style: [STYLES.label, {
[labelPosition === 'left' ? 'marginRight' : 'marginLeft']: CV.checkbox_label_margin
}, disabled ? STYLES.label_disabled : null, labelTextStyle],
onPress: labelDisabled ? undefined : onChangeValue
}, label) : children;
const iconProps = {
style: iconStyle,
active,
activeColor,
disabled,
size: iconSize,
onPress: onChangeValue
};
const iconJSX = renderIcon ? renderIcon(iconProps) : /*#__PURE__*/React.createElement(CheckboxIcon, iconProps);
return /*#__PURE__*/React.createElement(View, {
style: [STYLES.checkbox, style]
}, labelPosition === 'left' ? labelJSX : null, iconJSX, labelPosition === 'right' ? labelJSX : null);
}
export default /*#__PURE__*/memo(Checkbox);
//# sourceMappingURL=checkbox.js.map