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

feat: Added validation for Select #33

Merged
merged 2 commits into from
Dec 5, 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
2 changes: 1 addition & 1 deletion src/components/BuiInput/BuiInput.story.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const modelForDisabling = ref(false)
:required="true"
:validation-status="model.length > 0 ? undefined : 'error'"
>
<template #validationMessage v-if="model.length <= 0"> Some error text </template>
<template #validationMessage> Some error text </template>
</BuiInput>
</div>
</Variant>
Expand Down
2 changes: 1 addition & 1 deletion src/components/BuiInput/BuiInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ const validationWrapperClasses = computed(() =>
props.validationStatus === 'error' && (!!props.hasForcedValidation || isDirty.value)
? 'text-red-300'
: '',
!props.hasForcedValidation && !isDirty.value && 'hidden'
(props.validationStatus === null || (!props.hasForcedValidation && !isDirty.value)) && 'hidden'
)
)

Expand Down
47 changes: 45 additions & 2 deletions src/components/BuiSelect/BuiSelect.story.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import BuiSelect from './BuiSelect.vue'
import BuiInput from '../BuiInput/BuiInput.vue'
import { ref } from 'vue'

const model = ref(null)
const model = ref('')
const options = [
{
name: 'One',
Expand All @@ -20,7 +20,18 @@ const options = [
<Story title="BuiSelect" autoPropsDisabled :layout="{ type: 'grid', width: '400px' }">
<Variant title="Default">
<div class="p-2">
<BuiSelect label="Default" v-model="model" :options="options" :required="true" />
<BuiSelect label="Default" v-model="model" :options="options" />
</div>
</Variant>
<Variant title="Required">
<div class="p-2">
<BuiSelect
label="Default"
v-model="model"
:options="options"
:required="true"
:validation-status="model ? null : 'error'"
/>
</div>
</Variant>
<Variant title="Disabled">
Expand All @@ -34,5 +45,37 @@ const options = [
<BuiInput label="Default" :required="true" />
</div>
</Variant>
<Variant title="With error status forced">
<div class="p-2">
<BuiSelect
label="Default"
:options="options"
:validation-status="'error'"
:has-forced-validation="true"
>
<template #validationMessage> Some error message </template>
</BuiSelect>
</div>
</Variant>
<Variant title="With success status forced">
<BuiSelect
label="Default"
:options="options"
:validation-status="'success'"
:has-forced-validation="true"
>
<template #validationMessage> Some success message </template>
</BuiSelect>
</Variant>
<Variant title="Required with dynamic validation">
<BuiSelect
label="Select right option (2)"
:options="options"
v-model="model"
:validation-status="model !== '1' ? null : 'error'"
>
<template #validationMessage> Do not select 'One'</template>
</BuiSelect>
</Variant>
</Story>
</template>
42 changes: 38 additions & 4 deletions src/components/BuiSelect/BuiSelect.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,41 @@
</svg>
</span>
</span>
<div v-if="$slots.validationMessage" :class="validationWrapperClasses">
<slot name="validationMessage" />
</div>
</label>
</template>

<script lang="ts" setup>
import { computed } from 'vue'
import { computed, ref, watch } from 'vue'
import { twMerge } from 'tailwind-merge'
import RequiredIcon from '@/components/CommonElements/RequiredIcon.vue'

type ValidationStatus = 'success' | 'error'
type OptionsType = {
name: string
value: any
}
interface InputProps {
value?: string
value?: any
label?: string
options?: OptionsType[]
placeholder?: string
disabled?: boolean
required?: boolean
hasForcedValidation?: boolean
validationStatus?: ValidationStatus | null
}
const props = withDefaults(defineProps<InputProps>(), {
value: '',
label: '',
options: () => [],
placeholder: 'Please select one',
disabled: false,
required: false
required: false,
hasForcedValidation: false,
validationStatus: null
})
const emit = defineEmits(['input'])

Expand All @@ -62,6 +70,13 @@ const model = computed({
}
})

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

const defaultLabelClasses =
'flex flex-row gap-2 mb-1 text-sm font-semibold leading-6 text-gray-900 dark:text-white'
const defaultSelectClasses =
Expand All @@ -75,9 +90,28 @@ const selectIconClasses = computed(() =>
)
)
const selectClasses = computed(() => {
return twMerge(defaultSelectClasses, props.disabled && disabledSelectClasses)
return twMerge(
defaultSelectClasses,
props.validationStatus === 'success' &&
'border-green-300 focus:border-green-300 focus:ring-green-200',
props.validationStatus === 'error' &&
(!!props.hasForcedValidation || isDirty.value) &&
'border-red-300 focus:border-red-300 focus:ring-red-200',
props.disabled && disabledSelectClasses
)
})

const validationWrapperClasses = computed(() =>
twMerge(
'text-sm font-normal mt-1',
props.validationStatus === 'success' ? 'text-green-300' : '',
props.validationStatus === 'error' && (!!props.hasForcedValidation || isDirty.value)
? 'text-red-300'
: '',
(props.validationStatus === null || (!props.hasForcedValidation && !isDirty.value)) && 'hidden'
)
)

const labelClasses = computed(() => {
return defaultLabelClasses
})
Expand Down