This commit is contained in:
icssoa
2025-11-10 15:34:26 +08:00
4 changed files with 57 additions and 2 deletions

View File

@@ -59,6 +59,18 @@
</cl-input>
</demo-item>
<demo-item :label="t('保留精度')">
<demo-tips>当 type 为 digit 时,可设置 precision 属性来保留精度</demo-tips>
<cl-input type="digit" :precision="precision"></cl-input>
<cl-list border :pt="{ className: 'mt-5' }">
<cl-list-item :label="t('精度')">
<cl-input-number v-model="precision"></cl-input-number>
</cl-list-item>
</cl-list>
</demo-item>
<demo-item :label="t('自定义')">
<cl-input
v-model="content"
@@ -128,6 +140,8 @@ const isRightIcon = ref(false);
const isDisabled = ref(false);
const isColor = ref(false);
const precision = ref<number>(2);
function toAlert() {
ui.showToast({
message: "Hello"

View File

@@ -7,6 +7,9 @@
},
pt.className
]"
:style="{
height: parseRpx(size!)
}"
>
<view
class="cl-input-number__minus"

View File

@@ -8,7 +8,7 @@
'cl-input--border': border,
'cl-input--focus': isFocus,
'cl-input--disabled': isDisabled,
'cl-input--error': isError
'cl-input--error': isError,
}
]"
@tap="onTap"
@@ -23,12 +23,14 @@
></cl-icon>
</view>
<!-- @vue-ignore -->
<input
class="cl-input__inner"
:class="[
{
'is-disabled': isDisabled,
'is-dark': isDark
'is-dark': isDark,
'is-exceed': isExceed
},
ptClassName
]"
@@ -189,6 +191,11 @@ const props = defineProps({
holdKeyboard: {
type: Boolean,
default: false
},
// 保留精度
precision: {
type: Number,
default: 0
}
});
@@ -256,6 +263,17 @@ const showClear = computed(() => {
// 是否显示密码
const isPassword = ref(props.password);
// 是否超出限制
const isExceed = computed(() => {
// 检查数字精度是否超出限制
if (props.type == "digit" && props.precision >= 0 && value.value != "") {
const parts = value.value.split(".");
return parts.length > 1 && parts[1].length > props.precision;
} else {
return false;
}
});
// 切换密码显示状态
function showPassword() {
isPassword.value = !isPassword.value;
@@ -271,6 +289,17 @@ function onFocus(e: UniInputFocusEvent) {
function onBlur(e: UniInputBlurEvent) {
emit("blur", e);
// 处理数字精度
if (props.type == "digit" && props.precision > 0 && value.value != "") {
const numValue = parseFloat(value.value);
if (!isNaN(numValue)) {
const formattedValue = numValue.toFixed(props.precision);
value.value = formattedValue;
emit("update:modelValue", formattedValue);
emit("change", formattedValue);
}
}
setTimeout(() => {
isFocus.value = false;
}, 0);
@@ -367,6 +396,14 @@ defineExpose({
&.is-dark {
@apply text-white;
}
&.is-exceed {
@apply text-red-500;
}
&.is-exceed.is-dark {
@apply text-red-400;
}
}
&__icon {

View File

@@ -29,4 +29,5 @@ export type ClInputProps = {
adjustPosition?: boolean;
maxlength?: number;
holdKeyboard?: boolean;
precision?: number;
};