Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: first validation on leave, not type + number value for input #34

Merged
merged 1 commit into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions src/components/BuiInput/BuiInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
:disabled="disabled"
:required="required"
:class="[inputClasses, $slots.prefix ? 'pl-10' : '']"
@focusin="emit('focus')"
@blur="emit('blur')"
@focusin="handleFocus"
@blur="handleBlur"
:placeholder="placeholder"
/>
<div v-if="$slots.suffix" class="absolute right-2.5 bottom-2.5">
Expand All @@ -43,7 +43,7 @@ type ValidationStatus = 'success' | 'error'
interface InputProps {
disabled?: boolean
label?: string
value?: string
value?: string | number
required?: boolean
validationStatus?: ValidationStatus | null
placeholder?: string | undefined
Expand All @@ -70,6 +70,23 @@ const model = computed({
})

const isDirty = ref(false)
const isInFocus = ref(false)
function handleBlur() {
isInFocus.value = false
isDirty.value = true
emit('blur')
}

function handleFocus() {
isInFocus.value = true
emit('focus')
}

watch(model, (newValue, oldValue) => {
if (newValue !== oldValue && !isDirty.value && !isInFocus.value) {
isDirty.value = true
}
})

const inputClasses = computed(() =>
twMerge(
Expand All @@ -96,10 +113,4 @@ const validationWrapperClasses = computed(() =>
(props.validationStatus === null || (!props.hasForcedValidation && !isDirty.value)) && 'hidden'
)
)

watch(model, (newValue, oldValue) => {
if (newValue !== oldValue && !isDirty.value) {
isDirty.value = true
}
})
</script>
9 changes: 7 additions & 2 deletions src/components/BuiSelect/BuiSelect.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</span>

<span class="relative flex">
<select v-model="model" :disabled="disabled" :class="selectClasses">
<select v-model="model" :disabled="disabled" :class="selectClasses" @blur="handleBlur">
<option disabled selected value="">
{{ placeholder }}
</option>
Expand Down Expand Up @@ -59,7 +59,7 @@ const props = withDefaults(defineProps<InputProps>(), {
hasForcedValidation: false,
validationStatus: null
})
const emit = defineEmits(['input'])
const emit = defineEmits(['input', 'blur'])

const model = computed({
get() {
Expand All @@ -71,6 +71,11 @@ const model = computed({
})

const isDirty = ref(false)
function handleBlur() {
isDirty.value = true
emit('blur')
}

watch(model, (newValue, oldValue) => {
if (newValue !== oldValue && !isDirty.value) {
isDirty.value = true
Expand Down