Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 2059556ffe | |||
| a95cf8cdfb |
@@ -88,11 +88,46 @@ describe('MalioSelect', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
await wrapper.get('button').trigger('click')
|
await wrapper.get('button').trigger('click')
|
||||||
await wrapper.findAll('li')[2].trigger('click')
|
await wrapper.findAll('li')[1].trigger('click')
|
||||||
|
|
||||||
expect(wrapper.emitted('update:modelValue')?.[0]).toEqual(['be'])
|
expect(wrapper.emitted('update:modelValue')?.[0]).toEqual(['be'])
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('does not render empty option when emptyOptionLabel is empty', async () => {
|
||||||
|
const wrapper = mount(SelectForTest, {
|
||||||
|
props: {
|
||||||
|
modelValue: null,
|
||||||
|
options: [
|
||||||
|
{label: 'AM', value: 'am'},
|
||||||
|
{label: 'PM', value: 'pm'},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
await wrapper.get('button').trigger('click')
|
||||||
|
|
||||||
|
const items = wrapper.findAll('li[role="option"]')
|
||||||
|
expect(items).toHaveLength(2)
|
||||||
|
expect(items[0].text()).toBe('AM')
|
||||||
|
expect(items[1].text()).toBe('PM')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('renders empty option when emptyOptionLabel is provided', async () => {
|
||||||
|
const wrapper = mount(SelectForTest, {
|
||||||
|
props: {
|
||||||
|
modelValue: null,
|
||||||
|
options: [{label: 'AM', value: 'am'}],
|
||||||
|
emptyOptionLabel: 'Choisir...',
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
await wrapper.get('button').trigger('click')
|
||||||
|
|
||||||
|
const items = wrapper.findAll('li[role="option"]')
|
||||||
|
expect(items).toHaveLength(2)
|
||||||
|
expect(items[0].text()).toBe('Choisir...')
|
||||||
|
})
|
||||||
|
|
||||||
it('renders the empty option with muted text style', async () => {
|
it('renders the empty option with muted text style', async () => {
|
||||||
const wrapper = mount(SelectForTest, {
|
const wrapper = mount(SelectForTest, {
|
||||||
props: {
|
props: {
|
||||||
|
|||||||
@@ -208,10 +208,10 @@ const buttonId = `custom-select-btn-${uid}`
|
|||||||
const listboxId = `custom-select-listbox-${uid}`
|
const listboxId = `custom-select-listbox-${uid}`
|
||||||
const listRef = ref<HTMLElement | null>(null)
|
const listRef = ref<HTMLElement | null>(null)
|
||||||
const listHeight = ref(0)
|
const listHeight = ref(0)
|
||||||
const normalizedOptions = computed<Option[]>(() => [
|
const normalizedOptions = computed<Option[]>(() => {
|
||||||
{label: props.emptyOptionLabel, value: null},
|
if (!props.emptyOptionLabel) return props.options
|
||||||
...props.options,
|
return [{label: props.emptyOptionLabel, value: null}, ...props.options]
|
||||||
])
|
})
|
||||||
const mergedGroupClass = computed(() =>
|
const mergedGroupClass = computed(() =>
|
||||||
twMerge('relative w-full', props.minWidth, props.maxWidth, props.groupClass),
|
twMerge('relative w-full', props.minWidth, props.maxWidth, props.groupClass),
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ type SelectCheckboxProps = {
|
|||||||
displaySelectAll?: boolean
|
displaySelectAll?: boolean
|
||||||
selectAllLabel?: string
|
selectAllLabel?: string
|
||||||
disabled?: boolean
|
disabled?: boolean
|
||||||
|
groupClass?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
const SelectCheckboxForTest = SelectCheckbox as DefineComponent<SelectCheckboxProps>
|
const SelectCheckboxForTest = SelectCheckbox as DefineComponent<SelectCheckboxProps>
|
||||||
@@ -175,4 +176,21 @@ describe('MalioSelectCheckbox', () => {
|
|||||||
const checkboxes = wrapper.findAll('input[type="checkbox"]')
|
const checkboxes = wrapper.findAll('input[type="checkbox"]')
|
||||||
expect((checkboxes[0].element as HTMLInputElement).checked).toBe(false)
|
expect((checkboxes[0].element as HTMLInputElement).checked).toBe(false)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('applies minWidth via twMerge so it overrides w-full (parity with MalioSelect)', () => {
|
||||||
|
const wrapper = mount(SelectCheckboxForTest, {
|
||||||
|
props: {modelValue: [], options: [], minWidth: 'w-80'},
|
||||||
|
})
|
||||||
|
const root = wrapper.find('button').element.parentElement
|
||||||
|
expect(root?.className).toContain('w-80')
|
||||||
|
expect(root?.className).not.toContain('w-full')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('applies groupClass via twMerge', () => {
|
||||||
|
const wrapper = mount(SelectCheckboxForTest, {
|
||||||
|
props: {modelValue: [], options: [], groupClass: 'mt-4'},
|
||||||
|
})
|
||||||
|
const root = wrapper.find('button').element.parentElement
|
||||||
|
expect(root?.className).toContain('mt-4')
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -2,8 +2,7 @@
|
|||||||
<div>
|
<div>
|
||||||
<div
|
<div
|
||||||
ref="root"
|
ref="root"
|
||||||
class="relative w-full"
|
:class="mergedGroupClass"
|
||||||
:class="[minWidth, maxWidth]"
|
|
||||||
>
|
>
|
||||||
<button
|
<button
|
||||||
:id="buttonId"
|
:id="buttonId"
|
||||||
@@ -26,7 +25,7 @@
|
|||||||
? openDirection === 'down'
|
? openDirection === 'down'
|
||||||
? 'rounded-b-none !border-2 !border-m-primary !border-b-0'
|
? 'rounded-b-none !border-2 !border-m-primary !border-b-0'
|
||||||
: 'rounded-t-none !border-2 !border-m-primary !border-t-0'
|
: 'rounded-t-none !border-2 !border-m-primary !border-t-0'
|
||||||
: isOptionSelected
|
: isOptionSelected
|
||||||
? 'border-black'
|
? 'border-black'
|
||||||
: 'border-m-muted',
|
: 'border-m-muted',
|
||||||
disabled ? 'cursor-not-allowed border-m-muted text-black/60' : 'cursor-pointer',
|
disabled ? 'cursor-not-allowed border-m-muted text-black/60' : 'cursor-pointer',
|
||||||
@@ -45,7 +44,7 @@
|
|||||||
v-if="label"
|
v-if="label"
|
||||||
class="floating-label pointer-events-none absolute left-3 inline-block origin-left transition-transform duration-150 font-medium"
|
class="floating-label pointer-events-none absolute left-3 inline-block origin-left transition-transform duration-150 font-medium"
|
||||||
:class="[
|
:class="[
|
||||||
shouldFloatLabel ? 'top-2 z-30' : 'top-1/2 -translate-y-1/2',
|
isOpen ? 'top-2 z-30' : 'top-2',
|
||||||
hasError
|
hasError
|
||||||
? 'text-m-danger'
|
? 'text-m-danger'
|
||||||
: hasSuccess
|
: hasSuccess
|
||||||
@@ -206,6 +205,7 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import {computed, onBeforeUnmount, onMounted, ref, useId, nextTick} from 'vue'
|
import {computed, onBeforeUnmount, onMounted, ref, useId, nextTick} from 'vue'
|
||||||
import {Icon as IconifyIcon} from '@iconify/vue'
|
import {Icon as IconifyIcon} from '@iconify/vue'
|
||||||
|
import {twMerge} from 'tailwind-merge'
|
||||||
import Checkbox from '../checkbox/Checkbox.vue'
|
import Checkbox from '../checkbox/Checkbox.vue'
|
||||||
|
|
||||||
defineOptions({name: 'MalioSelectCheckbox', inheritAttrs: false})
|
defineOptions({name: 'MalioSelectCheckbox', inheritAttrs: false})
|
||||||
@@ -232,6 +232,7 @@ const props = withDefaults(defineProps<{
|
|||||||
displaySelectAll?: boolean
|
displaySelectAll?: boolean
|
||||||
selectAllLabel?: string
|
selectAllLabel?: string
|
||||||
disabled?: boolean
|
disabled?: boolean
|
||||||
|
groupClass?: string
|
||||||
}>(), {
|
}>(), {
|
||||||
options: () => [],
|
options: () => [],
|
||||||
emptyOptionLabel: '',
|
emptyOptionLabel: '',
|
||||||
@@ -249,6 +250,7 @@ const props = withDefaults(defineProps<{
|
|||||||
displaySelectAll: false,
|
displaySelectAll: false,
|
||||||
selectAllLabel: 'Tout sélectionner',
|
selectAllLabel: 'Tout sélectionner',
|
||||||
disabled: false,
|
disabled: false,
|
||||||
|
groupClass: '',
|
||||||
})
|
})
|
||||||
|
|
||||||
const emit = defineEmits<{
|
const emit = defineEmits<{
|
||||||
@@ -264,6 +266,9 @@ const listboxId = `custom-select-listbox-${uid}`
|
|||||||
const listRef = ref<HTMLElement | null>(null)
|
const listRef = ref<HTMLElement | null>(null)
|
||||||
const listHeight = ref(0)
|
const listHeight = ref(0)
|
||||||
const normalizedOptions = computed<Option[]>(() => props.options)
|
const normalizedOptions = computed<Option[]>(() => props.options)
|
||||||
|
const mergedGroupClass = computed(() =>
|
||||||
|
twMerge('relative w-full', props.minWidth, props.maxWidth, props.groupClass),
|
||||||
|
)
|
||||||
const hasError = computed(() => !!props.error)
|
const hasError = computed(() => !!props.error)
|
||||||
const hasSuccess = computed(() => !!props.success && !hasError.value)
|
const hasSuccess = computed(() => !!props.success && !hasError.value)
|
||||||
const isOptionSelected = computed(() =>
|
const isOptionSelected = computed(() =>
|
||||||
@@ -281,6 +286,10 @@ const shouldFloatLabel = computed(() =>
|
|||||||
const selectionSummary = computed(() =>
|
const selectionSummary = computed(() =>
|
||||||
`${props.modelValue.length}/${normalizedOptions.value.length}`
|
`${props.modelValue.length}/${normalizedOptions.value.length}`
|
||||||
)
|
)
|
||||||
|
const allSelected = computed(() =>
|
||||||
|
normalizedOptions.value.length > 0
|
||||||
|
&& normalizedOptions.value.every(opt => props.modelValue.includes(opt.value)),
|
||||||
|
)
|
||||||
const describedBy = computed(() =>
|
const describedBy = computed(() =>
|
||||||
(hasError.value || hasSuccess.value || !!props.hint) ? `${buttonId}-describedby` : undefined,
|
(hasError.value || hasSuccess.value || !!props.hint) ? `${buttonId}-describedby` : undefined,
|
||||||
)
|
)
|
||||||
@@ -320,18 +329,22 @@ function open() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const labelTransformStyle = computed(() => {
|
const labelTransformStyle = computed(() => {
|
||||||
|
// label non flottant
|
||||||
if (!shouldFloatLabel.value) {
|
if (!shouldFloatLabel.value) {
|
||||||
return undefined
|
return {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// fermé ou ouverture vers le bas : comportement classique
|
||||||
if (!isOpen.value || openDirection.value === 'down') {
|
if (!isOpen.value || openDirection.value === 'down') {
|
||||||
return {
|
return {
|
||||||
transform: 'translateY(-1.15rem) scale(0.9)',
|
transform: 'translateY(-1.15rem) scale(0.9)',
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ouverture vers le haut : on remonte en fonction de la hauteur de la liste
|
||||||
const extraOffset = 8 // marge visuelle au-dessus de la liste en px
|
const extraOffset = 8 // marge visuelle au-dessus de la liste en px
|
||||||
const total = 4 + listHeight.value + extraOffset
|
const total = 4 +listHeight.value + extraOffset
|
||||||
|
// 18 ≈ 1.15rem pour garder la même base que votre flottant actuel
|
||||||
|
|
||||||
return {
|
return {
|
||||||
transform: `translateY(-${total}px) scale(0.9)`,
|
transform: `translateY(-${total}px) scale(0.9)`,
|
||||||
@@ -351,19 +364,6 @@ function toggle() {
|
|||||||
open()
|
open()
|
||||||
}
|
}
|
||||||
|
|
||||||
const allSelected = computed(() =>
|
|
||||||
normalizedOptions.value.length > 0
|
|
||||||
&& normalizedOptions.value.every(opt => props.modelValue.includes(opt.value)),
|
|
||||||
)
|
|
||||||
|
|
||||||
function toggleAll() {
|
|
||||||
if (allSelected.value) {
|
|
||||||
emit('update:modelValue', [])
|
|
||||||
} else {
|
|
||||||
emit('update:modelValue', normalizedOptions.value.map(opt => opt.value))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function isChecked(value: string | number) {
|
function isChecked(value: string | number) {
|
||||||
return props.modelValue.includes(value)
|
return props.modelValue.includes(value)
|
||||||
}
|
}
|
||||||
@@ -373,10 +373,17 @@ function toggleOption(value: string | number) {
|
|||||||
emit('update:modelValue', props.modelValue.filter(item => item !== value))
|
emit('update:modelValue', props.modelValue.filter(item => item !== value))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
emit('update:modelValue', [...props.modelValue, value])
|
emit('update:modelValue', [...props.modelValue, value])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function toggleAll() {
|
||||||
|
if (allSelected.value) {
|
||||||
|
emit('update:modelValue', [])
|
||||||
|
} else {
|
||||||
|
emit('update:modelValue', normalizedOptions.value.map(opt => opt.value))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function onClickOutside(e: MouseEvent) {
|
function onClickOutside(e: MouseEvent) {
|
||||||
if (!root.value) return
|
if (!root.value) return
|
||||||
if (!root.value.contains(e.target as Node)) close()
|
if (!root.value.contains(e.target as Node)) close()
|
||||||
|
|||||||
Reference in New Issue
Block a user