Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

اسلش در هنگام تایپ #306

Open
zahraMohammaddoust opened this issue Aug 4, 2024 · 1 comment
Open

اسلش در هنگام تایپ #306

zahraMohammaddoust opened this issue Aug 4, 2024 · 1 comment

Comments

@zahraMohammaddoust
Copy link

وقتی کاربر تاریخ و دستی تایپ میکنه قابلیتش و داره که اسلش خودکار بیفته؟

@Mr-Yavar
Copy link

1.

npm i react-text-mask --save

2.

import MaskedInput from "react-text-mask/dist/reactTextMask.js";

// تبدیل اعداد فارسی یا عربی به انگلیسی 
function toEnglishNumber(str) {
  const persianDigits = [/۰/g, /۱/g, /۲/g, /۳/g, /۴/g, /۵/g, /۶/g, /۷/g, /۸/g, /۹/g];
  const arabicDigits = [/٠/g, /١/g, /٢/g, /٣/g, /٤/g, /٥/g, /٦/g, /٧/g, /٨/g, /٩/g];
  for (let i = 0; i < 10; ++i) {
    str = str.replace(persianDigits[i], i).replace(arabicDigits[i], i);
  }
  return str;
}

// کامپوننت ورود تاریخ
export function DateMaskInput({
  value,
  handleValueChange,
  openCalendar,
  mask = [/\d/, /\d/, /\d/, /\d/, "/", /\d/, /\d/, "/", /\d/, /\d/],
}) {
  return (
    <MaskedInput
      mask={mask}
      format="yyyy/mm/dd"
      inputMode="numeric"
      onFocus={openCalendar}
      value={toEnglishNumber(value.toString())}
      placeholderChar={"-"}
      showMask
      onChange={(e) => {
        let [year, month, day] = e.target.value.split("/");

        e.target.value = `${year}/${month}/${day}`;

        handleValueChange(e);
      }}
    />
  );
}

3.

 <DatePicker render={<DateMaskInput />} ... />

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants