MCPcopy
hub / github.com/deepops-ai/deepops / InputSelect

Function InputSelect

ui/src/components/select/InputSelect.tsx:45–119  ·  view source on GitHub ↗
({ value,label, options, onChange, variant = "outline", customOption = null, placeholder = "...", size = "sm", isClearable = false, placement="bottom", showArrow = true,closeOnBlur=true ,matchWidth=true,enableInput=true,width="200px"}: SelectProps)

Source from the content-addressed store, hash-verified

43}
44
45const InputSelect = ({ value,label, options, onChange, variant = "outline", customOption = null, placeholder = "...", size = "sm", isClearable = false, placement="bottom", showArrow = true,closeOnBlur=true ,matchWidth=true,enableInput=true,width="200px"}: SelectProps) => {
46 const { isOpen, onToggle, onClose } = useDisclosure()
47 const [entered, setEntered] = useState(false)
48 const [query, setQuery] = useState('')
49
50 useEffect(() => {
51 if (enableInput) {
52 setQuery(label??(value??''))
53 }
54 },[value,label])
55
56 const ref = useRef(null)
57
58 const onOptionClick = option => {
59 onChange(option.value)
60 onToggle()
61 setEntered(false)
62 }
63
64 const searchedResult = useMemo(() => {
65 const q = query.trim()
66 if (!entered || q == "") {
67 return options
68 }
69
70 return options.filter(o => (label !== null ? o.label : o.value).toLowerCase().indexOf(q) >= 0)
71 }, [options, query])
72
73 const getBorderStyle = () => {
74 if (variant == "outline") return "bordered"
75 if (variant == "flushed") return "bordered-bottom"
76 if (variant == "unstyled") return ""
77 }
78
79 const clearSelected = (e: MouseEvent) => {
80 e.stopPropagation()
81 onChange('')
82 }
83
84 const [isLargeScreen] = useMediaQuery(MobileBreakpoint)
85 return (<>
86 {<Flex height={`${size=="md" ? 'var(--chakra-sizes-10)' : (size=="sm" ? 'var(--chakra-sizes-8)' : 'var(--chakra-sizes-12)')}`} px="3" className={getBorderStyle()} py="1" justifyContent="space-between" alignItems="center" cursor="pointer" onClick={onToggle}><Tooltip placement="right" openDelay={500} label={value}><Text width={width} maxW={width} noOfLines={1} layerStyle="textSecondary" opacity="0.7" fontSize={size == "sm" ? "0.9rem" : "1rem"}>{label ?? (isEmpty(value)? placeholder : value)}</Text></Tooltip> {isClearable && value ? <FaTimes fontSize="0.8rem" onClick={clearSelected} opacity="0.6"/> : showArrow && <Box pl="1"><FaChevronDown fontSize="0.6rem" /></Box>}</Flex>}
87 <Popover matchWidth={matchWidth} closeOnBlur={closeOnBlur} placement={placement} isOpen={isOpen} initialFocusRef={ref} onClose={onClose}>
88 <PopoverTrigger >
89 <Box position="absolute"></Box>
90 </PopoverTrigger>
91 <Portal>
92 <PopoverContent borderRadius={2} width={isLargeScreen ? "500px" : "240px"}>
93 {/* <PopoverArrow /> */}
94 <PopoverBody p="0">
95 <Box>
96 <Input px="2" py="2" value={query} onChange={e => { setQuery(e.currentTarget.value.toLowerCase());if (!entered) {setEntered(true)} }} ref={ref} size={size} variant="unstyled" className="bordered-bottom" placeholder="input to search, press Enter to use current input" onKeyDown={e => {
97 if (enableInput && e.key === 'Enter') {
98 onChange(query)
99 onToggle()
100 setEntered(false)
101 }
102 }} />

Callers

nothing calls this directly

Calls 4

isEmptyFunction · 0.90
getBorderStyleFunction · 0.70
onOptionClickFunction · 0.70
onChangeFunction · 0.50

Tested by

no test coverage detected