@appbuckets/react-ui
Version:
Just Another React UI Framework
230 lines (223 loc) • 6.75 kB
JavaScript
'use strict';
var tslib = require('tslib');
var React = require('react');
var clsx = require('clsx');
var reactUiCore = require('@appbuckets/react-ui-core');
var customHook = require('../utils/customHook.js');
require('../BucketTheme/BucketTheme.js');
var BucketContext = require('../BucketTheme/BucketContext.js');
var Column = require('../Column/Column.js');
var Collapsable = require('../Collapsable/Collapsable.js');
var Header = require('../Header/Header.js');
var Icon = require('../Icon/Icon.js');
var Row = require('../Row/Row.js');
function _interopDefaultLegacy(e) {
return e && typeof e === 'object' && 'default' in e ? e : { default: e };
}
function _interopNamespace(e) {
if (e && e.__esModule) return e;
var n = Object.create(null);
if (e) {
Object.keys(e).forEach(function (k) {
if (k !== 'default') {
var d = Object.getOwnPropertyDescriptor(e, k);
Object.defineProperty(
n,
k,
d.get
? d
: {
enumerable: true,
get: function () {
return e[k];
},
}
);
}
});
}
n['default'] = e;
return Object.freeze(n);
}
var React__namespace = /*#__PURE__*/ _interopNamespace(React);
var clsx__default = /*#__PURE__*/ _interopDefaultLegacy(clsx);
/* --------
* Component Render
* -------- */
var Accordions = function (receivedProps) {
var props = BucketContext.useWithDefaultProps('accordions', receivedProps);
var _a = customHook.useSharedClassName(props),
className = _a.className,
_b = _a.rest,
userDefinedActiveIndexes = _b.activeIndexes,
allowMultiple = _b.allowMultiple,
userDefinedDefaultActiveIndexes = _b.defaultActiveIndexes,
icon = _b.icon,
iconRotation = _b.iconRotation,
onSectionChange = _b.onSectionChange,
onSectionClose = _b.onSectionClose,
onSectionOpen = _b.onSectionOpen,
sections = _b.sections,
rest = tslib.__rest(_b, [
'activeIndexes',
'allowMultiple',
'defaultActiveIndexes',
'icon',
'iconRotation',
'onSectionChange',
'onSectionClose',
'onSectionOpen',
'sections',
]);
/** Control the Active Section */
var _c = tslib.__read(
reactUiCore.useAutoControlledValue([], {
prop: userDefinedActiveIndexes,
defaultProp: userDefinedDefaultActiveIndexes,
}),
2
),
activeIndexes = _c[0],
trySetActiveIndexes = _c[1];
/** Get the component element type */
var ElementType = reactUiCore.useElementType(
Accordions,
receivedProps,
props
);
/** Build the element class list */
var classes = clsx__default['default']('accordions', className);
// ----
// Handlers
// ----
var handleSectionOpen = function (index) {
/** Build new Indexes */
var newIndexes = allowMultiple
? tslib.__spreadArray([], tslib.__read(activeIndexes), false)
: [index];
if (allowMultiple && !newIndexes.includes(index)) {
newIndexes.push(index);
}
/** Fire the onOpen Event */
if (typeof onSectionOpen === 'function') {
onSectionOpen(
null,
tslib.__assign(tslib.__assign({}, props), { activeIndexes: newIndexes })
);
}
/** Fire the onChange Event */
if (typeof onSectionChange === 'function') {
onSectionChange(
'open',
tslib.__assign(tslib.__assign({}, props), { activeIndexes: newIndexes })
);
}
/** Try to set new state */
trySetActiveIndexes(newIndexes);
};
var handleSectionClose = function (index) {
/** Build new indexes */
var newIndexes = allowMultiple
? activeIndexes.filter(function (val) {
return val !== index;
})
: [];
/** Fire the onClose Event */
if (typeof onSectionClose === 'function') {
onSectionClose(
null,
tslib.__assign(tslib.__assign({}, props), { activeIndexes: newIndexes })
);
}
/** Fire the onChange Event */
if (typeof onSectionChange === 'function') {
onSectionChange(
'close',
tslib.__assign(tslib.__assign({}, props), { activeIndexes: newIndexes })
);
}
/** Try to set new state */
trySetActiveIndexes(newIndexes);
};
// ----
// Define SubComponent Render
// ----
var renderTrigger = React__namespace.useCallback(
function (trigger, index) {
var headerElement = Header.create(trigger, { autoGenerateKey: false });
var iconElement = Icon.create(icon, {
autoGenerateKey: false,
overrideProps: {
style:
iconRotation !== undefined && activeIndexes.includes(index)
? { transform: 'rotate('.concat(iconRotation, 'deg)') }
: undefined,
},
});
if (!headerElement && !iconElement) {
return undefined;
}
return React__namespace.createElement(
'div',
{ className: 'accordion-trigger' },
React__namespace.createElement(
Row,
{ verticalAlign: 'center' },
React__namespace.createElement(Column, null, headerElement),
iconElement &&
React__namespace.createElement(
Column,
{ width: 'auto' },
iconElement
)
)
);
},
// @ts-ignore
[activeIndexes, icon, iconRotation]
);
var renderSectionsElement = function () {
/** Assert Sections Array exists */
if (!Array.isArray(sections)) {
return null;
}
return sections.map(function (section, index) {
return Collapsable.create(
tslib.__assign(tslib.__assign({}, section), {
content: React__namespace.createElement(
'div',
{ className: 'accordion-content' },
section.content
),
trigger: renderTrigger(section.trigger, index) || undefined,
}),
{
autoGenerateKey: false,
defaultProps: {
open: activeIndexes.includes(index),
className: clsx__default['default']({
'previous-opened':
index !== 0 && activeIndexes.includes(index - 1),
'next-opened': activeIndexes.includes(index + 1),
}),
},
overrideProps: {
onClose: function () {
handleSectionClose(index);
},
onOpen: function () {
handleSectionOpen(index);
},
},
}
);
});
};
return React__namespace.createElement(
ElementType,
tslib.__assign({}, rest, { className: classes }),
renderSectionsElement()
);
};
Accordions.displayName = 'Accordions';
module.exports = Accordions;