@neo4j-ndl/react
Version:
React implementation of Neo4j Design System
126 lines (125 loc) • 7.36 kB
JavaScript
/**
*
* Copyright (c) "Neo4j"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Select = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const react_select_1 = __importDefault(require("react-select"));
const async_1 = __importDefault(require("react-select/async"));
/** Different types of select components */
const creatable_1 = __importDefault(require("react-select/creatable"));
const defaultImports_1 = require("../_common/defaultImports");
const utils_1 = require("../_common/utils");
const Dialog_1 = require("../dialog/Dialog");
const helpers_1 = require("../helpers");
const icons_1 = require("../icons");
const Overrides_1 = require("./Overrides");
/**
*
*
* Helpers
*
*
*/
const overrideComponents = (props) => ({
DropdownIndicator: (0, Overrides_1.DropdownIndicatorCurrying)(props),
IndicatorSeparator: null,
ClearIndicator: (0, Overrides_1.CustomClearIndication)(props),
Placeholder: (0, Overrides_1.CustomPlaceholder)(props),
Option: (0, Overrides_1.CustomOption)(props),
MultiValue: (0, Overrides_1.CustomMultiValue)(props),
Control: (0, Overrides_1.CustomControl)(props),
IndicatorsContainer: (0, Overrides_1.CustomIndicatorsContainer)(props),
ValueContainer: (0, Overrides_1.CustomValueContainer)(props),
Input: (0, Overrides_1.CustomInput)(props),
SingleValue: (0, Overrides_1.CustomSingleValue)(props),
Menu: (0, Overrides_1.CustomMenu)(props),
MenuPortal: (0, Overrides_1.CustomMenuPortal)(props),
MenuList: (0, Overrides_1.CustomMenuList)(props),
});
const customStyles = () => {
return {
control: () => ({}),
menuList: () => ({}),
option: () => ({}),
valueContainer: (provided) => (Object.assign(Object.assign({}, provided), { padding: '0' })),
indicatorsContainer: () => ({}),
dropdownIndicator: () => ({}),
clearIndicator: () => ({}),
input: () => ({}),
singleValue: () => ({}),
placeholder: () => ({}),
menu: (provided) => ({
top: provided.top,
bottom: provided.bottom,
}),
};
};
exports.Select = (0, helpers_1.forwardRef)(function Select(props, ref) {
var _a, _b, _c;
const { as, label, helpText, errorText, isFluid = true, size = 'small', type = 'select', 'aria-label': ariaLabel, selectProps = {}, hasBorder = true, isDisabled, htmlAttributes, style, } = props;
const isInsideDialog = (0, Dialog_1.useIsInsideDialog)();
const menuPosition = isInsideDialog ? 'fixed' : 'absolute';
const Component = as !== null && as !== void 0 ? as : 'div';
const ComponentOverrides = (0, react_1.useMemo)(() => overrideComponents({
errorText,
hasBorder,
size,
}), [errorText, size, hasBorder]);
const StyleOverrides = (0, react_1.useMemo)(() => customStyles(), []);
const identifier = (0, react_1.useMemo)(() => {
if (typeof label === 'string')
return label;
if (selectProps['aria-label'] !== undefined)
return selectProps['aria-label'];
(0, utils_1.needleWarningMessage)('Consider adding a label or aria-label to the selectProps to make the select more accessible');
// 7 char long pseudo-random string
return Math.random().toString(36).slice(2, 9);
}, [label, selectProps]);
let SelectElement;
switch (type) {
case 'async': {
SelectElement = ((0, jsx_runtime_1.jsx)(async_1.default, Object.assign({ name: identifier, inputId: identifier, isDisabled: isDisabled || selectProps.isDisabled, menuPlacement: "auto" }, selectProps, { menuPosition: (_a = selectProps.menuPosition) !== null && _a !== void 0 ? _a : menuPosition, hideSelectedOptions: false, closeMenuOnSelect: !selectProps.isMulti, styles: Object.assign(Object.assign({}, StyleOverrides), selectProps.styles), components: Object.assign(Object.assign({}, ComponentOverrides), selectProps.components) })));
break;
}
case 'creatable':
SelectElement = ((0, jsx_runtime_1.jsx)(creatable_1.default, Object.assign({ name: identifier, inputId: identifier, isDisabled: isDisabled || selectProps.isDisabled, menuPlacement: "auto" }, selectProps, { menuPosition: (_b = selectProps.menuPosition) !== null && _b !== void 0 ? _b : menuPosition, hideSelectedOptions: false, closeMenuOnSelect: !selectProps.isMulti, styles: Object.assign(Object.assign({}, StyleOverrides), selectProps.styles), components: Object.assign(Object.assign({}, ComponentOverrides), selectProps.components) })));
break;
default:
SelectElement = ((0, jsx_runtime_1.jsx)(react_select_1.default, Object.assign({ name: identifier, inputId: identifier, isDisabled: isDisabled || selectProps.isDisabled, menuPlacement: "auto" }, selectProps, { menuPosition: (_c = selectProps.menuPosition) !== null && _c !== void 0 ? _c : menuPosition, hideSelectedOptions: false, closeMenuOnSelect: !selectProps.isMulti, styles: Object.assign(Object.assign({}, StyleOverrides), selectProps.styles), components: Object.assign(Object.assign({}, ComponentOverrides), selectProps.components) })));
}
const classes = (0, defaultImports_1.classNames)('ndl-select', props.className, {
'ndl-small': size === 'small',
'ndl-medium': size === 'medium',
'ndl-large': size === 'large',
'ndl-fluid': isFluid,
'ndl-disabled': selectProps.isDisabled,
'ndl-multi': selectProps.isMulti,
'ndl-creatable': type === 'creatable',
'ndl-has-error': !!errorText,
});
return ((0, jsx_runtime_1.jsxs)(Component, Object.assign({ "aria-label": ariaLabel, ref: ref, className: classes, style: style }, htmlAttributes, { children: [label && (0, jsx_runtime_1.jsx)("label", { htmlFor: identifier, children: label }), SelectElement, helpText && !errorText && ((0, jsx_runtime_1.jsx)("span", { className: "ndl-sub-text", "aria-disabled": isDisabled || selectProps.isDisabled, children: helpText })), errorText && ((0, jsx_runtime_1.jsxs)("div", { className: "n-flex n-flex-row n-gap-token-3 n-items-center", children: [(0, jsx_runtime_1.jsx)(icons_1.ExclamationCircleIconSolid, { className: (0, defaultImports_1.classNames)('ndl-error-icon', classes), role: "img", type: "solid" }), (0, jsx_runtime_1.jsx)("span", { className: "ndl-sub-text ndl-error-text", children: errorText })] }))] })));
});
//# sourceMappingURL=Select.js.map