import PropTypes from 'prop-types' import React, { useEffect, useRef, useState } from 'react' import { BGColor, PColor, SECColor, SEGColor, SFColor } from '../../../../assets/colors' import { IconArrowBottom, IconFolder, IconLoading, IconPlus, IconCancel as IconWarning } from '../../../../assets/icons' import { Overline } from '../../../atoms/Overline' import { changeSearch, changeValue, findOptionById, renderVal } from './helpers' import { BoxOptions, BoxSelect, ButtonAction, ContainerItems, ContentBox, CustomButtonS, IconSel, InputText, LabelInput, MainButton, SpanText, TextNotResult, Tooltip } from './styles' export const NewSelect = ({ options = [], beforeLabel = '', noLabel = false, disabled = false, id = '', icon = true, loading = false, sideLabel = '', name = '', action = false, optionName = '', topTitle = '15px', value = '', border, width = '100%', search = ' ', title = '', padding = '', margin = '', heightBody, minWidth = '', error = false, required = false, overLine = false, accessor, fullName, onChange = () => { return }, handleClickAction = () => { return } }) => { /** Hooks */ const bodyHeight = 100 const [select, setSelect] = useState(false) const [selectRef, setSelectRef] = useState(0) const [valueInput, setValueInput] = useState() const [selectBody, setSelectBody] = useState(0) const [newOption, setNewOption] = useState(false) const inputSearch = useRef(null) const [refSelect, setRefSelect] = useState(false) const refComponente = useRef(null) useEffect(() => { const handleDocumentClick = (event) => { if (refComponente.current && !refComponente.current.contains(event.target)) { setSelect(false) } } const handleWindowBlur = () => { setSelect(false) } document.addEventListener('mousedown', handleDocumentClick) window.addEventListener('blur', handleWindowBlur) return () => { document.removeEventListener('mousedown', handleDocumentClick) window.removeEventListener('blur', handleWindowBlur) } }, []) // guarda la referencia de la caja */ const changeRef = (v) => { setSelectRef(v.offsetTop + selectBody) setRefSelect(v) } const handleClick = (e) => { e.preventDefault() setSelect(!select) setTimeout(() => { return setNewOption(options) }, 500) } const handleBlur = () => { return setSelect(false) } const val = findOptionById(options, id, value) useEffect(() => { setNewOption(options) }, [options]) useEffect(() => { if (search) { select && inputSearch.current.focus() } }, [select, search]) return (