@razorpay/blade
Version:
The Design System that powers Razorpay
28 lines (25 loc) • 4.4 kB
JavaScript
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import _objectWithoutProperties from '@babel/runtime/helpers/objectWithoutProperties';
import { forwardRef, useState, useRef, useCallback, useMemo, Children } from 'react';
import { CollapsibleContext } from './CollapsibleContext.js';
import { MAX_WIDTH, MAX_WIDTH_NO_RESTRICTIONS } from './styles.native.js';
import { componentIds } from './componentIds.js';
import { BaseBox } from '../Box/BaseBox/BaseBox.native.js';
import { getStyledProps } from '../Box/styledProps/getStyledProps.js';
import 'react-native';
import '../../tokens/global/typography.js';
import '../../tokens/global/motion.js';
import { size } from '../../tokens/global/size.js';
import { throwBladeError } from '../../utils/logger/logger.js';
import { makeSize } from '../../utils/makeSize/makeSize.js';
import '../BladeProvider/useTheme.js';
import { metaAttribute } from '../../utils/metaAttribute/metaAttribute.native.js';
import { MetaConstants } from '../../utils/metaAttribute/metaConstants.js';
import { useId } from '../../utils/useId.js';
import { isValidAllowedChildren } from '../../utils/isValidAllowedChildren/isValidAllowedChildren.js';
import { makeAnalyticsAttribute } from '../../utils/makeAnalyticsAttribute/makeAnalyticsAttribute.js';
import { assignWithoutSideEffects } from '../../utils/assignWithoutSideEffects/assignWithoutSideEffects.js';
import { jsx } from 'react/jsx-runtime';
var _excluded=["children","direction","defaultIsExpanded","isExpanded","onExpandChange","testID","_shouldApplyWidthRestrictions","_dangerouslyDisableValidations"];var MIN_WIDTH=makeSize(size[200]);var _Collapsible=function _Collapsible(_ref,ref){var children=_ref.children,_ref$direction=_ref.direction,direction=_ref$direction===void 0?'bottom':_ref$direction,_ref$defaultIsExpande=_ref.defaultIsExpanded,defaultIsExpanded=_ref$defaultIsExpande===void 0?false:_ref$defaultIsExpande,isExpanded=_ref.isExpanded,onExpandChange=_ref.onExpandChange,testID=_ref.testID,_ref$_shouldApplyWidt=_ref._shouldApplyWidthRestrictions,_shouldApplyWidthRestrictions=_ref$_shouldApplyWidt===void 0?true:_ref$_shouldApplyWidt,_ref$_dangerouslyDisa=_ref._dangerouslyDisableValidations,_dangerouslyDisableValidations=_ref$_dangerouslyDisa===void 0?false:_ref$_dangerouslyDisa,rest=_objectWithoutProperties(_ref,_excluded);var _useState=useState(isExpanded!=null?isExpanded:defaultIsExpanded),_useState2=_slicedToArray(_useState,2),isBodyExpanded=_useState2[0],setIsBodyExpanded=_useState2[1];var collapsibleBodyId=useId(MetaConstants.CollapsibleBody);var initialDefaultExpanded=useRef(Boolean(defaultIsExpanded||isExpanded));var handleExpandChange=useCallback(function(nextIsExpanded){if(typeof isExpanded!=='undefined'){onExpandChange==null?void 0:onExpandChange({isExpanded:nextIsExpanded});}else {setIsBodyExpanded(nextIsExpanded);onExpandChange==null?void 0:onExpandChange({isExpanded:nextIsExpanded});}},[onExpandChange,isExpanded]);var contextValue=useMemo(function(){return {isExpanded:isExpanded!=null?isExpanded:isBodyExpanded,onExpandChange:handleExpandChange,defaultIsExpanded:initialDefaultExpanded.current,direction:direction,collapsibleBodyId:collapsibleBodyId};},[isBodyExpanded,direction,handleExpandChange,isExpanded,collapsibleBodyId]);if(__DEV__){Children.forEach(children,function(child){if(!(isValidAllowedChildren(child,componentIds.CollapsibleBody)||isValidAllowedChildren(child,componentIds.CollapsibleButton)||isValidAllowedChildren(child,componentIds.CollapsibleLink))&&!_dangerouslyDisableValidations){throwBladeError({message:`only the following are supported as valid children: CollapsibleBody, CollapsibleButton, CollapsibleLink`,moduleName:'Collapsible'});}});}return jsx(CollapsibleContext.Provider,{value:contextValue,children:jsx(BaseBox,Object.assign({ref:ref},metaAttribute({name:MetaConstants.Collapsible,testID:testID}),getStyledProps(rest),makeAnalyticsAttribute(rest),{children:jsx(BaseBox,{display:"flex",flexDirection:direction==='bottom'?'column':'column-reverse',alignItems:"flex-start",minWidth:_shouldApplyWidthRestrictions?MIN_WIDTH:makeSize(size[0]),maxWidth:_shouldApplyWidthRestrictions?MAX_WIDTH:MAX_WIDTH_NO_RESTRICTIONS,children:children})}))});};var Collapsible=assignWithoutSideEffects(forwardRef(_Collapsible),{componentId:componentIds.Collapsible});
export { Collapsible };
//# sourceMappingURL=Collapsible.js.map