From 302b06249177e8cb3905f2833e0a5e306da6cfa0 Mon Sep 17 00:00:00 2001 From: Sacha Weatherstone Date: Thu, 19 Jan 2023 20:05:24 +1000 Subject: [PATCH] Commit IP input prototype --- src/components/form/IPInput.tsx | 70 +++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100755 src/components/form/IPInput.tsx diff --git a/src/components/form/IPInput.tsx b/src/components/form/IPInput.tsx new file mode 100755 index 00000000..b21bfd20 --- /dev/null +++ b/src/components/form/IPInput.tsx @@ -0,0 +1,70 @@ +import type React from "react"; +import { forwardRef, InputHTMLAttributes, useEffect } from "react"; + +import { InfoWrapper, InfoWrapperProps } from "@components/form/InfoWrapper.js"; +import { ExclamationCircleIcon } from "@heroicons/react/24/outline"; +import { useState } from "react"; +import type { InputProps } from "./Input.js"; + +export const IPInput = forwardRef(function Input( + { + label, + description, + prefix, + suffix, + action, + error, + disabled, + value, + ...rest + }: InputProps, + ref +) { + const [numericalValue, setNumericalValue] = useState(); + const [facadeInputValue, setFacadeInputValue] = useState(); + + useEffect(() => { + if (typeof value === "number") { + setFacadeInputValue( + value + .toString(16) + .match(/.{1,3}/g) + ?.map((v) => parseInt(v, 10)) + ?.join(".") + ); + } + }, [value]); + + return ( + +
+ + { + setFacadeInputValue(e.target.value); + setNumericalValue( + parseInt( + e.target.value + .split(".") + .map((v) => parseInt(v).toString(16)) + .join(""), + 16 + ) + ); + }} + className={`flex h-10 w-full rounded-md border-none bg-backgroundPrimary px-3 text-sm text-textPrimary focus:outline-none focus:ring-2 focus:ring-accent ${ + prefix ? "rounded-l-none" : "" + } ${action ? "rounded-r-none" : ""} ${ + disabled + ? "cursor-not-allowed text-textSecondary brightness-disabled hover:brightness-disabled" + : "" + }`} + disabled={disabled} + step="any" + {...rest} + /> +
+
+ ); +});