ny
23 小时以前 282fbc6488f4e8ceb5fda759f963ee88fbf7b999
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<script lang="ts" setup>
import { computed, ref, unref, watch } from 'vue';
 
import { useAttrs } from '@jnpf/hooks';
import { getAmountChinese, thousandsFormat } from '@jnpf/utils';
 
import { InputNumber } from 'ant-design-vue';
 
defineOptions({ inheritAttrs: false, name: 'JnpfInputNumber' });
const props = defineProps({
  addonAfter: { default: '', type: String },
  addonBefore: { default: '', type: String },
  detailed: { default: false, type: Boolean },
  isAmountChinese: { default: false, type: Boolean },
  thousands: { default: false, type: Boolean },
  value: [Number, String],
});
const emit = defineEmits(['update:value', 'change']);
const attrs: any = useAttrs({ excludeDefaultKeys: false });
const prefixCls = 'jnpf-input-number';
const innerValue = ref<null | number | undefined>(undefined);
 
const getBindValue = computed(() => {
  const bindValue = {
    ...unref(attrs),
    addonAfter: props.addonAfter || null,
    addonBefore: props.addonBefore || null,
    precision: Reflect.has(unref(attrs), 'precision') && (unref(attrs).precision || unref(attrs).precision === 0) ? unref(attrs).precision : undefined,
  };
  if (props.thousands) {
    bindValue.formatter = (value) => thousandsFormat(value);
    bindValue.parser = (value) => value.replaceAll(/\$\s?|(,*)/g, '');
  }
  return bindValue;
});
const getChineseName = computed(() => (!props.isAmountChinese || (!innerValue.value && innerValue.value !== 0) ? '' : getAmountChinese(innerValue.value)));
const getStyle = computed(() => (Reflect.has(unref(attrs), 'style') ? unref(attrs).style : {}));
 
watch(
  () => unref(props.value),
  (val) => {
    setValue(val);
  },
  { immediate: true },
);
 
function setValue(value) {
  innerValue.value = (!value && value !== 0) || Number.isNaN(value) ? null : props.detailed ? value : Number(value);
}
function onChange(value) {
  emit('update:value', value);
  emit('change', value);
}
</script>
 
<template>
  <div :class="`${prefixCls}`" :style="getStyle">
    <InputNumber v-model:value="innerValue" v-bind="getBindValue" v-if="!detailed" class="w-full" @change="onChange">
      <template v-for="item in Object.keys($slots)" #[item]="data">
        <slot :name="item" v-bind="data || {}"></slot>
      </template>
    </InputNumber>
    <p v-if="detailed" :title="thousands ? thousandsFormat(innerValue) : innerValue" class="detail-txt leading-[32px]">
      <span v-if="addonBefore">{{ addonBefore }}</span>
      {{ thousands ? thousandsFormat(innerValue) : innerValue }}
      <span v-if="addonAfter">{{ addonAfter }}</span>
    </p>
    <p v-if="isAmountChinese && getChineseName" class="amount-chinese-name">{{ getChineseName }}</p>
  </div>
</template>
<style lang="scss" scoped>
.jnpf-input-number {
  .amount-chinese-name {
    margin-top: 4px;
    font-size: 14px;
    line-height: 20px;
    color: var(--text-color-secondary);
  }
}
 
.ant-table,
.jnpf-vxe-table {
  .jnpf-input-number {
    .detail-txt {
      overflow: hidden;
      text-overflow: ellipsis;
      line-height: 22px !important;
      white-space: nowrap;
    }
  }
}
</style>