Skip to content

Instantly share code, notes, and snippets.

@AlexVipond
Forked from lmiller1990/app.vue
Created November 9, 2020 02:09
Show Gist options
  • Save AlexVipond/5d02d6702262f0e97544fb19cc007be2 to your computer and use it in GitHub Desktop.
Save AlexVipond/5d02d6702262f0e97544fb19cc007be2 to your computer and use it in GitHub Desktop.
Renderless Components
<template>
<v-input
v-slot="{ error }"
:min="5"
:max="10"
:value="username"
:validation-algorthm
>
<input v-model="username" />
<div v-if="error" class="error">
{{ error }}
</div>
</v-input>
</template>
<script>
import { ref } from 'vue'
import VInput from './v-input.vue'
export default {
components: { VInput },
setup() {
return {
username: ref('')
}
}
}
</script>
<style>
.error { color: red; }
</style>
<script>
function getError(value, { min, max }) {
if (!value) {
return 'Required'
}
if (value.length < min) {
return `Min is ${min}`
}
if (value.length > max) {
return `Max is ${max}`
}
}
import { computed } from 'vue'
export default {
props: ['min', 'max', 'value'],
setup(props, ctx) {
const error = computed(() => getError(
props.value,
{ min: props.min, max: props.max }
))
return () => ctx.slots.default({
error: error.value
})
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment