diff --git a/comps/form/field/FieldInput.tsx b/comps/form/field/FieldInput.tsx index ef348b7..ff851e5 100755 --- a/comps/form/field/FieldInput.tsx +++ b/comps/form/field/FieldInput.tsx @@ -135,7 +135,7 @@ export const FieldInput: FC<{ {prefix && prefix !== "" ? (
) => void; onBlur?: (e: FocusEvent) => void; @@ -110,6 +112,7 @@ export const FieldTypeInput: FC<{ const disabled = typeof field.disabled === "function" ? field.disabled() : field.disabled; + switch (type_field) { case "toggle": return ( @@ -198,6 +201,8 @@ export const FieldTypeInput: FC<{ ); + case "otp": + return ; case "rich-text": return ; case "date": { diff --git a/comps/form/field/type/TypeOTP.tsx b/comps/form/field/type/TypeOTP.tsx new file mode 100755 index 0000000..703c3df --- /dev/null +++ b/comps/form/field/type/TypeOTP.tsx @@ -0,0 +1,87 @@ +import { useLocal } from "lib/utils/use-local"; +import { FC } from "react"; +import { FieldLocal, FieldProp, FMLocal } from "../../typings"; +import { PropTypeInput } from "./TypeInput"; + +export const FieldOTP: FC<{ + digit: number; + field: FieldLocal; + fm: FMLocal; + prop: PropTypeInput; + arg: FieldProp; +}> = ({ digit, fm, field }) => { + const local = useLocal({ + otp: [] as string[], + ref: [] as HTMLInputElement[], + }); + + if (local.otp.length === 0 && digit) { + for (let i = 0; i < digit; i++) { + local.otp.push(""); + } + } + + return ( +
+ {local.otp.map((item, idx) => ( + {}} + value={item} + ref={(ref) => { + if (ref) local.ref[idx] = ref; + }} + onPaste={(e) => { + e.preventDefault(); + + var clipboardData = + e.clipboardData || (window as any).clipboardData; + var pastedData = clipboardData.getData("text"); + for (let i = 0; i < pastedData.length; i++) { + if (i >= local.otp.length) break; + local.otp[i] = pastedData[i]; + } + local.render(); + }} + onKeyDown={async (e) => { + if (e.key === "Backspace") { + local.otp[idx] = ""; + local.render(); + const ref = local.ref[idx - 1]; + if (ref) { + ref.focus(); + } + } else if (parseInt(e.key) || e.key === "0") { + local.otp[idx] = e.key; + local.render(); + const ref = local.ref[idx + 1]; + if (ref) { + ref.focus(); + } + } + + const otp = local.otp.join(""); + if (otp.length === digit) { + fm.data[field.name] = otp; + fm.render(); + } + local.render(); + }} + /> + ))} +
+ ); +};