|
| 1 | +<template> |
| 2 | + <div |
| 3 | + class="uic-input-number-container" |
| 4 | + data-testid="uic-input-number-container" |
| 5 | + > |
| 6 | + <Label |
| 7 | + :label-for="id" |
| 8 | + :label="label" |
| 9 | + :required="required" |
| 10 | + :sr-only-label="hideLabel" |
| 11 | + data-testid="uic-input-number-label" |
| 12 | + /> |
| 13 | + <InputNumber |
| 14 | + :currency="currency" |
| 15 | + :disabled="disabled" |
| 16 | + :input-class="[ |
| 17 | + 'uic-input-number', |
| 18 | + { 'uic-error': error }, |
| 19 | + { 'uic-success': success } |
| 20 | + ]" |
| 21 | + :input-id="id" |
| 22 | + :input-props="computedInputProps" |
| 23 | + :invalid="error" |
| 24 | + :max="max" |
| 25 | + :min-fraction-digits="minFractionDigits" |
| 26 | + :min="min" |
| 27 | + :mode="mode" |
| 28 | + :model-value="modelValue" |
| 29 | + :placeholder="placeholder" |
| 30 | + :readonly="readonly" |
| 31 | + locale="en-US" |
| 32 | + unstyled |
| 33 | + @blur="emits('blur')" |
| 34 | + @focus="emits('focus')" |
| 35 | + @input="(e) => emits('update:modelValue', e.value as number)" |
| 36 | + /> |
| 37 | + <!-- The PrimeVue number input does not have helper text, I copied this from the PrimeVue text input. --> |
| 38 | + <small |
| 39 | + v-if="helperText" |
| 40 | + :id="helperTextId" |
| 41 | + :class="[ |
| 42 | + 'uic-input-number-helper', |
| 43 | + { 'uic-error': error }, |
| 44 | + { 'uic-success': success } |
| 45 | + ]" |
| 46 | + data-testid="uic-input-number-helper" |
| 47 | + > |
| 48 | + {{ helperText }} |
| 49 | + </small> |
| 50 | + </div> |
| 51 | +</template> |
| 52 | + |
| 53 | +<script lang="ts"> |
| 54 | +export default { |
| 55 | + inheritAttrs: false |
| 56 | +}; |
| 57 | +</script> |
| 58 | +<script setup lang="ts"> |
| 59 | +import InputNumber, { InputNumberProps } from 'primevue/inputnumber'; |
| 60 | +import { InputHTMLAttributes, computed, useAttrs } from 'vue'; |
| 61 | +
|
| 62 | +import Label from '../Label/Label.vue'; |
| 63 | +import { InputNumberMode } from './constants'; |
| 64 | +
|
| 65 | +const attrs = useAttrs(); |
| 66 | +
|
| 67 | +const props = withDefaults( |
| 68 | + defineProps<{ |
| 69 | + disabled?: InputNumberProps['disabled']; |
| 70 | + error?: boolean; |
| 71 | + helperText?: string; |
| 72 | + hideLabel?: boolean; |
| 73 | + id: InputNumberProps['inputId']; |
| 74 | + label: string; |
| 75 | + max?: InputNumberProps['max']; |
| 76 | + maxFractionDigits?: InputNumberProps['maxFractionDigits']; |
| 77 | + min?: InputNumberProps['min']; |
| 78 | + minFractionDigits?: InputNumberProps['minFractionDigits']; |
| 79 | + mode?: InputNumberMode; |
| 80 | + modelValue?: number; |
| 81 | + name: string; |
| 82 | + placeholder?: InputNumberProps['placeholder']; |
| 83 | + readonly?: InputNumberProps['readonly']; |
| 84 | + required?: boolean; |
| 85 | + success?: boolean; |
| 86 | + }>(), |
| 87 | + { |
| 88 | + disabled: false, |
| 89 | + error: false, |
| 90 | + helperText: undefined, |
| 91 | + hideLabel: false, |
| 92 | + max: undefined, |
| 93 | + maxFractionDigits: undefined, |
| 94 | + min: undefined, |
| 95 | + minFractionDigits: undefined, |
| 96 | + mode: InputNumberMode.DECIMAL, |
| 97 | + modelValue: 0, |
| 98 | + placeholder: undefined, |
| 99 | + readonly: false, |
| 100 | + required: false, |
| 101 | + success: false |
| 102 | + } |
| 103 | +); |
| 104 | +
|
| 105 | +const emits = defineEmits<{ |
| 106 | + (e: 'update:modelValue', value: number): void; |
| 107 | + (e: 'change', value: number): void; |
| 108 | + (e: 'focus'): void; |
| 109 | + (e: 'blur'): void; |
| 110 | +}>(); |
| 111 | +
|
| 112 | +const helperTextId = computed(() => `${props.id}-helper`); |
| 113 | +const computedInputProps = computed<InputHTMLAttributes>(() => { |
| 114 | + return { |
| 115 | + ...attrs, |
| 116 | + 'aria-describedby': props.helperText ? helperTextId.value : undefined, |
| 117 | + name: props.name, |
| 118 | + required: props.required |
| 119 | + }; |
| 120 | +}); |
| 121 | +const currency = computed(() => |
| 122 | + props.mode === InputNumberMode.CURRENCY ? 'USD' : undefined |
| 123 | +); |
| 124 | +</script> |
| 125 | + |
| 126 | +<style lang="scss"> |
| 127 | +.uic-input-number-container { |
| 128 | + @apply flex flex-col gap-1; |
| 129 | +} |
| 130 | +
|
| 131 | +.uic-input-number { |
| 132 | + @apply px-4 py-3; |
| 133 | + @apply text-sm text-gray-800; |
| 134 | + @apply bg-white; |
| 135 | + @apply border border-gray-200 rounded; |
| 136 | + outline: none; |
| 137 | +
|
| 138 | + &:hover { |
| 139 | + @apply border-gray-300; |
| 140 | + } |
| 141 | +
|
| 142 | + &:focus-within { |
| 143 | + @apply border-gray-400; |
| 144 | + } |
| 145 | +
|
| 146 | + &::placeholder { |
| 147 | + @apply text-gray-200; |
| 148 | + } |
| 149 | +
|
| 150 | + &.uic-error { |
| 151 | + @apply bg-red-50; |
| 152 | + @apply border-red-600; |
| 153 | + @apply text-red-600; |
| 154 | +
|
| 155 | + &::placeholder { |
| 156 | + @apply text-red-600; |
| 157 | + } |
| 158 | + } |
| 159 | +
|
| 160 | + &.uic-success { |
| 161 | + @apply bg-green-50; |
| 162 | + @apply border-green-700; |
| 163 | + @apply text-green-700; |
| 164 | +
|
| 165 | + &::placeholder { |
| 166 | + @apply text-green-700; |
| 167 | + } |
| 168 | + } |
| 169 | +
|
| 170 | + &:disabled { |
| 171 | + @apply bg-gray-50; |
| 172 | + @apply text-gray-300; |
| 173 | + @apply border-gray-200; |
| 174 | +
|
| 175 | + &::placeholder { |
| 176 | + @apply text-gray-300; |
| 177 | + } |
| 178 | + } |
| 179 | +} |
| 180 | +.uic-input-number-helper { |
| 181 | + @apply text-xs text-gray-500; |
| 182 | +
|
| 183 | + &.uic-error { |
| 184 | + @apply text-red-600; |
| 185 | + } |
| 186 | +
|
| 187 | + &.uic-success { |
| 188 | + @apply text-green-700; |
| 189 | + } |
| 190 | +} |
| 191 | +</style> |
0 commit comments