Refactor #3922 - For Dropdown

pull/3938/head
Tuğçe Küçükoğlu 2023-05-07 23:37:26 +03:00
parent 316f94470c
commit 133c543eab
4 changed files with 213 additions and 25 deletions

View File

@ -256,6 +256,12 @@ const DropdownProps = [
type: 'string', type: 'string',
default: 'null', default: 'null',
description: 'Identifier of the underlying input element.' description: 'Identifier of the underlying input element.'
},
{
name: 'pt',
type: 'any',
default: 'null',
description: 'Uses to pass attributes to DOM elements inside the component.'
} }
]; ];

View File

@ -23,6 +23,7 @@ import { DeferredContentPassThroughOptions } from '../deferredcontent';
import { DialogPassThroughOptions } from '../dialog'; import { DialogPassThroughOptions } from '../dialog';
import { DividerPassThroughOptions } from '../divider'; import { DividerPassThroughOptions } from '../divider';
import { DockPassThroughOptions } from '../dock'; import { DockPassThroughOptions } from '../dock';
import { DropdownPassThroughOptions } from '../dropdown';
import { EditorPassThroughOptions } from '../editor'; import { EditorPassThroughOptions } from '../editor';
import { FieldsetPassThroughOptions } from '../fieldset'; import { FieldsetPassThroughOptions } from '../fieldset';
import { FileUploadPassThroughOptions } from '../fileupload'; import { FileUploadPassThroughOptions } from '../fileupload';
@ -108,6 +109,7 @@ interface PrimeVuePTOptions {
divider?: DividerPassThroughOptions; divider?: DividerPassThroughOptions;
dialog?: DialogPassThroughOptions; dialog?: DialogPassThroughOptions;
dock?: DockPassThroughOptions; dock?: DockPassThroughOptions;
dropdown?: DropdownPassThroughOptions;
dynamicdialog?: DialogPassThroughOptions; dynamicdialog?: DialogPassThroughOptions;
editor?: EditorPassThroughOptions; editor?: EditorPassThroughOptions;
fieldset?: FieldsetPassThroughOptions; fieldset?: FieldsetPassThroughOptions;

View File

@ -9,7 +9,18 @@
*/ */
import { HTMLAttributes, InputHTMLAttributes, VNode } from 'vue'; import { HTMLAttributes, InputHTMLAttributes, VNode } from 'vue';
import { ClassComponent, GlobalComponentConstructor } from '../ts-helpers'; import { ClassComponent, GlobalComponentConstructor } from '../ts-helpers';
import { VirtualScrollerItemOptions, VirtualScrollerProps } from '../virtualscroller'; import { VirtualScrollerItemOptions, VirtualScrollerPassThroughOptionType, VirtualScrollerProps } from '../virtualscroller';
export declare type DropdownPassThroughOptionType = DropdownPassThroughAttributes | ((options: DropdownPassThroughMethodOptions) => DropdownPassThroughAttributes) | null | undefined;
/**
* Custom passthrough(pt) option method.
*/
export interface DropdownPassThroughMethodOptions {
props: DropdownProps;
state: DropdownState;
context: DropdownContext;
}
/** /**
* Custom change event. * Custom change event.
@ -41,6 +52,151 @@ export interface DropdownFilterEvent {
value: any; value: any;
} }
/**
* Custom passthrough(pt) options.
* @see {@link DropdownProps.pt}
*/
export interface DropdownPassThroughOptions {
/**
* Uses to pass attributes to the root's DOM element.
*/
root?: DropdownPassThroughOptionType;
/**
* Uses to pass attributes to the input's DOM element.
*/
input?: DropdownPassThroughOptionType;
/**
* Uses to pass attributes to the clear icon's DOM element.
*/
clearIcon?: DropdownPassThroughOptionType;
/**
* Uses to pass attributes to the trigger' DOM element.
*/
trigger?: DropdownPassThroughOptionType;
/**
* Uses to pass attributes to the loading icon's DOM element.
*/
loadingIcon?: DropdownPassThroughOptionType;
/**
* Uses to pass attributes to the panel's DOM element.
*/
panel?: DropdownPassThroughOptionType;
/**
* Uses to pass attributes to the header's DOM element.
*/
header?: DropdownPassThroughOptionType;
/**
* Uses to pass attributes to the filter container's DOM element.
*/
filterContainer?: DropdownPassThroughOptionType;
/**
* Uses to pass attributes to the filter input's DOM element.
*/
filterInput?: DropdownPassThroughOptionType;
/**
* Uses to pass attributes to the filter icon's DOM element.
*/
filterIcon?: DropdownPassThroughOptionType;
/**
* Uses to pass attributes to the wrapper's DOM element.
*/
wrapper?: DropdownPassThroughOptionType;
/**
* Uses to pass attributes to the VirtualScroller component.
* @see {@link VirtualScrollerPassThroughOptionType}
*/
virtualScroller?: VirtualScrollerPassThroughOptionType;
/**
* Uses to pass attributes to the list's DOM element.
*/
list?: DropdownPassThroughOptionType;
/**
* Uses to pass attributes to the item group's DOM element.
*/
itemGroup?: DropdownPassThroughOptionType;
/**
* Uses to pass attributes to the item's DOM element.
*/
item?: DropdownPassThroughOptionType;
/**
* Uses to pass attributes to the emptyMessage's DOM element.
*/
emptyMessage?: DropdownPassThroughOptionType;
/**
* Uses to pass attributes to the hidden first focusable element's DOM element.
*/
hiddenFirstFocusableEl?: DropdownPassThroughOptionType;
/**
* Uses to pass attributes to the hidden filter result's DOM element.
*/
hiddenFilterResult?: DropdownPassThroughOptionType;
/**
* Uses to pass attributes to the hidden selected message's DOM element.
*/
hiddenSelectedMessage?: DropdownPassThroughOptionType;
/**
* Uses to pass attributes to the hidden last focusable element's DOM element.
*/
hiddenLastFocusableEl?: DropdownPassThroughOptionType;
}
/**
* Custom passthrough attributes for each DOM elements
*/
export interface DropdownPassThroughAttributes {
[key: string]: any;
}
/**
* Defines current inline state in Dropdown component.
*/
export interface DropdownState {
/**
* Current id state as a string.
*/
id: string;
/**
* Current focused state as a boolean.
* @defaultValue false
*/
focused: boolean;
/**
* Current focused item index as a number.
* @defaultvalue -1
*/
focusedOptionIndex: number;
/**
* Current filter value state as a string.
*/
filterValue: string;
/**
* Current overlay visible state as a boolean.
* @defaultValue false
*/
overlayVisible: boolean;
}
/**
* Defines current options in Dropdown component.
*/
export interface DropdownContext {
/**
* Current selection state of the item as a boolean.
* @defaultValue false
*/
selected: boolean;
/**
* Current focus state of the item as a boolean.
* @defaultValue false
*/
focused: boolean;
/**
* Current disabled state of the item as a boolean.
* @defaultValue false
*/
disabled: boolean;
}
/** /**
* Defines valid properties in Dropdown component. * Defines valid properties in Dropdown component.
*/ */
@ -251,6 +407,11 @@ export interface DropdownProps {
* Identifier of the underlying input element. * Identifier of the underlying input element.
*/ */
'aria-labelledby'?: string | undefined; 'aria-labelledby'?: string | undefined;
/**
* Uses to pass attributes to DOM elements inside the component.
* @type {DropdownPassThroughOptions}
*/
pt?: DropdownPassThroughOptions;
} }
/** /**

View File

@ -1,5 +1,5 @@
<template> <template>
<div ref="container" :id="id" :class="containerClass" @click="onContainerClick"> <div ref="container" :id="id" :class="containerClass" @click="onContainerClick" v-bind="ptm('root')">
<input <input
v-if="editable" v-if="editable"
ref="focusInput" ref="focusInput"
@ -23,7 +23,7 @@
@blur="onBlur" @blur="onBlur"
@keydown="onKeyDown" @keydown="onKeyDown"
@input="onEditableInput" @input="onEditableInput"
v-bind="inputProps" v-bind="{ ...inputProps, ...ptm('input') }"
/> />
<span <span
v-else v-else
@ -43,29 +43,29 @@
@focus="onFocus" @focus="onFocus"
@blur="onBlur" @blur="onBlur"
@keydown="onKeyDown" @keydown="onKeyDown"
v-bind="inputProps" v-bind="{ ...inputProps, ...ptm('input') }"
> >
<slot name="value" :value="modelValue" :placeholder="placeholder">{{ label === 'p-emptylabel' ? '&nbsp;' : label || 'empty' }}</slot> <slot name="value" :value="modelValue" :placeholder="placeholder">{{ label === 'p-emptylabel' ? '&nbsp;' : label || 'empty' }}</slot>
</span> </span>
<slot v-if="showClear && modelValue != null" name="clearicon" :onClick="onClearClick"> <slot v-if="showClear && modelValue != null" name="clearicon" :onClick="onClearClick">
<component :is="clearIcon ? 'i' : 'TimesIcon'" :class="['p-dropdown-clear-icon', clearIcon]" @click="onClearClick" v-bind="clearIconProps" /> <component :is="clearIcon ? 'i' : 'TimesIcon'" :class="['p-dropdown-clear-icon', clearIcon]" @click="onClearClick" v-bind="{ ...clearIconProps, ...ptm('clearIcon') }" />
</slot> </slot>
<div class="p-dropdown-trigger"> <div class="p-dropdown-trigger" v-bind="ptm('trigger')">
<slot v-if="loading" name="loadingicon" class="p-dropdown-trigger-icon"> <slot v-if="loading" name="loadingicon" class="p-dropdown-trigger-icon">
<span v-if="loadingIcon" :class="['p-dropdown-trigger-icon pi-spin', loadingIcon]" aria-hidden="true" /> <span v-if="loadingIcon" :class="['p-dropdown-trigger-icon pi-spin', loadingIcon]" aria-hidden="true" v-bind="ptm('loadingIcon')" />
<SpinnerIcon v-else class="p-dropdown-trigger-icon" spin aria-hidden="true" /> <SpinnerIcon v-else class="p-dropdown-trigger-icon" spin aria-hidden="true" v-bind="ptm('loadingIcon')" />
</slot> </slot>
<slot v-else name="dropdownicon" class="p-dropdown-trigger-icon"> <slot v-else name="dropdownicon" class="p-dropdown-trigger-icon">
<component :is="dropdownIcon ? 'span' : 'ChevronDownIcon'" :class="['p-dropdown-trigger-icon', dropdownIcon]" aria-hidden="true" /> <component :is="dropdownIcon ? 'span' : 'ChevronDownIcon'" :class="['p-dropdown-trigger-icon', dropdownIcon]" aria-hidden="true" v-bind="ptm('dropdownIcon')" />
</slot> </slot>
</div> </div>
<Portal :appendTo="appendTo"> <Portal :appendTo="appendTo">
<transition name="p-connected-overlay" @enter="onOverlayEnter" @after-enter="onOverlayAfterEnter" @leave="onOverlayLeave" @after-leave="onOverlayAfterLeave"> <transition name="p-connected-overlay" @enter="onOverlayEnter" @after-enter="onOverlayAfterEnter" @leave="onOverlayLeave" @after-leave="onOverlayAfterLeave">
<div v-if="overlayVisible" :ref="overlayRef" :style="panelStyle" :class="panelStyleClass" @click="onOverlayClick" @keydown="onOverlayKeyDown" v-bind="panelProps"> <div v-if="overlayVisible" :ref="overlayRef" :style="panelStyle" :class="panelStyleClass" @click="onOverlayClick" @keydown="onOverlayKeyDown" v-bind="{ ...panelProps, ...ptm('panel') }">
<span ref="firstHiddenFocusableElementOnOverlay" role="presentation" aria-hidden="true" class="p-hidden-accessible p-hidden-focusable" :tabindex="0" @focus="onFirstHiddenFocus"></span> <span ref="firstHiddenFocusableElementOnOverlay" role="presentation" aria-hidden="true" class="p-hidden-accessible p-hidden-focusable" :tabindex="0" @focus="onFirstHiddenFocus" v-bind="ptm('hiddenFirstFocusableEl')"></span>
<slot name="header" :value="modelValue" :options="visibleOptions"></slot> <slot name="header" :value="modelValue" :options="visibleOptions"></slot>
<div v-if="filter" class="p-dropdown-header"> <div v-if="filter" class="p-dropdown-header" v-bind="ptm('header')">
<div class="p-dropdown-filter-container"> <div class="p-dropdown-filter-container" v-bind="ptm('filterContainer')">
<input <input
ref="filterInput" ref="filterInput"
type="text" type="text"
@ -80,22 +80,29 @@
@keydown="onFilterKeyDown" @keydown="onFilterKeyDown"
@blur="onFilterBlur" @blur="onFilterBlur"
@input="onFilterChange" @input="onFilterChange"
v-bind="filterInputProps" v-bind="{ ...filterInputProps, ...ptm('filterInput') }"
/> />
<slot name="filtericon"> <slot name="filtericon">
<component :is="filterIcon ? 'span' : 'FilterIcon'" :class="['p-dropdown-filter-icon', filterIcon]" /> <component :is="filterIcon ? 'span' : 'FilterIcon'" :class="['p-dropdown-filter-icon', filterIcon]" v-bind="ptm('filterIcon')" />
</slot> </slot>
</div> </div>
<span role="status" aria-live="polite" class="p-hidden-accessible"> <span role="status" aria-live="polite" class="p-hidden-accessible" v-bind="ptm('hiddenFilterResult')">
{{ filterResultMessageText }} {{ filterResultMessageText }}
</span> </span>
</div> </div>
<div class="p-dropdown-items-wrapper" :style="{ 'max-height': virtualScrollerDisabled ? scrollHeight : '' }"> <div class="p-dropdown-items-wrapper" :style="{ 'max-height': virtualScrollerDisabled ? scrollHeight : '' }" v-bind="ptm('wrapper')">
<VirtualScroller :ref="virtualScrollerRef" v-bind="virtualScrollerOptions" :items="visibleOptions" :style="{ height: scrollHeight }" :tabindex="-1" :disabled="virtualScrollerDisabled"> <VirtualScroller :ref="virtualScrollerRef" v-bind="{ ...virtualScrollerOptions, ...ptm('virtualScroller') }" :items="visibleOptions" :style="{ height: scrollHeight }" :tabindex="-1" :disabled="virtualScrollerDisabled">
<template v-slot:content="{ styleClass, contentRef, items, getItemOptions, contentStyle, itemSize }"> <template v-slot:content="{ styleClass, contentRef, items, getItemOptions, contentStyle, itemSize }">
<ul :ref="(el) => listRef(el, contentRef)" :id="id + '_list'" :class="['p-dropdown-items', styleClass]" :style="contentStyle" role="listbox"> <ul :ref="(el) => listRef(el, contentRef)" :id="id + '_list'" :class="['p-dropdown-items', styleClass]" :style="contentStyle" role="listbox" v-bind="ptm('list')">
<template v-for="(option, i) of items" :key="getOptionRenderKey(option, getOptionIndex(i, getItemOptions))"> <template v-for="(option, i) of items" :key="getOptionRenderKey(option, getOptionIndex(i, getItemOptions))">
<li v-if="isOptionGroup(option)" :id="id + '_' + getOptionIndex(i, getItemOptions)" :style="{ height: itemSize ? itemSize + 'px' : undefined }" class="p-dropdown-item-group" role="option"> <li
v-if="isOptionGroup(option)"
:id="id + '_' + getOptionIndex(i, getItemOptions)"
:style="{ height: itemSize ? itemSize + 'px' : undefined }"
class="p-dropdown-item-group"
role="option"
v-bind="ptm('itemGroup')"
>
<slot name="optiongroup" :option="option.optionGroup" :index="getOptionIndex(i, getItemOptions)">{{ getOptionGroupLabel(option.optionGroup) }}</slot> <slot name="optiongroup" :option="option.optionGroup" :index="getOptionIndex(i, getItemOptions)">{{ getOptionGroupLabel(option.optionGroup) }}</slot>
</li> </li>
<li <li
@ -112,14 +119,15 @@
:aria-posinset="getAriaPosInset(getOptionIndex(i, getItemOptions))" :aria-posinset="getAriaPosInset(getOptionIndex(i, getItemOptions))"
@click="onOptionSelect($event, option)" @click="onOptionSelect($event, option)"
@mousemove="onOptionMouseMove($event, getOptionIndex(i, getItemOptions))" @mousemove="onOptionMouseMove($event, getOptionIndex(i, getItemOptions))"
v-bind="getPTOptions(option, getItemOptions, i, 'item')"
> >
<slot name="option" :option="option" :index="getOptionIndex(i, getItemOptions)">{{ getOptionLabel(option) }}</slot> <slot name="option" :option="option" :index="getOptionIndex(i, getItemOptions)">{{ getOptionLabel(option) }}</slot>
</li> </li>
</template> </template>
<li v-if="filterValue && (!items || (items && items.length === 0))" class="p-dropdown-empty-message" role="option"> <li v-if="filterValue && (!items || (items && items.length === 0))" class="p-dropdown-empty-message" role="option" v-bind="ptm('emptyMessage')">
<slot name="emptyfilter">{{ emptyFilterMessageText }}</slot> <slot name="emptyfilter">{{ emptyFilterMessageText }}</slot>
</li> </li>
<li v-else-if="!options || (options && options.length === 0)" class="p-dropdown-empty-message" role="option"> <li v-else-if="!options || (options && options.length === 0)" class="p-dropdown-empty-message" role="option" v-bind="ptm('emptyMessage')">
<slot name="empty">{{ emptyMessageText }}</slot> <slot name="empty">{{ emptyMessageText }}</slot>
</li> </li>
</ul> </ul>
@ -130,13 +138,13 @@
</VirtualScroller> </VirtualScroller>
</div> </div>
<slot name="footer" :value="modelValue" :options="visibleOptions"></slot> <slot name="footer" :value="modelValue" :options="visibleOptions"></slot>
<span v-if="!options || (options && options.length === 0)" role="status" aria-live="polite" class="p-hidden-accessible"> <span v-if="!options || (options && options.length === 0)" role="status" aria-live="polite" class="p-hidden-accessible" v-bind="ptm('emptyMessage')">
{{ emptyMessageText }} {{ emptyMessageText }}
</span> </span>
<span role="status" aria-live="polite" class="p-hidden-accessible"> <span role="status" aria-live="polite" class="p-hidden-accessible" v-bind="ptm('hiddenSelectedMessage')">
{{ selectedMessageText }} {{ selectedMessageText }}
</span> </span>
<span ref="lastHiddenFocusableElementOnOverlay" role="presentation" aria-hidden="true" class="p-hidden-accessible p-hidden-focusable" :tabindex="0" @focus="onLastHiddenFocus"></span> <span ref="lastHiddenFocusableElementOnOverlay" role="presentation" aria-hidden="true" class="p-hidden-accessible p-hidden-focusable" :tabindex="0" @focus="onLastHiddenFocus" v-bind="ptm('hiddenLastFocusableEl')"></span>
</div> </div>
</transition> </transition>
</Portal> </Portal>
@ -145,6 +153,7 @@
<script> <script>
import { FilterService } from 'primevue/api'; import { FilterService } from 'primevue/api';
import BaseComponent from 'primevue/basecomponent';
import ChevronDownIcon from 'primevue/icons/chevrondown'; import ChevronDownIcon from 'primevue/icons/chevrondown';
import FilterIcon from 'primevue/icons/filter'; import FilterIcon from 'primevue/icons/filter';
import SpinnerIcon from 'primevue/icons/spinner'; import SpinnerIcon from 'primevue/icons/spinner';
@ -157,6 +166,7 @@ import VirtualScroller from 'primevue/virtualscroller';
export default { export default {
name: 'Dropdown', name: 'Dropdown',
extends: BaseComponent,
emits: ['update:modelValue', 'change', 'focus', 'blur', 'before-show', 'before-hide', 'show', 'hide', 'filter'], emits: ['update:modelValue', 'change', 'focus', 'blur', 'before-show', 'before-hide', 'show', 'hide', 'filter'],
props: { props: {
modelValue: null, modelValue: null,
@ -377,6 +387,15 @@ export default {
getOptionRenderKey(option, index) { getOptionRenderKey(option, index) {
return (this.dataKey ? ObjectUtils.resolveFieldData(option, this.dataKey) : this.getOptionLabel(option)) + '_' + index; return (this.dataKey ? ObjectUtils.resolveFieldData(option, this.dataKey) : this.getOptionLabel(option)) + '_' + index;
}, },
getPTOptions(option, itemOptions, index, key) {
return this.ptm(key, {
context: {
selected: this.isSelected(option),
focused: this.focusedOptionIndex === this.getOptionIndex(index, itemOptions),
disabled: this.isOptionDisabled(option)
}
});
},
isOptionDisabled(option) { isOptionDisabled(option) {
return this.optionDisabled ? ObjectUtils.resolveFieldData(option, this.optionDisabled) : false; return this.optionDisabled ? ObjectUtils.resolveFieldData(option, this.optionDisabled) : false;
}, },