UNPKG

@audira/carbon-react-native

Version:

Build React Native apps with component and shared patterns using Carbon

116 lines (115 loc) 3.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Checkbox = void 0; var _react = require("react"); var _reactNative = require("react-native"); var _carbonReactNativeElements = require("@audira/carbon-react-native-elements"); var _index = require("../../_internal/contexts/index.js"); var _index2 = require("../../_internal/style-sheets/index.js"); var _index3 = require("../../carbon-style-sheet/index.js"); var _index4 = require("../../contexts/index.js"); var _index5 = require("../checkbox-input/index.js"); var _index6 = require("../form-label/index.js"); var _jsxRuntime = require("react/jsx-runtime"); const Checkbox = exports.Checkbox = /*#__PURE__*/(0, _react.forwardRef)(function Checkbox({ defaultValue, value, interactiveState = 'normal', label, role = 'checkbox', 'aria-label': ariaLabel, onChange, onPress, checkboxInputProps, formLabelProps, pressableProps, style, dir, ...props }, forwardedRef) { (0, _react.useContext)(_index4.ThemeContext); const globalConfigContext = (0, _react.useContext)(_index.GlobalConfigContext), checkboxInputRef = (0, _react.useRef)(null), viewRef = (0, _react.useRef)(null), pressHandler = (0, _react.useCallback)(event => { onPress?.(event); if (checkboxInputRef.current) { checkboxInputRef.current.setValue(checkboxInputRef.current.value === null ? true : !checkboxInputRef.current.value); } }, [onPress]); (0, _react.useImperativeHandle)(forwardedRef, () => { return Object.assign(viewRef.current ?? {}, { get value() { return checkboxInputRef.current.value; }, setValue(value_) { checkboxInputRef.current?.setValue(value_); } }); }, []); return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, { ...props, "aria-label": ariaLabel || label, dir: dir ?? globalConfigContext.rtl ? 'rtl' : undefined, style: [_index2.FlexStyleSheet.flex_row, globalConfigContext.rtl ? _index2.CommonStyleSheet.rtl : undefined, style], ref: viewRef, children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Pressable, { ...pressableProps, role: role, disabled: interactiveState === 'disabled', "aria-label": pressableProps?.['aria-label'] ?? label, onPress: pressHandler, style: [_index2.CommonStyleSheet.absolute, _index2.CommonStyleSheet.w_full, _index2.CommonStyleSheet.h_full, baseStyle.pressable, pressableProps?.style] }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_index5.CheckboxInput, { ...checkboxInputProps, role: "none", defaultValue: defaultValue, value: value, interactiveState: interactiveState, onChange: onChange, onPress: onPress, style: [baseStyle.checkboxInput, checkboxInputProps?.style], ref: checkboxInputRef }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_index6.FormLabel, { ...formLabelProps, label: label, textProps: { ...formLabelProps?.textProps, type: formLabelProps?.textProps?.type || 'body_compact_01', style: [textColorStyle[interactiveState], formLabelProps?.textProps?.style] }, style: [_index2.FlexStyleSheet.flex_1, baseStyle.label, formLabelProps?.style] })] }); }); const baseStyle = _reactNative.StyleSheet.create({ pressable: { zIndex: 1 }, checkboxInput: { zIndex: 2 }, label: { marginStart: _carbonReactNativeElements.Spacing.spacing_03 } }), textColorStyle = _index3.CarbonStyleSheet.create({ normal: { color: _index3.CarbonStyleSheet.color.text_primary }, disabled: { color: _index3.CarbonStyleSheet.color.text_disabled }, error: { color: _index3.CarbonStyleSheet.color.text_primary }, read_only: { color: _index3.CarbonStyleSheet.color.text_primary }, warning: { color: _index3.CarbonStyleSheet.color.text_primary } }); //# sourceMappingURL=Checkbox.js.map