@audira/carbon-react-native
Version:
Build React Native apps with component and shared patterns using Carbon
87 lines (86 loc) • 2.99 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Item = 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("../collapsible/index.js");
var _Header = require("./_Header.js");
var _context = require("./_context.js");
var _motion = require("./_motion.js");
var _jsxRuntime = require("react/jsx-runtime");
const Item = exports.Item = /*#__PURE__*/(0, _react.forwardRef)(function Item({
defaultOpen,
open: openProp,
title,
children,
style: styleProp,
onPressHeader,
headerProps,
...props
}, forwardedRef) {
const globalConfigContext = (0, _react.useContext)(_index.GlobalConfigContext),
accordionContext = (0, _react.useContext)(_context.Context),
ref = (0, _react.useRef)({
allowOnChangeEffect: false,
open: !!defaultOpen
}),
viewRef = (0, _react.useRef)(null),
[openSelf, setOpenSelf] = (0, _react.useState)(ref.current.open),
controlled = typeof openProp !== 'undefined',
open = controlled ? !!openProp : openSelf,
pressHandler = (0, _react.useCallback)(event => {
onPressHeader?.(event);
if (!controlled) {
ref.current.allowOnChangeEffect = true;
setOpenSelf(state => !state);
}
}, [controlled, onPressHeader]);
(0, _react.useImperativeHandle)(forwardedRef, () => {
return Object.assign(viewRef.current ?? {}, {
get open() {
return ref.current.open;
},
setOpen(value) {
if (!controlled) {
if (typeof value === 'boolean') {
ref.current.open = value;
} else {
ref.current.open = value(ref.current.open);
}
setOpenSelf(ref.current.open);
}
}
});
}, [controlled]);
return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
...props,
style: styleProp,
children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_Header.Header, {
...headerProps,
open: open,
size: accordionContext.size,
text: title,
flushAlignment: accordionContext.flushAlignment,
onPress: pressHandler
}), /*#__PURE__*/(0, _jsxRuntime.jsx)(_index3.Collapsible, {
motion: _motion.Motion,
open: open,
dir: globalConfigContext.rtl ? 'rtl' : undefined,
contentContainerStyle: [globalConfigContext.rtl ? _index2.CommonStyleSheet.rtl : undefined, accordionContext.collapsibleContentContainerStyle, style.panel],
children: children
})]
});
});
const style = _reactNative.StyleSheet.create({
panel: {
paddingTop: _carbonReactNativeElements.Spacing.spacing_03,
paddingBottom: _carbonReactNativeElements.Spacing.spacing_06,
paddingLeft: _carbonReactNativeElements.Spacing.spacing_05
}
});
//# sourceMappingURL=_Item.js.map