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

36 lines
1.2 KiB
TypeScript
Raw Normal View History

2022-08-12 18:51:50 +00:00
import { NumberInput, NumberInputProps } from "@mantine/core"
2022-08-12 16:00:58 +00:00
import React, { useState } from "react"
2022-08-12 18:51:50 +00:00
interface PortInputProps extends NumberInputProps {
fullWidth?: boolean
}
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 value = parseInt((e.target as HTMLInputElement).value)
if (value > 65535) {
(e.target as HTMLInputElement).value = oldValue
} else if (value < 1) {
(e.target as HTMLInputElement).value = oldValue
}else{
(e.target as HTMLInputElement).value = value.toString()
}
setOldValue((e.target as HTMLInputElement).value)
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}
{...propeties}
2022-08-12 16:00:58 +00:00
/>
2022-08-12 18:51:50 +00:00
})
export default PortInput