@codinglane/dropdown
Version:
An easy-to-use react dropdown
99 lines (98 loc) • 5.22 kB
JavaScript
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Grouped = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = __importDefault(require("react"));
const Contracts = __importStar(require("../../../../contracts"));
const group_1 = require("./group");
const DEFAULT_FAVORITE = 'Favorite';
const DEFAULT_NON_FAVORITE = 'Standard';
exports.Grouped = react_1.default.forwardRef((_a, ref) => {
var _b;
var { id, options, onOptionClick, onFilteredChange, current, filter, onFavorize, favoriteGroupName, nonFavoriteGroupName, grouping, favorize, anchor } = _a, props = __rest(_a, ["id", "options", "onOptionClick", "onFilteredChange", "current", "filter", "onFavorize", "favoriteGroupName", "nonFavoriteGroupName", "grouping", "favorize", "anchor"]);
const favorites = options.filter((opt) => opt.favorite).length;
const favoritesName = favoriteGroupName !== null && favoriteGroupName !== void 0 ? favoriteGroupName : DEFAULT_FAVORITE;
const nonFavoritesName = nonFavoriteGroupName !== null && nonFavoriteGroupName !== void 0 ? nonFavoriteGroupName : DEFAULT_NON_FAVORITE;
const top = react_1.default.useMemo(() => {
if ((anchor === null || anchor === void 0 ? void 0 : anchor.direction) === 'DOWN')
return anchor.at;
}, [anchor]);
const bottom = react_1.default.useMemo(() => {
if ((anchor === null || anchor === void 0 ? void 0 : anchor.direction) === 'UP')
return anchor.at;
}, [anchor]);
const grouped = react_1.default.useMemo(() => {
if (favorize && !grouping)
return Contracts.mapToFavoriteGroup(options, { favorite: favoritesName, nonFavorite: nonFavoritesName }, filter);
if (!favorize)
return Contracts.mapToGroups(options, filter);
return Contracts.mapToGroupsWithFavorites(options, { favorite: favoritesName, nonFavorite: nonFavoritesName }, filter);
}, [options, filter, favorize, favorites]);
const selected = react_1.default.useMemo(() => grouped
.reduce((prev, curr) => {
if (!curr.isParent)
return prev.concat(curr.options);
return prev.concat(curr.options.reduce((prv, crr) => {
if (!crr.isParent)
return prv.concat(crr.options);
return [];
}, []));
}, [])
.filter((option) => option.value === current)
.pop(), [options, current]);
react_1.default.useEffect(() => {
onFilteredChange(grouped
.reduce((prev, curr) => {
if (!curr.isParent)
return prev.concat(curr.options);
return curr.options.reduce((prv, crr) => {
if (!crr.isParent)
return prv.concat(crr.options);
return [];
}, []);
}, [])
.map((fltr) => fltr.value));
}, [grouped]);
return ((0, jsx_runtime_1.jsx)("div", Object.assign({ className: 'dropdown-content dropdown-grouped', ref: ref, style: {
top,
bottom,
maxHeight: Contracts.MENU_MAX_HEIGHT,
} }, props, { children: (0, jsx_runtime_1.jsx)(group_1.Group, { onOptionClick: onOptionClick, id: id === null || id === void 0 ? void 0 : id.concat('option'), selected: selected, favorize: favorize, onFavorize: onFavorize, grouped: grouped, "data-testid": (_b = props['data-testid']) === null || _b === void 0 ? void 0 : _b.concat('-group') }) })));
});
;