UNPKG

@brizy/ui

Version:
112 lines (111 loc) 6.11 kB
import React, { useCallback, useEffect, useRef, useState } from "react"; import { ClickOutside } from "../ClickOutside"; import { Select } from "./Select"; import { Option } from "./Option"; import { OnChangeCases } from "../SelectEditable/types"; import { BRZ_PREFIX } from "../constants"; import { useTranslation } from "../utils/localization/useTranslation"; import { Icon } from "../Icon"; import { Check } from "@brizy/ui-icons/lib/icons/Check"; import { CloseOulined } from "@brizy/ui-icons/lib/icons/CloseOulined"; const editModeClickOutsideExceptions = [".brz-ui-input", ".brz-ui-icon__svg"]; export const SelectEditable2 = ({ value, placeholder, error, choices, title, onChange, hasEdit, hasDelete, hasDuplicate, hasReset, }) => { const ref = useRef(null); const { t } = useTranslation(); const [isDropdownOpen, setIsDropdownOpen] = useState(false); const [isEditMode, setIsEditMode] = useState(false); const [err, setErr] = useState(error); const [edit, setEdit] = useState(title !== null && title !== void 0 ? title : ""); useEffect(() => { setEdit(title !== null && title !== void 0 ? title : ""); }, [title]); const openDropdown = useCallback(() => setIsDropdownOpen(true), []); const closeDropdown = useCallback(() => { setIsDropdownOpen(false); }, []); const handleEditModeClickOutside = useCallback(() => { var _a; if (!err) { setIsEditMode(false); const originalNameBeforeEdit = (_a = choices.find(i => i.title === value)) === null || _a === void 0 ? void 0 : _a.title; setEdit(originalNameBeforeEdit !== null && originalNameBeforeEdit !== void 0 ? originalNameBeforeEdit : ""); } }, [err, choices, value]); const handleInputChange = useCallback((e) => { const inputValue = e.target.value.trim(); setEdit(inputValue); if (inputValue === "") { setErr(t("Title can't be empty")); } else if (choices.find(item => item.title === inputValue)) { setErr(t("Item with the same title already exists")); } else { setErr(undefined); } }, [choices, t]); useEffect(() => { if (isEditMode && ref.current) { ref.current.focus(); } }, [isEditMode]); const handleEdit = useCallback(() => { setIsEditMode(true); setIsDropdownOpen(false); }, []); const handleDuplicate = useCallback(() => { onChange({ type: OnChangeCases.Duplicate, }); setIsDropdownOpen(false); }, [onChange]); const handleReset = useCallback(() => { onChange({ type: OnChangeCases.Reset, }); setIsDropdownOpen(false); }, [onChange]); const handleDelete = useCallback(() => { onChange({ type: OnChangeCases.Delete, }); setIsDropdownOpen(false); }, [onChange]); const handleMFChange = useCallback((s) => { setEdit(s); onChange({ type: OnChangeCases.Symbol, payload: s }); }, [onChange]); const handleEditSave = useCallback(() => { if (!err) { onChange({ type: OnChangeCases.Edit, payload: edit, }); setIsEditMode(false); } else { setIsEditMode(false); setErr(undefined); setEdit(title !== null && title !== void 0 ? title : ""); } }, [edit, err, onChange, title]); const selectedChoice = choices.find(item => item.title === value); const allowEdit = (selectedChoice === null || selectedChoice === void 0 ? void 0 : selectedChoice.allowEdit) === undefined ? true : selectedChoice.allowEdit; const allowRemove = (selectedChoice === null || selectedChoice === void 0 ? void 0 : selectedChoice.allowRemove) === undefined ? true : selectedChoice.allowRemove; const allowDuplicate = (selectedChoice === null || selectedChoice === void 0 ? void 0 : selectedChoice.allowDuplicate) === undefined ? true : selectedChoice.allowDuplicate; const isNotAllActionButtonDisabled = hasDelete || hasEdit || hasReset || hasDuplicate; return (React.createElement(React.Fragment, null, React.createElement("div", { className: `${BRZ_PREFIX}-editable-select` }, isEditMode ? (React.createElement(ClickOutside, { onClickOutside: handleEditModeClickOutside, exceptions: editModeClickOutsideExceptions }, React.createElement("input", { className: `${BRZ_PREFIX}-editable-select-input`, type: "text", ref: ref, value: edit !== null && edit !== void 0 ? edit : "", onChange: handleInputChange }))) : (React.createElement(Select, { placeholder: placeholder, value: value, choices: choices, onChange: handleMFChange })), isNotAllActionButtonDisabled && (isEditMode ? (React.createElement(Icon, { source: err ? CloseOulined : Check, color: "white", onClick: handleEditSave, size: "15px" })) : (React.createElement("div", { className: `${BRZ_PREFIX}-editable-select-dots`, onClick: openDropdown }))), isDropdownOpen && (React.createElement(ClickOutside, { onClickOutside: closeDropdown }, React.createElement(React.Fragment, null, React.createElement("div", { className: `${BRZ_PREFIX}-editable-select__options` }, hasEdit && React.createElement(Option, { title: t("Edit Name"), disabled: isEditMode || !allowEdit, onClick: handleEdit }), hasDuplicate && (React.createElement(Option, { title: t("Duplicate"), disabled: isEditMode || !allowDuplicate, onClick: handleDuplicate })), hasReset && (React.createElement(Option, { title: t("Reset"), disabled: isEditMode || !allowDuplicate, onClick: handleReset })), hasDelete && (React.createElement(Option, { title: t("Delete"), disabled: isEditMode || !allowRemove, onClick: handleDelete }))))))), err && React.createElement("div", { className: `${BRZ_PREFIX}-editable-select__error` }, err))); };