@mikezimm/fps-library-v2
Version:
Library of reusable typescript/javascript functions, interfaces and constants
56 lines • 3.86 kB
JavaScript
import * as React from 'react';
require('@mikezimm/fps-styles/dist/fps-Dropdown.css');
const MultiSelectDropdownC = ({ key, label, description, maxVsibleRows = 5, multiSelect = false, options, selectedKeys, onChange, styles = {}, labelStyles = {}, className = '', autoClose = true, showCount = true, required = true, disabled = false, }) => {
// Local state to manage dropdown open/closed state
const [isOpen, setIsOpen] = React.useState(false);
// Toggles the dropdown open/closed
const handleToggle = () => setIsOpen(!isOpen);
// Handles selection changes for single or multiple selections
const handleOptionClick = (key, idx) => {
if (multiSelect) {
const newSelectedKeys = selectedKeys.indexOf(key) !== -1
? selectedKeys.filter((k) => k !== key) // Remove if already selected
: [...selectedKeys, key]; // Add if not selected
const newSelectedIdx = [];
options.map((option, i) => {
if (key === option.key)
newSelectedIdx.push(i);
});
onChange(newSelectedKeys, newSelectedIdx);
}
else {
onChange([key], [idx]); // Single selection
setIsOpen(false); // Close dropdown after selection
}
};
// Close dropdown on blur https://github.com/fps-solutions/SP-API-Tester/issues/59
const handleBlur = (event) => {
// Check if the blur event is triggered due to focus moving within the dropdown
if (autoClose === true && !event.currentTarget.contains(event.relatedTarget)) {
setIsOpen(false); // Close dropdown
}
};
let needsSelect = required === true && selectedKeys.length === 0 ? true : false;
let selectedLabel = showCount === false ? '' : `[ ${selectedKeys.length} of ${options.length} ]${needsSelect === true ? ' *' : ''}`;
return (React.createElement("div", { tabIndex: -1, onBlur: handleBlur, className: `fps-Dropdown ${className}`, style: { display: 'flex', flexDirection: 'column', maxWidth: '200px', ...styles } },
React.createElement("label", { className: "dropdown-label", style: labelStyles, title: needsSelect === true ? 'Selection is required' : '' }, `${label} ${selectedLabel}`),
React.createElement("div", { tabIndex: 0, className: "dropdown-trigger", onClick: handleToggle },
React.createElement("span", null, selectedKeys.length > 0
? options.filter((option) => selectedKeys.indexOf(option.key) !== -1).map((option) => option.text).join(', ')
: 'Select...'),
React.createElement("span", { className: `dropdown-arrow ${isOpen ? 'open' : ''}` }, "v")),
isOpen && (React.createElement("div", { className: "dropdown-options", tabIndex: 0, style: {
position: 'absolute',
top: '100%',
left: 0,
width: '100%',
maxHeight: `${Math.min(options.length, maxVsibleRows) * 2.5}em`,
overflowY: 'auto',
zIndex: 10,
} }, options.map((option, idx) => (React.createElement("div", { key: option.key, onClick: () => disabled === false && option.disabled !== true && handleOptionClick(option.key, idx), className: `dropdown-option ${selectedKeys.indexOf(option.key) !== -1 ? 'selected' : ''} ${disabled === false && option.disabled === true ? 'disabled' : ''} ${option.heading ? 'heading' : ''}`, style: option.style },
option.icon && React.createElement("span", { className: "dropdown-icon" }, option.icon),
option.text))))),
description && React.createElement("div", { className: "dropdown-description" }, description)));
};
export default MultiSelectDropdownC;
//# sourceMappingURL=fps-Dropdown.js.map