|
| 1 | +import { |
| 2 | + Box, |
| 3 | + Input, |
| 4 | + Tag, |
| 5 | + TagCloseButton, |
| 6 | + TagLabel, |
| 7 | + Wrap, |
| 8 | + WrapItem, |
| 9 | +} from "@chakra-ui/react" |
| 10 | +import { |
| 11 | + type ClipboardEvent, |
| 12 | + type KeyboardEvent, |
| 13 | + useRef, |
| 14 | + useState, |
| 15 | +} from "react" |
| 16 | + |
| 17 | +interface CidrInputProps { |
| 18 | + value: string |
| 19 | + onChange: (value: string) => void |
| 20 | + placeholder?: string |
| 21 | + isInvalid?: boolean |
| 22 | +} |
| 23 | + |
| 24 | +export function parseCidrInput(input: string): string[] { |
| 25 | + // split by comma, space, semicolon or newlines |
| 26 | + const rawParts = input.split(/[\s,;\n]+/) |
| 27 | + const parsed: string[] = [] |
| 28 | + |
| 29 | + for (let part of rawParts) { |
| 30 | + part = part.trim() |
| 31 | + if (!part) continue |
| 32 | + |
| 33 | + // Check if it already has a slash |
| 34 | + if (part.includes("/")) { |
| 35 | + parsed.push(part) |
| 36 | + } else { |
| 37 | + // Check if it looks like IPv4 or IPv6 |
| 38 | + if (/^(\d{1,3}\.){3}\d{1,3}$/.test(part)) { |
| 39 | + parsed.push(`${part}/32`) |
| 40 | + } else if (/^[0-9a-fA-F:]+$/.test(part) && part.includes(":")) { |
| 41 | + parsed.push(`${part}/128`) |
| 42 | + } else { |
| 43 | + parsed.push(part) |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + return parsed |
| 48 | +} |
| 49 | + |
| 50 | +export const CidrInput = ({ |
| 51 | + value, |
| 52 | + onChange, |
| 53 | + placeholder, |
| 54 | + isInvalid, |
| 55 | +}: CidrInputProps) => { |
| 56 | + const [inputValue, setInputValue] = useState("") |
| 57 | + const inputRef = useRef<HTMLInputElement>(null) |
| 58 | + |
| 59 | + // Convert comma-separated string to array |
| 60 | + const cidrs = value |
| 61 | + ? value |
| 62 | + .split(",") |
| 63 | + .map((c) => c.trim()) |
| 64 | + .filter(Boolean) |
| 65 | + : [] |
| 66 | + |
| 67 | + const updateCidrs = (newCidrs: string[]) => { |
| 68 | + // deduplicate |
| 69 | + const unique = Array.from(new Set(newCidrs)) |
| 70 | + onChange(unique.join(",")) |
| 71 | + } |
| 72 | + |
| 73 | + const addCidrs = (rawInput: string) => { |
| 74 | + const parsed = parseCidrInput(rawInput) |
| 75 | + if (parsed.length > 0) { |
| 76 | + updateCidrs([...cidrs, ...parsed]) |
| 77 | + setInputValue("") |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + const removeCidr = (indexToRemove: number) => { |
| 82 | + const updated = cidrs.filter((_, idx) => idx !== indexToRemove) |
| 83 | + updateCidrs(updated) |
| 84 | + } |
| 85 | + |
| 86 | + const handleKeyDown = (e: KeyboardEvent<HTMLInputElement>) => { |
| 87 | + if (e.key === "Enter" || e.key === ",") { |
| 88 | + e.preventDefault() |
| 89 | + addCidrs(inputValue) |
| 90 | + } else if (e.key === "Backspace" && !inputValue && cidrs.length > 0) { |
| 91 | + removeCidr(cidrs.length - 1) |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + const handleBlur = () => { |
| 96 | + if (inputValue.trim()) { |
| 97 | + addCidrs(inputValue) |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + const handlePaste = (e: ClipboardEvent<HTMLInputElement>) => { |
| 102 | + e.preventDefault() |
| 103 | + const pastedText = e.clipboardData.getData("text") |
| 104 | + addCidrs(pastedText) |
| 105 | + } |
| 106 | + |
| 107 | + const handleContainerClick = () => { |
| 108 | + if (inputRef.current) { |
| 109 | + inputRef.current.focus() |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + return ( |
| 114 | + <Box |
| 115 | + border="1px solid" |
| 116 | + borderColor={isInvalid ? "red.500" : "gray.300"} |
| 117 | + borderRadius="md" |
| 118 | + p={2} |
| 119 | + minH="40px" |
| 120 | + bg="white" |
| 121 | + _hover={{ borderColor: isInvalid ? "red.600" : "gray.400" }} |
| 122 | + _focusWithin={{ |
| 123 | + borderColor: isInvalid ? "red.500" : "ui.main", |
| 124 | + boxShadow: isInvalid ? "0 0 0 1px #e53e3e" : "0 0 0 1px #009688", |
| 125 | + }} |
| 126 | + cursor="text" |
| 127 | + onClick={handleContainerClick} |
| 128 | + > |
| 129 | + <Wrap spacing={2} align="center"> |
| 130 | + {cidrs.map((cidr, index) => ( |
| 131 | + <WrapItem key={`${cidr}-${index}`}> |
| 132 | + <Tag |
| 133 | + size="sm" |
| 134 | + borderRadius="full" |
| 135 | + variant="solid" |
| 136 | + colorScheme="teal" |
| 137 | + backgroundColor="ui.main" |
| 138 | + color="white" |
| 139 | + > |
| 140 | + <TagLabel>{cidr}</TagLabel> |
| 141 | + <TagCloseButton |
| 142 | + onClick={(e) => { |
| 143 | + e.stopPropagation() |
| 144 | + removeCidr(index) |
| 145 | + }} |
| 146 | + /> |
| 147 | + </Tag> |
| 148 | + </WrapItem> |
| 149 | + ))} |
| 150 | + <WrapItem flexGrow={1}> |
| 151 | + <Input |
| 152 | + ref={inputRef} |
| 153 | + value={inputValue} |
| 154 | + onChange={(e) => setInputValue(e.target.value)} |
| 155 | + onKeyDown={handleKeyDown} |
| 156 | + onBlur={handleBlur} |
| 157 | + onPaste={handlePaste} |
| 158 | + placeholder={cidrs.length === 0 ? placeholder : ""} |
| 159 | + size="sm" |
| 160 | + variant="unstyled" |
| 161 | + minW="120px" |
| 162 | + p={0} |
| 163 | + _focus={{ border: "none" }} |
| 164 | + /> |
| 165 | + </WrapItem> |
| 166 | + </Wrap> |
| 167 | + </Box> |
| 168 | + ) |
| 169 | +} |
0 commit comments