Files
firegex-traffic-viewer/frontend/src/components/PortInput.tsx

61 lines
2.2 KiB
TypeScript
Raw Normal View History

import { Input, NumberInput, NumberInputProps, TextInput, TextInputProps } from "@mantine/core"
2022-08-12 16:00:58 +00:00
import React, { useState } from "react"
2023-09-25 18:10:12 +02:00
import { regex_port, regex_range_port } from "../js/utils"
2022-08-12 16:00:58 +00:00
2022-08-12 18:51:50 +00:00
interface PortInputProps extends NumberInputProps {
fullWidth?: boolean
}
2022-08-12 18:51:50 +00:00
const PortInput = React.forwardRef<HTMLInputElement, PortInputProps>( (props, ref) => {
const [oldValue, setOldValue] = useState<string>(props.defaultValue?props.defaultValue.toString():"")
const {fullWidth, ...propeties} = props
2022-08-12 16:00:58 +00:00
return <NumberInput
2022-08-12 18:51:50 +00:00
variant={props.variant?props.variant:"filled"}
2022-08-12 16:00:58 +00:00
hideControls
placeholder="80"
2022-08-12 18:51:50 +00:00
min={props.min?props.min:1}
max={props.max?props.min:65535}
style={fullWidth?props.style:{ width: "75px", ...props.style }}
2022-08-12 16:00:58 +00:00
onInput={(e) => {
const target = e.target as HTMLInputElement
2023-09-25 18:10:12 +02:00
target.value.match(regex_port)?setOldValue(target.value):target.value = oldValue
props.onInput?.(e)
}}
ref={ref}
{...propeties}
/>
})
interface PortRangeInputProps extends TextInputProps {
fullWidth?: boolean,
defaultValues?: number[]
}
export const PortRangeInput = React.forwardRef<HTMLInputElement, PortRangeInputProps>( (props, ref) => {
2023-09-25 18:10:12 +02:00
const [oldValue, setOldValue] = useState<string>(props.defaultValue?props.defaultValue.toString():"")
const {fullWidth, defaultValues, ...propeties} = props
let defaultValuesInt = defaultValues
if (defaultValuesInt?.length == 2 && defaultValuesInt[0] == defaultValuesInt[1]){
defaultValuesInt = [defaultValuesInt[0]]
}
return <TextInput
variant={props.variant?props.variant:"filled"}
placeholder="1000-1337"
style={fullWidth?props.style:{ width: "150px", ...props.style }}
onInput={(e) => {
const target = e.target as HTMLInputElement
2023-09-25 18:10:12 +02:00
target.value.match(regex_range_port)?setOldValue(target.value):target.value = oldValue
2022-08-12 18:51:50 +00:00
props.onInput?.(e)
2022-08-12 16:00:58 +00:00
}}
2022-08-12 18:51:50 +00:00
ref={ref}
defaultValue={defaultValuesInt?defaultValuesInt.map(v => v.toString()).join("-"):props.defaultValue}
{...propeties}
2022-08-12 16:00:58 +00:00
/>
2022-08-12 18:51:50 +00:00
})
export default PortInput