From 4ab2a80f92effdd2fe0d36047d0bbf1dd6603073 Mon Sep 17 00:00:00 2001 From: morning-star <1453017105@qq.com> Date: Wed, 20 Sep 2023 13:16:11 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20`input-number`=20=20?= =?UTF-8?q?=E7=BB=84=E4=BB=B6=20`lay-precision`=20attributes=EF=BC=8C?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=A6=81=E7=94=A8=E8=A7=86=E8=A7=89=E6=95=88?= =?UTF-8?q?=E6=9E=9C=20(#1375)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 新增 `input-number` 组件 `lay-precision` attributes,优化禁用视觉效果 * update code * refactor --- src/modules/form.js | 81 +++++++++++++++++++++++++++++---------------- 1 file changed, 52 insertions(+), 29 deletions(-) diff --git a/src/modules/form.js b/src/modules/form.js index dfb10324..d7705c2f 100644 --- a/src/modules/form.js +++ b/src/modules/form.js @@ -174,6 +174,48 @@ layui.define(['lay', 'layer', 'util'], function(exports){ // 初始化全局的 autocomplete options.autocomplete && inputs.attr('autocomplete', options.autocomplete); + var handleInputNumberEvents = function(elem, eventType){ + var that = this; + var rawValue = elem.val(); + var value = Number(rawValue); + var step = Number(elem.attr('step')) || 1; // 加减的数字间隔 + var min = Number(elem.attr('min')); + var max = Number(elem.attr('max')); + var precision = Number(elem.attr('lay-precision')); + var noAction = eventType === 'blur' && rawValue === '' // 失焦时空值不作处理 + + if(isNaN(value)) return; // 若非数字,则不作处理 + + if(eventType === 'click'){ + var isDecrement = !!$(that).index() // 0: icon-up, 1: icon-down + value = isDecrement ? value - step : value + step; + } + + // 获取小数点后位数 + var decimals = function(step){ + var decimals = (step.toString().match(/\.(\d+$)/) || [])[1] || ''; + return decimals.length; + }; + + precision = precision >= 0 ? precision : Math.max(decimals(step), decimals(rawValue)); + + if(!noAction){ + if(value <= min) value = min; + if(value >= max) value = max; + if(precision) value = value.toFixed(precision); + + elem.val(value) + } + + // 更新按钮状态 + var controlBtn = { + increment: elem.next().find('.layui-icon-up'), + decrement: elem.next().find('.layui-icon-down') + } + controlBtn.increment[(value >= max && !noAction) ? 'addClass' : 'removeClass'](DISABLED) + controlBtn.decrement[(value <= min && !noAction) ? 'addClass' : 'removeClass'](DISABLED) + } + // 初始化输入框动态点缀 elemForm.find('input[lay-affix],textarea[lay-affix]').each(function(){ var othis = $(this); @@ -248,6 +290,11 @@ layui.define(['lay', 'layer', 'util'], function(exports){ var value = this.value; opts.show === 'auto' && showAffix(elemAffix, value); }); + + // 失去焦点事件 + othis.on('blur', function(){ + typeof opts.blur === 'function' && opts.blur.call(this, othis, opts); + }); // 点击动态后缀事件 elemIcon.on('click', function(){ @@ -295,35 +342,11 @@ layui.define(['lay', 'layer', 'util'], function(exports){ className: 'layui-input-number', disabled: othis.is('[disabled]'), // 跟随输入框禁用状态 click: function(elem){ - var index = $(this).index(); - var value = elem.val(); - var rawValue = value; - var step = Number(elem.attr('step')) || 1; // 加减的数字间隔 - var min = Number(elem.attr('min')); - var max = Number(elem.attr('max')); - - if(isNaN(value)) return; // 若非数字,则不作处理 - - value = Number(value); - value = index ? value - step : value + step; - - // min max - if(value < min) value = min; - if(value > max) value = max; - - // 获取小数点后位数 - var decimals = function(step){ - var decimals = (step.toString().match(/\.(\d+$)/) || [])[1] || ''; - return decimals.length; - }; - - // 位数比较 - var fixed = Math.max(decimals(step), decimals(rawValue)); - - if(fixed) value = value.toFixed(fixed); - - elem.val(value); - } + handleInputNumberEvents.call(this, elem, 'click') + }, + blur: function(elem){ + handleInputNumberEvents.call(this, elem, 'blur') + }, } };