DatePicker Ant Design – 为手动输入的日期添加掩码

huangapple go评论71阅读模式
英文:

DatePicker Ant Design - add a mask to the manually entered date

问题

我想和你分享我在使用 Ant Design - React 的 DatePicker 组件时遇到的问题。

第一个挑战是为手动输入的日期添加掩码到输入字段中。

第二个挑战是在不必按日期确认按钮的情况下接受输入的更改。

英文:

I would like to share with you a problem I was having while using the DatePicker component from Ant Design - React.

The first challenge was to add a mask to the manually entered date in the input field.

The second challenge was to accept changes in the input without having to press the date confirmation button.

答案1

得分: 1

以下是您提供的代码部分的中文翻译:

<!-- jquery -->
<script src="https://code.jquery.com/jquery-3.6.3.js" integrity="sha256-nQLuAZGRRcILA+6dMBOvcRh5Pe310sBpanc6+QBmyVM=" crossorigin="anonymous"></script>

<!-- jquery-mask -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.10/jquery.mask.js"></script>

<!-- mask formatter -->
<script>
  jQuery(function($){
    $('form').on('DOMSubtreeModified', function(){
      $('.ant-picker-input').find('input').attr('maxlength', '10').mask('99/99/9999');
    });

    $('.ant-picker-input').find('input').attr('maxlength', '10').mask('99/99/9999');
  });
</script>

以及:

<Form.Item
  label="Data de nascimento"
  name="birthDate">
     <DatePicker style={{width: '100%'}} format="DD/MM/YYYY" onBlur={(date) =>{
     const currentFormInitialValues = form.getFieldsValue();                                      
     currentFormInitialValues.birthDate = dayjs(date.target.value, 'DD/MM/YYYY');
                                            
     if(!date.target.value || date.target.value === ''){
        return
     }else{                                              
        form.setFieldsValue(currentFormInitialValues);
     };
 }}/>&lt;/Form.Item&gt;

如果您有任何其他需要,请随时提出。

英文:

Both issues were resolved as follows:

public/index.html

&lt;!-- jquery --&gt;
&lt;script src=&quot;https://code.jquery.com/jquery-3.6.3.js&quot; integrity=&quot;sha256-nQLuAZGRRcILA+6dMBOvcRh5Pe310sBpanc6+QBmyVM=&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;

&lt;!-- jquery-mask --&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.10/jquery.mask.js&quot;&gt;&lt;/script&gt;

&lt;!-- mask formatter --&gt;
&lt;script&gt;
  jQuery(function($){
    $(&#39;form&#39;).on(&#39;DOMSubtreeModified&#39;, function(){
      $(&#39;.ant-picker-input&#39;).find(&#39;input&#39;).attr(&#39;maxlength&#39;, &#39;10&#39;).mask(&#39;99/99/9999&#39;);
    });

    $(&#39;.ant-picker-input&#39;).find(&#39;input&#39;).attr(&#39;maxlength&#39;, &#39;10&#39;).mask(&#39;99/99/9999&#39;);
  });
&lt;/script&gt;

And finally:

&lt;Form.Item
  label=&quot;Data de nascimento&quot;
  name=&quot;birthDate&quot;&gt;
     &lt;DatePicker style={{width: &#39;100%&#39;}} format=&quot;DD/MM/YYYY&quot; onBlur={(date) =&gt;{
     const currentFormInitialValues = form.getFieldsValue();                                      
     currentFormInitialValues.birthDate = dayjs(date.target.value, &#39;DD/MM/YYYY&#39;);
                                            
     if(!date.target.value || date.target.value === &#39;&#39;){
        return
     }else{                                              
        form.setFieldsValue(currentFormInitialValues);
     };
 }}/&gt;
&lt;/Form.Item&gt;

It may not be the best way to solve the problem, but it worked. I'm open to suggestions if anyone has a better idea of how to do it.

答案2

得分: 0

只创建一个被接受的日期格式数组

const dateFormatList = [
  'DD/MM/YYYY',
  'DD/MM/YY',
  'DD-MM-YYYY',
  'DD-MM-YY',
  'DDMMYYYY',
  'DDMMYY'
]

然后将其分配给DatePicker格式:

<DatePicker
  mode={'date'}
  format={dateFormatList}
/>

现在,如果您在日期字段中仅输入数字并按enter键,日期将被格式化为数组中的第一个可用格式。

英文:

Just create an array of accepted date formats

const dateFormatList = [
  &#39;DD/MM/YYYY&#39;,
  &#39;DD/MM/YY&#39;,
  &#39;DD-MM-YYYY&#39;,
  &#39;DD-MM-YY&#39;,
  &#39;DDMMYYYY&#39;,
  &#39;DDMMYY&#39;
]

And assign it to DatePicker format:

&lt;DatePicker
  mode={ &#39;date&#39; }
  format={ dateFormatList }
/&gt;

Now if you type only numbers in your date field and press enter the date will be formated to the first format available in your array.

huangapple
  • 本文由 发表于 2023年6月6日 16:05:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76412573.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定