UNPKG

infinity-forge

Version:
118 lines • 7.39 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; 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 () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Select = Select; var jsx_runtime_1 = require("react/jsx-runtime"); var react_1 = require("react"); var formik_1 = require("formik"); var react_select_1 = __importDefault(require("react-select")); var ui_1 = require("../../../../ui/index.js"); var hooks_1 = require("./hooks/index.js"); var S = __importStar(require("./styles.js")); function Select(props) { var _a, _b, _c, _d; var _e = (0, react_1.useState)(false), isLoading = _e[0], setIsLoading = _e[1]; var _f = (0, react_1.useState)(false), menuIsOpen = _f[0], setMenuIsOpen = _f[1]; var _g = (0, react_1.useState)(props.options || []), options = _g[0], setOptions = _g[1]; var selectRef = (0, react_1.useRef)(null); var _h = (0, formik_1.useField)({ name: props.name }), field = _h[0], state = _h[1], utils = _h[2]; var id = (0, react_1.useMemo)(function () { return "select-".concat(props.name); }, [props.name]); var isMounted = (0, ui_1.useComponentIsMounted)(); var formik = (0, ui_1.useFormHandler)().formik; var values = (_a = formik.values) === null || _a === void 0 ? void 0 : _a[props.name]; var hooksParameters = { id: id, field: field, state: state, utils: utils, options: options, selectRef: selectRef, isLoading: isLoading, menuIsOpen: menuIsOpen, setOptions: setOptions, setIsLoading: setIsLoading, setMenuIsOpen: setMenuIsOpen, selectPropsInfinityForge: props, onCloseMenu: props.onCloseMenu, maxItemsValueContainer: props.maxItemsValueContainer, isMultiple: props.multiple || props.isMultiple, }; var _j = (0, hooks_1.selectState)(hooksParameters), value = _j.value, valueExternalSelect = _j.valueExternalSelect; var valueSelect = props.isMultiple || props.multiple ? value || [] : valueExternalSelect || (!(value === null || value === void 0 ? void 0 : value[0]) ? null : value[0]); var selectProps = (0, hooks_1.useSelectProps)(__assign(__assign({}, hooksParameters), { valueSelect: valueSelect })); var components = (0, hooks_1.useSelectComponents)(hooksParameters); if (!isMounted) { return (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, {}); } return ((0, jsx_runtime_1.jsxs)(S.Select, { ref: selectRef, style: __assign({}, props.style), children: [(0, jsx_runtime_1.jsxs)(ui_1.InputControl, __assign({}, props, { i18n: { disabled: true }, children: [!!props.showSelectedValues && Array.isArray(values) && ((0, jsx_runtime_1.jsx)("div", { className: 'show_selected_values_infinity_forge', children: values === null || values === void 0 ? void 0 : values.map(function (v) { var _a; return ((0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsx)("button", { type: 'button', onClick: function () { return utils.setValue(values.filter(function (value) { return value !== v; })); }, children: (0, jsx_runtime_1.jsx)(ui_1.Icon, { name: 'IconClose' }) }), (0, jsx_runtime_1.jsx)("span", { children: (_a = props.options.find(function (o) { return o.value === v; })) === null || _a === void 0 ? void 0 : _a.label })] }, v)); }) })), (0, jsx_runtime_1.jsx)(react_select_1.default, __assign({}, selectProps, { value: valueSelect, components: components, options: options, isClearable: props.isClearable, onChange: function (ev) { var _a; var value = props.onlyOneValue ? ((_a = ev === null || ev === void 0 ? void 0 : ev.value) !== null && _a !== void 0 ? _a : null) : Array.isArray(ev) ? ev.map(function (ev) { return ev.value; }) : ev ? [ev.value] : []; if (!props.controlledOnChangeValue) { utils.setValue(value); } props.onChangeInput && props.onChangeInput(value, function () { return utils.setValue(value); }, { state: state, utils: utils }); if (props.onlyOneValue) { setMenuIsOpen(false); } } }))] })), ((_b = props === null || props === void 0 ? void 0 : props.addButton) === null || _b === void 0 ? void 0 : _b.CustomComponent) ? ((0, jsx_runtime_1.jsx)("div", { className: 'custom-component', children: (_c = props === null || props === void 0 ? void 0 : props.addButton) === null || _c === void 0 ? void 0 : _c.CustomComponent })) : (((_d = props === null || props === void 0 ? void 0 : props.addButton) === null || _d === void 0 ? void 0 : _d.onClick) && ((0, jsx_runtime_1.jsx)("button", { className: 'add', type: 'button', onClick: props.addButton.onClick, children: (0, jsx_runtime_1.jsx)(ui_1.Icon, { name: 'PlusIcon' }) }))), menuIsOpen && ((0, jsx_runtime_1.jsx)(ui_1.Overlay, { isTransparent: true, onClick: function () { var _a; setMenuIsOpen(false); (_a = props === null || props === void 0 ? void 0 : props.onCloseMenu) === null || _a === void 0 ? void 0 : _a.call(props); }, isExiting: false, zIndex: 999, children: '' }))] })); } //# sourceMappingURL=index.js.map