如何防止imask库中最后一个字符根据日期而改变?

问题描述 投票:0回答:1

我想为日期范围制作一个掩码。但是,当我删除开始日期中的最后一个字符时,它会被结束日期中的数字替换。

是否可以使得当删除最新字符时,placeholderChar 中的值被替换?

https://codesandbox.io/p/sandbox/hungry-resonance-dyqrmx

import { IMask, useIMask } from "react-imask";

export default function App() {
  const { ref } = useIMask<HTMLInputElement>(
    {
      mask: "from - to",
      pattern: "d`.m`.0000 - d`.m`.0000",
      blocks: {
        from: {
          mask: Date,
          blocks: {
            d: {
              mask: IMask.MaskedRange,
              placeholderChar: "д",
              from: 1,
              to: 31,
              maxLength: 2,
            },
            m: {
              mask: IMask.MaskedRange,
              placeholderChar: "м",
              from: 1,
              to: 12,
              maxLength: 2,
            },
            Y: {
              mask: IMask.MaskedRange,
              placeholderChar: "г",
              from: 1900,
              to: 2999,
              maxLength: 4,
            },
          },
        },
        to: {
          mask: Date,
          blocks: {
            d: {
              mask: IMask.MaskedRange,
              placeholderChar: "д",
              from: 1,
              to: 31,
              maxLength: 2,
            },
            m: {
              mask: IMask.MaskedRange,
              placeholderChar: "м",
              from: 1,
              to: 12,
              maxLength: 2,
            },
            Y: {
              mask: IMask.MaskedRange,
              placeholderChar: "г",
              from: 1900,
              to: 2999,
              maxLength: 4,
            },
          },
        },
      },
      lazy: true,
      placeholderChar: " ",
    }
  );
  return (
    <div className="App">
      <input ref={ref} style={{ width: 180 }} />
    </div>
  );
}

reactjs mask imaskjs react-imask
1个回答
0
投票

你需要使用`:

d`.m`.0000` - d`.m`.0000
© www.soinside.com 2019 - 2024. All rights reserved.