@neo4j-ndl/react
Version:
React implementation of Neo4j Design System
129 lines • 7.59 kB
JavaScript
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;
};
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
/**
*
* 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/>.
*/
import classNames from 'classnames';
import { useEffect, useId, useMemo } from 'react';
import ReactSelect from 'react-select';
import AsyncSelect from 'react-select/async';
/** Different types of select components */
import Creatable from 'react-select/creatable';
import { needleWarningMessage } from '../_common/utils';
import { useIsInsideDialog } from '../dialog/dialog-context';
import { ExclamationCircleIconSolid } from '../icons';
import { CustomClearIndication, CustomControl, CustomIndicatorsContainer, CustomInput, CustomMenu, CustomMenuList, CustomMenuPortal, CustomMultiValue, CustomOption, CustomPlaceholder, CustomSingleValue, CustomValueContainer, DropdownIndicatorCurrying, } from './Overrides';
/**
*
*
* Helpers
*
*
*/
const overrideComponents = (props) => ({
ClearIndicator: CustomClearIndication(props),
Control: CustomControl(props),
DropdownIndicator: DropdownIndicatorCurrying(props),
IndicatorSeparator: null,
IndicatorsContainer: CustomIndicatorsContainer(props),
Input: CustomInput(props),
Menu: CustomMenu(props),
MenuList: CustomMenuList(props),
MenuPortal: CustomMenuPortal(props),
MultiValue: CustomMultiValue(props),
Option: CustomOption(props),
Placeholder: CustomPlaceholder(props),
SingleValue: CustomSingleValue(props),
ValueContainer: CustomValueContainer(props),
});
const customStyles = () => {
return {
clearIndicator: () => ({}),
control: () => ({}),
dropdownIndicator: () => ({}),
indicatorsContainer: () => ({}),
input: () => ({}),
menu: (provided) => ({
bottom: provided.bottom,
top: provided.top,
}),
menuList: (provided) => ({
maxHeight: provided.maxHeight,
minHeight: provided.minHeight,
}),
option: () => ({}),
placeholder: () => ({}),
singleValue: () => ({}),
valueContainer: (provided) => (Object.assign(Object.assign({}, provided), { padding: '0' })),
};
};
export const Select = (_a) => {
var _b, _c, _d;
var { as, label, helpText, errorText, isFluid = true, size = 'medium', type = 'select', ariaLabel, selectProps = {}, isClean = false, isDisabled, htmlAttributes, className, style, ref } = _a, restProps = __rest(_a, ["as", "label", "helpText", "errorText", "isFluid", "size", "type", "ariaLabel", "selectProps", "isClean", "isDisabled", "htmlAttributes", "className", "style", "ref"]);
const isInsideDialog = useIsInsideDialog();
const menuPosition = isInsideDialog ? 'fixed' : 'absolute';
const identifier = useId();
const Component = as !== null && as !== void 0 ? as : 'div';
const ComponentOverrides = useMemo(() => overrideComponents({
errorText,
isClean,
size,
}), [errorText, size, isClean]);
const StyleOverrides = useMemo(() => customStyles(), []);
useEffect(() => {
if (label === undefined && ariaLabel === undefined) {
needleWarningMessage('A Select needs to have a label or an aria label to be accessible. link: https://dequeuniversity.com/rules/axe/4.2/label?application=axeAPI');
}
}, [label, ariaLabel]);
let SelectElement;
switch (type) {
case 'async': {
SelectElement = (_jsx(AsyncSelect, Object.assign({ name: identifier, inputId: identifier, isDisabled: Boolean(isDisabled) || selectProps.isDisabled, menuPlacement: "auto", "aria-label": ariaLabel }, selectProps, { menuPosition: (_b = selectProps.menuPosition) !== null && _b !== void 0 ? _b : menuPosition, hideSelectedOptions: false, closeMenuOnSelect: Boolean(selectProps.isMulti) === false, styles: Object.assign(Object.assign({}, StyleOverrides), selectProps.styles), components: Object.assign(Object.assign({}, ComponentOverrides), selectProps.components) })));
break;
}
case 'creatable':
SelectElement = (_jsx(Creatable, Object.assign({ name: identifier, inputId: identifier, isDisabled: Boolean(isDisabled) || selectProps.isDisabled, menuPlacement: "auto" }, selectProps, { "aria-label": ariaLabel, menuPosition: (_c = selectProps.menuPosition) !== null && _c !== void 0 ? _c : menuPosition, hideSelectedOptions: false, closeMenuOnSelect: Boolean(selectProps.isMulti) === false, styles: Object.assign(Object.assign({}, StyleOverrides), selectProps.styles), components: Object.assign(Object.assign({}, ComponentOverrides), selectProps.components) })));
break;
default:
SelectElement = (_jsx(ReactSelect, Object.assign({ name: identifier, inputId: identifier, isDisabled: Boolean(isDisabled) || selectProps.isDisabled, menuPlacement: "auto" }, selectProps, { "aria-label": ariaLabel, menuPosition: (_d = selectProps.menuPosition) !== null && _d !== void 0 ? _d : menuPosition, hideSelectedOptions: false, closeMenuOnSelect: Boolean(selectProps.isMulti) === false, styles: Object.assign(Object.assign({}, StyleOverrides), selectProps.styles), components: Object.assign(Object.assign({}, ComponentOverrides), selectProps.components) })));
}
const classes = classNames('ndl-select', className, {
'ndl-creatable': type === 'creatable',
'ndl-disabled': selectProps.isDisabled,
'ndl-fluid': isFluid,
'ndl-has-error': Boolean(errorText),
'ndl-large': size === 'large',
'ndl-medium': size === 'medium',
'ndl-multi': selectProps.isMulti,
'ndl-small': size === 'small',
});
return (_jsxs(Component, Object.assign({ ref: ref, className: classes, style: style }, restProps, htmlAttributes, { children: [Boolean(label) && _jsx("label", { htmlFor: identifier, children: label }), SelectElement, Boolean(helpText) && Boolean(errorText) === false && (_jsx("span", { className: "ndl-sub-text", "aria-disabled": Boolean(isDisabled) || selectProps.isDisabled, children: helpText })), Boolean(errorText) && (_jsxs("div", { className: "n-flex n-flex-row n-gap-token-4 n-items-center", children: [_jsx(ExclamationCircleIconSolid, { className: classNames('ndl-error-icon', classes), htmlAttributes: { role: 'img', type: 'solid' } }), _jsx("span", { className: "ndl-sub-text ndl-error-text", children: errorText })] }))] })));
};
//# sourceMappingURL=Select.js.map