AgreementPrefillInline.vue 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <template>
  2. <span class="prefill-wrap">
  3. <span v-if="label" class="prefill-label">{{ label }}</span>
  4. <a-input
  5. :value="textValue"
  6. :type="numberMode ? 'number' : 'text'"
  7. :placeholder="placeholder"
  8. :maxlength="maxLength"
  9. :disabled="disabled"
  10. class="prefill-input"
  11. @update:value="onUpdate"
  12. />
  13. <span v-if="suffix" class="prefill-suffix">{{ suffix }}</span>
  14. </span>
  15. </template>
  16. <script setup lang="ts">
  17. import { computed } from 'vue';
  18. const props = withDefaults(
  19. defineProps<{
  20. modelValue?: string | number | null;
  21. numberMode?: boolean;
  22. placeholder?: string;
  23. label?: string;
  24. suffix?: string;
  25. maxLength?: number;
  26. disabled?: boolean;
  27. }>(),
  28. {
  29. modelValue: '',
  30. numberMode: false,
  31. disabled: false,
  32. placeholder: '请填写',
  33. label: '',
  34. suffix: '',
  35. maxLength: 20,
  36. },
  37. );
  38. const emit = defineEmits<{
  39. 'update:modelValue': [value: string | number];
  40. }>();
  41. const textValue = computed(() =>
  42. props.modelValue === null || props.modelValue === undefined ? '' : String(props.modelValue),
  43. );
  44. function onUpdate(raw: string) {
  45. if (props.numberMode) {
  46. const n = parseInt(String(raw).replace(/\D/g, ''), 10);
  47. emit('update:modelValue', Number.isFinite(n) ? n : 0);
  48. } else {
  49. emit('update:modelValue', raw);
  50. }
  51. }
  52. </script>
  53. <style scoped>
  54. .prefill-wrap {
  55. display: inline-flex;
  56. align-items: center;
  57. flex-wrap: wrap;
  58. gap: 4px;
  59. vertical-align: middle;
  60. }
  61. .prefill-label {
  62. white-space: nowrap;
  63. }
  64. .prefill-input {
  65. width: 5rem;
  66. min-width: 4rem;
  67. text-align: center;
  68. }
  69. .prefill-suffix {
  70. white-space: nowrap;
  71. }
  72. </style>