UNPKG

@razorpay/blade

Version:

The Design System that powers Razorpay

118 lines (114 loc) 6.52 kB
import _defineProperty from '@babel/runtime/helpers/defineProperty'; import _slicedToArray from '@babel/runtime/helpers/slicedToArray'; import _objectWithoutProperties from '@babel/runtime/helpers/objectWithoutProperties'; import { useState, useRef, useCallback, useMemo, Children, forwardRef } from 'react'; import { CollapsibleContext } from './CollapsibleContext.js'; import { MAX_WIDTH, MAX_WIDTH_NO_RESTRICTIONS } from './styles.web.js'; import { componentIds } from './componentIds.js'; import '../Box/BaseBox/index.js'; import '../Box/styledProps/index.js'; import '../../utils/index.js'; import '../../tokens/global/index.js'; import '../../utils/metaAttribute/index.js'; import { useId } from '../../utils/useId.js'; import '../../utils/isValidAllowedChildren/index.js'; import '../../utils/logger/index.js'; import '../../utils/makeAnalyticsAttribute/index.js'; import '../../utils/assignWithoutSideEffects/index.js'; import { jsx } from 'react/jsx-runtime'; import { makeSize } from '../../utils/makeSize/makeSize.js'; import { size } from '../../tokens/global/size.js'; import { MetaConstants } from '../../utils/metaAttribute/metaConstants.js'; import { isValidAllowedChildren } from '../../utils/isValidAllowedChildren/isValidAllowedChildren.js'; import { throwBladeError } from '../../utils/logger/logger.js'; import { BaseBox } from '../Box/BaseBox/BaseBox.web.js'; import { metaAttribute } from '../../utils/metaAttribute/metaAttribute.web.js'; import { getStyledProps } from '../Box/styledProps/getStyledProps.js'; import { makeAnalyticsAttribute } from '../../utils/makeAnalyticsAttribute/makeAnalyticsAttribute.js'; import { assignWithoutSideEffects } from '../../utils/assignWithoutSideEffects/assignWithoutSideEffects.js'; var _excluded = ["children", "direction", "defaultIsExpanded", "isExpanded", "onExpandChange", "testID", "_shouldApplyWidthRestrictions", "_dangerouslyDisableValidations"]; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } 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 !== void 0 ? isExpanded : defaultIsExpanded), _useState2 = _slicedToArray(_useState, 2), isBodyExpanded = _useState2[0], setIsBodyExpanded = _useState2[1]; var collapsibleBodyId = useId(MetaConstants.CollapsibleBody); /** * Maintain a ref to the initial value of `defaultExpanded || isExpanded` so changing it has no effect. * This will help in determining the correct initial height of collapsing body content */ var initialDefaultExpanded = useRef(Boolean(defaultIsExpanded || isExpanded)); var handleExpandChange = useCallback(function (nextIsExpanded) { if (typeof isExpanded !== 'undefined') { // controlled onExpandChange === null || onExpandChange === void 0 ? void 0 : onExpandChange({ isExpanded: nextIsExpanded }); } else { // uncontrolled setIsBodyExpanded(nextIsExpanded); onExpandChange === null || onExpandChange === void 0 ? void 0 : onExpandChange({ isExpanded: nextIsExpanded }); } }, [onExpandChange, isExpanded]); var contextValue = useMemo(function () { return { // controlled behavior if isExpanded is provided isExpanded: isExpanded !== null && isExpanded !== void 0 ? isExpanded : isBodyExpanded, onExpandChange: handleExpandChange, defaultIsExpanded: initialDefaultExpanded.current, direction: direction, collapsibleBodyId: collapsibleBodyId }; }, [isBodyExpanded, direction, handleExpandChange, isExpanded, collapsibleBodyId]); if (false) { 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 /*#__PURE__*/jsx(CollapsibleContext.Provider, { value: contextValue, children: /*#__PURE__*/jsx(BaseBox, _objectSpread(_objectSpread(_objectSpread(_objectSpread({ ref: ref }, metaAttribute({ name: MetaConstants.Collapsible, testID: testID })), getStyledProps(rest)), makeAnalyticsAttribute(rest)), {}, { children: /*#__PURE__*/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 = /*#__PURE__*/assignWithoutSideEffects( /*#__PURE__*/forwardRef(_Collapsible), { componentId: componentIds.Collapsible }); export { Collapsible }; //# sourceMappingURL=Collapsible.js.map