@commercetools-uikit/search-select-field
Version:
A search select field built on top of search select input, allowing users to asynchronously search for options
143 lines (139 loc) • 7.75 kB
JavaScript
import _defineProperty from '@babel/runtime-corejs3/helpers/esm/defineProperty';
import _objectWithoutProperties from '@babel/runtime-corejs3/helpers/esm/objectWithoutProperties';
import _someInstanceProperty from '@babel/runtime-corejs3/core-js-stable/instance/some';
import _Object$values from '@babel/runtime-corejs3/core-js-stable/object/values';
import _Array$isArray from '@babel/runtime-corejs3/core-js-stable/array/is-array';
import _Object$keys from '@babel/runtime-corejs3/core-js-stable/object/keys';
import _Object$getOwnPropertySymbols from '@babel/runtime-corejs3/core-js-stable/object/get-own-property-symbols';
import _filterInstanceProperty from '@babel/runtime-corejs3/core-js-stable/instance/filter';
import _Object$getOwnPropertyDescriptor from '@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptor';
import _forEachInstanceProperty from '@babel/runtime-corejs3/core-js-stable/instance/for-each';
import _Object$getOwnPropertyDescriptors from '@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptors';
import _Object$defineProperties from '@babel/runtime-corejs3/core-js-stable/object/define-properties';
import _Object$defineProperty from '@babel/runtime-corejs3/core-js-stable/object/define-property';
import { isValidElement } from 'react';
import { createSequentialId, warning, filterDataAttributes } from '@commercetools-uikit/utils';
import Constraints from '@commercetools-uikit/constraints';
import { useFieldId } from '@commercetools-uikit/hooks';
import Spacings from '@commercetools-uikit/spacings';
import FieldLabel from '@commercetools-uikit/field-label';
import SearchSelectInput from '@commercetools-uikit/search-select-input';
import FieldErrors from '@commercetools-uikit/field-errors';
import FieldWarnings from '@commercetools-uikit/field-warnings';
import { jsx, jsxs } from '@emotion/react/jsx-runtime';
const _excluded = ["controlShouldRenderValue"];
function ownKeys(e, r) { var t = _Object$keys(e); if (_Object$getOwnPropertySymbols) { var o = _Object$getOwnPropertySymbols(e); r && (o = _filterInstanceProperty(o).call(o, function (r) { return _Object$getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var _context3, _context4; var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? _forEachInstanceProperty(_context3 = ownKeys(Object(t), !0)).call(_context3, function (r) { _defineProperty(e, r, t[r]); }) : _Object$getOwnPropertyDescriptors ? _Object$defineProperties(e, _Object$getOwnPropertyDescriptors(t)) : _forEachInstanceProperty(_context4 = ownKeys(Object(t))).call(_context4, function (r) { _Object$defineProperty(e, r, _Object$getOwnPropertyDescriptor(t, r)); }); } return e; }
const hasErrors = errors => {
var _context;
return errors && _someInstanceProperty(_context = _Object$values(errors)).call(_context, Boolean);
};
const hasWarnings = warnings => {
var _context2;
return warnings && _someInstanceProperty(_context2 = _Object$values(warnings)).call(_context2, Boolean);
};
const sequentialId = createSequentialId('search-select-field-');
const SearchSelectField = _ref => {
let _ref$controlShouldRen = _ref.controlShouldRenderValue,
controlShouldRenderValue = _ref$controlShouldRen === void 0 ? true : _ref$controlShouldRen,
props = _objectWithoutProperties(_ref, _excluded);
const hasError = Boolean(props.touched) && hasErrors(props.errors);
const hasWarning = props.hasWarning || Boolean(props.touched) && hasWarnings(props.warnings);
const id = useFieldId(props.id, sequentialId);
const errorsContainerId = `${id}-errors`;
const warningsContainerId = `${id}-warnings`;
if (!props.isReadOnly) {
process.env.NODE_ENV !== "production" ? warning(typeof props.onChange === 'function', 'SearchSelectField: `onChange` is required when field is not read only.') : void 0;
}
if (props.hintIcon) {
process.env.NODE_ENV !== "production" ? warning(typeof props.hint === 'string' || /*#__PURE__*/isValidElement(props.hint), 'SearchSelectField: `hint` is required to be string or ReactNode if hintIcon is present') : void 0;
}
if (props.isMulti) {
process.env.NODE_ENV !== "production" ? warning(_Array$isArray(props.value), 'SearchSelectField: `value` is expected to be an array of string when isMulti is true') : void 0;
process.env.NODE_ENV !== "production" ? warning(typeof props.touched === 'undefined' || _Array$isArray(props.touched), 'SearchSelectField: `touched` is expected to be an array of boolean when isMulti is true') : void 0;
}
return jsx(Constraints.Horizontal, {
max: props.horizontalConstraint,
children: jsxs(Spacings.Stack, {
scale: "xs",
children: [jsx(FieldLabel, {
title: props.title,
hint: props.hint,
description: props.description,
onInfoButtonClick: props.onInfoButtonClick,
hintIcon: props.hintIcon,
badge: props.badge,
hasRequiredIndicator: props.isRequired,
htmlFor: id
}), jsx(SearchSelectInput, _objectSpread(_objectSpread({}, filterDataAttributes(_objectSpread({
controlShouldRenderValue
}, props))), {}, {
horizontalConstraint: "scale",
hasError: hasError,
"aria-label": props['aria-label'],
"aria-labelledby": props['aria-labelledby'],
"aria-invalid": hasError,
"aria-errormessage": errorsContainerId,
isAutofocussed: props.isAutofocussed,
backspaceRemovesValue: props.backspaceRemovesValue,
components: props.components,
filterOption: props.filterOption,
id: id,
containerId: props.containerId,
isClearable: props.isClearable,
isCondensed: props.isCondensed,
isDisabled: props.isDisabled,
isReadOnly: props.isReadOnly,
isOptionDisabled: props.isOptionDisabled,
isMulti: props.isMulti,
hasWarning: hasWarning,
maxMenuHeight: props.maxMenuHeight,
menuPortalTarget: props.menuPortalTarget,
menuPortalZIndex: props.menuPortalZIndex,
menuShouldBlockScroll: props.menuShouldBlockScroll,
name: props.name,
noOptionsMessage: props.noOptionsMessage,
loadingMessage: props.loadingMessage,
onBlur: props.onBlur,
onChange: props.onChange,
onFocus: props.onFocus,
onInputChange: props.onInputChange,
placeholder: props.placeholder,
tabIndex: props.tabIndex,
tabSelectsValue: props.tabSelectsValue,
value: props.value
// Async react-select props
,
loadOptions: props.loadOptions,
cacheOptions: props.cacheOptions,
showOptionGroupDivider: props.showOptionGroupDivider,
optionType: props.optionType,
controlShouldRenderValue: controlShouldRenderValue,
iconLeft: props.iconLeft
})), jsx(FieldErrors, {
id: errorsContainerId,
errors: props.errors,
isVisible: hasError,
renderError: props.renderError
}), jsx(FieldWarnings, {
id: warningsContainerId,
warnings: props.warnings,
isVisible: hasWarning,
renderWarning: props.renderWarning
})]
})
});
};
SearchSelectField.displayName = 'SearchSelectField';
/**
* Use this function to convert the Formik `errors` object type to
* our custom field errors type.
* This is primarly useful when using TypeScript.
*/
SearchSelectField.toFieldErrors = function toFieldErrors(errors) {
return errors;
};
var SearchSelectField$1 = SearchSelectField;
// NOTE: This string will be replaced on build time with the package version.
var version = "20.6.1";
export { SearchSelectField$1 as default, version };