update App.js, Input.js, src/components/FormInput/index.js and src/components/TestComponentNew/index.js
parent
664c16f074
commit
be9e79528a
6 changed files with 107 additions and 4 deletions
@ -0,0 +1,22 @@ |
||||
const persianNumbers = [ |
||||
/۰/g, |
||||
/۱/g, |
||||
/۲/g, |
||||
/۳/g, |
||||
/۴/g, |
||||
/۵/g, |
||||
/۶/g, |
||||
/۷/g, |
||||
/۸/g, |
||||
/۹/g, |
||||
], |
||||
arabicNumbers = [/٠/g, /١/g, /٢/g, /٣/g, /٤/g, /٥/g, /٦/g, /٧/g, /٨/g, /٩/g], |
||||
fixNumbers = function (str) { |
||||
if (typeof str === "string") { |
||||
for (var i = 0; i < 10; i++) { |
||||
str = str.replace(persianNumbers[i], i).replace(arabicNumbers[i], i); |
||||
} |
||||
} |
||||
return str; |
||||
}; |
||||
export default fixNumbers; |
@ -0,0 +1,65 @@ |
||||
import fixNumbers from "./fixNumbers"; |
||||
import { useState } from "react"; |
||||
const Input = ({ |
||||
defaultValue, |
||||
set, |
||||
label, |
||||
className, |
||||
type, |
||||
maxLength, |
||||
max, |
||||
min, |
||||
register, |
||||
}) => { |
||||
const [value, setValue] = useState(defaultValue); |
||||
return ( |
||||
<div |
||||
className={ |
||||
"h-full rounded border border-color7 mx-1 relative flex items-center flex-row-reverse bg-color2_5 " + |
||||
className + |
||||
(value ? " border-blue-500 bg-white border rounded" : " bg-transparent") |
||||
} |
||||
> |
||||
<div |
||||
className={ |
||||
" text-sm absolute transition-all duration-300 pointer-events-none px-3 PY-2 text-left transform translate-x-[-10px] " + |
||||
(value |
||||
? `text-blue-500 bg-white top-[-10px]` |
||||
: "text-color19 top-[20px]") |
||||
} |
||||
> |
||||
{label} |
||||
</div> |
||||
{type !== "textarea" ? ( |
||||
<input |
||||
{...register} |
||||
maxLength={maxLength} |
||||
value={value} |
||||
type={"text"} |
||||
className={"rtl w-full h-full px-4 outline-0 "} |
||||
onChange={(e) => setValue(e.target.value)} |
||||
inputMode={type === "number" ? "numeric" : ""} |
||||
onInput={(e) => { |
||||
let value = e.target.value; |
||||
if (type === "number") |
||||
value = fixNumbers(value).replace(/[^\d.-]/g, ""); |
||||
if (maxLength && value.length > maxLength) |
||||
value = value.slice(0, maxLength); |
||||
if (!isNaN(max) && +e.target.value > max) value = max; |
||||
if (!isNaN(min) && +e.target.value < min) value = min; |
||||
e.target.value = value; |
||||
}} |
||||
/> |
||||
) : ( |
||||
<textarea |
||||
{...register} |
||||
className={"rtl w-full h-full p-4 outline-0 resize-none"} |
||||
onChange={(e) => setValue(e.target.value)} |
||||
> |
||||
{value} |
||||
</textarea> |
||||
)} |
||||
</div> |
||||
); |
||||
}; |
||||
export default Input; |
Loading…
Reference in new issue