UNPKG

infinity-forge

Version:
156 lines 9.66 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 __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; }; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; 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 react_select_2 = require("react-select"); var creatable_1 = __importDefault(require("react-select/creatable")); var ui_1 = require("../../../../ui/index.js"); var custom_styles_1 = require("./custom-styles.js"); var S = __importStar(require("./styles.js")); function Select(props) { var _a, _b, _c, _d; var _e = (0, react_1.useState)(props.options || []), options = _e[0], setOptions = _e[1]; var _f = (0, react_1.useState)(false), isLoading = _f[0], setIsLoading = _f[1]; var isMounted = (0, ui_1.useComponentIsMounted)(); var _g = (0, formik_1.useField)({ name: props.name, }), field = _g[0], state = _g[1], utils = _g[2]; var fieldValue = field.value || []; var value = !props.onlyOneValue ? fieldValue && (fieldValue === null || fieldValue === void 0 ? void 0 : fieldValue.length) > 0 && props.isGroup ? fieldValue === null || fieldValue === void 0 ? void 0 : fieldValue.map(function (defaultValueForm) { var _a; return (_a = options .find(function (option) { return option.options.find(function (o) { return o.value === defaultValueForm; }); })) === null || _a === void 0 ? void 0 : _a.options.find(function (opt) { return opt.value === defaultValueForm; }); }) : fieldValue === null || fieldValue === void 0 ? void 0 : fieldValue.map(function (valueForm) { return options.find(function (option) { return option.value === valueForm; }); }) : [options.find(function (option) { return option.value === fieldValue; }) || '']; function handleCreate(inputValue) { setIsLoading(true); setTimeout(function () { var newOption = { label: inputValue, value: inputValue, }; setIsLoading(false); setOptions(function (prev) { return __spreadArray(__spreadArray([], prev, true), [newOption], false); }); var newValue = !props.onlyOneValue ? __spreadArray(__spreadArray([], fieldValue, true), [newOption === null || newOption === void 0 ? void 0 : newOption.value], false) : newOption === null || newOption === void 0 ? void 0 : newOption.value; utils.setValue(newValue); }, 900); } (0, react_1.useEffect)(function () { if (props.options && props.options.length > 0) { setOptions(props.options); } }, [props.options]); var valueExternalSelect = props.value !== undefined ? props.value : undefined; (0, react_1.useEffect)(function () { if (valueExternalSelect) { utils.setValue(valueExternalSelect); } }, [valueExternalSelect]); var SelectComponent = props.creatableSelect ? creatable_1.default : react_select_1.default; (0, react_1.useEffect)(function () { var _a, _b; if (props.controlledInitialValue) { utils.setValue((_a = props.controlledInitialValue) === null || _a === void 0 ? void 0 : _a.value); props.onChangeInput && props.onChangeInput((_b = props.controlledInitialValue) === null || _b === void 0 ? void 0 : _b.value, function () { var _a; return utils.setValue((_a = props.controlledInitialValue) === null || _a === void 0 ? void 0 : _a.value); }, { state: state, utils: utils }); } }, [props.controlledInitialValue]); if (!isMounted) { return (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, {}); } var Input = function (_a) { var rest = __rest(_a, []); return (0, jsx_runtime_1.jsx)(react_select_2.components.Input, __assign({}, rest, { autoComplete: 'off' })); }; return ((0, jsx_runtime_1.jsxs)(S.SelectWrapper, { children: [(0, jsx_runtime_1.jsx)(ui_1.InputControl, __assign({}, props, { children: (0, jsx_runtime_1.jsx)(SelectComponent, { isClearable: props === null || props === void 0 ? void 0 : props.isClearable, menuPortalTarget: document === null || document === void 0 ? void 0 : document.body, onCreateOption: handleCreate, isDisabled: isLoading || props.disabled, isLoading: isLoading, isSearchable: props.isSearchable, menuPlacement: props.menuPlacement || 'auto', menuIsOpen: props.forceOpenToDevelop ? props.forceOpenToDevelop : undefined, placeholder: props.placeholder, name: field.name, closeMenuOnSelect: props === null || props === void 0 ? void 0 : props.closeMenuOnSelect, noOptionsMessage: function () { return props.noOptionMessage || 'Nenhuma opção encontrada.'; }, 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 }); }, value: valueExternalSelect || props.isMultiple ? value : value[0], isMulti: props.isMultiple, options: options, components: props.CustomOption ? { Option: props.CustomOption, SingleValue: props.CustomOption, Input: Input } : { Input: Input }, className: ((_a = field === null || field === void 0 ? void 0 : field.value) === null || _a === void 0 ? void 0 : _a.length) > 0 ? 'open' : '', styles: __assign({ menuPortal: function (base) { return (__assign(__assign({}, base), { zIndex: ui_1.zIndexInfinityForge.reactSelectDropdown })); } }, (props.customStlyes || custom_styles_1.customStyles)) }) })), ((_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' }) })))] })); } //# sourceMappingURL=index.js.map