优化 input 的 `lay-affix` 属性对于 `disabled` 或 `readonly` 时的状态处理

pull/1181/head
贤心 2022-12-06 17:46:19 +08:00
parent 0aceceb077
commit f1204bcd63
1 changed files with 10 additions and 4 deletions

View File

@ -164,14 +164,19 @@ layui.define(['layer', 'util'], function(exports){
var affix = othis.attr('lay-affix'); var affix = othis.attr('lay-affix');
var CLASS_SUFFIX = 'layui-input-suffix'; var CLASS_SUFFIX = 'layui-input-suffix';
var elemNext = othis.next('.'+ CLASS_SUFFIX); var elemNext = othis.next('.'+ CLASS_SUFFIX);
var disabled = othis.is('[disabled]') || othis.is('[readonly]');
var hideElem = function(elem, value){ var hideElem = function(elem, value){
if(!elem) return; if(!elem) return;
elem[$.trim(value) ? 'removeClass' : 'addClass'](HIDE); elem[$.trim(value) ? 'removeClass' : 'addClass'](HIDE);
}; };
var renderSuffix = function(type){ var renderSuffix = function(type, _disabled){
elemNext.remove(); elemNext.remove();
elemNext = $(['<div class="layui-input-suffix layui-input-affix-event">' elemNext = $(['<div class="layui-input-suffix layui-input-affix-event"'+ (
,'<i class="layui-icon layui-icon-'+ type +'"></i>' _disabled ? ' disabled' : ''
) +'>'
,'<i class="layui-icon layui-icon-'+ type + (
_disabled ? ' layui-disabled' : ''
) +'"></i>'
,'</div>'].join('')); ,'</div>'].join(''));
othis.after(elemNext); othis.after(elemNext);
@ -186,6 +191,7 @@ layui.define(['layer', 'util'], function(exports){
// 点击后缀套件事件 // 点击后缀套件事件
elemNext.on('click', function(){ elemNext.on('click', function(){
var filter = othis.attr('lay-filter'); var filter = othis.attr('lay-filter');
if($(this).is('[disabled]')) return;
obj[affix] && obj[affix][1].call(this); obj[affix] && obj[affix][1].call(this);
// 对外事件 // 对外事件
@ -200,7 +206,7 @@ layui.define(['layer', 'util'], function(exports){
var obj = { var obj = {
// 清空 // 清空
clear: [function(){ // 渲染 clear: [function(){ // 渲染
renderSuffix('clear'); renderSuffix('clear', disabled);
}, function(){ // 事件 }, function(){ // 事件
othis.val('').focus(); othis.val('').focus();
hideElem(elemNext, null); hideElem(elemNext, null);