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',
default: 'null',
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 { DividerPassThroughOptions } from '../divider';
import { DockPassThroughOptions } from '../dock';
import { DropdownPassThroughOptions } from '../dropdown';
import { EditorPassThroughOptions } from '../editor';
import { FieldsetPassThroughOptions } from '../fieldset';
import { FileUploadPassThroughOptions } from '../fileupload';
@ -108,6 +109,7 @@ interface PrimeVuePTOptions {
divider?: DividerPassThroughOptions;
dialog?: DialogPassThroughOptions;
dock?: DockPassThroughOptions;
dropdown?: DropdownPassThroughOptions;
dynamicdialog?: DialogPassThroughOptions;
editor?: EditorPassThroughOptions;
fieldset?: FieldsetPassThroughOptions;

View File

@ -9,7 +9,18 @@
*/
import { HTMLAttributes, InputHTMLAttributes, VNode } from 'vue';
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.
@ -41,6 +52,151 @@ export interface DropdownFilterEvent {
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.
*/
@ -251,6 +407,11 @@ export interface DropdownProps {
* Identifier of the underlying input element.
*/
'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>
<div ref="container" :id="id" :class="containerClass" @click="onContainerClick">
<div ref="container" :id="id" :class="containerClass" @click="onContainerClick" v-bind="ptm('root')">
<input
v-if="editable"
ref="focusInput"
@ -23,7 +23,7 @@
@blur="onBlur"
@keydown="onKeyDown"
@input="onEditableInput"
v-bind="inputProps"
v-bind="{ ...inputProps, ...ptm('input') }"
/>
<span
v-else
@ -43,29 +43,29 @@
@focus="onFocus"
@blur="onBlur"
@keydown="onKeyDown"
v-bind="inputProps"
v-bind="{ ...inputProps, ...ptm('input') }"
>
<slot name="value" :value="modelValue" :placeholder="placeholder">{{ label === 'p-emptylabel' ? '&nbsp;' : label || 'empty' }}</slot>
</span>
<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>
<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">
<span v-if="loadingIcon" :class="['p-dropdown-trigger-icon pi-spin', loadingIcon]" aria-hidden="true" />
<SpinnerIcon v-else class="p-dropdown-trigger-icon" spin 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" v-bind="ptm('loadingIcon')" />
</slot>
<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>
</div>
<Portal :appendTo="appendTo">
<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">
<span ref="firstHiddenFocusableElementOnOverlay" role="presentation" aria-hidden="true" class="p-hidden-accessible p-hidden-focusable" :tabindex="0" @focus="onFirstHiddenFocus"></span>
<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" v-bind="ptm('hiddenFirstFocusableEl')"></span>
<slot name="header" :value="modelValue" :options="visibleOptions"></slot>
<div v-if="filter" class="p-dropdown-header">
<div class="p-dropdown-filter-container">
<div v-if="filter" class="p-dropdown-header" v-bind="ptm('header')">
<div class="p-dropdown-filter-container" v-bind="ptm('filterContainer')">
<input
ref="filterInput"
type="text"
@ -80,22 +80,29 @@
@keydown="onFilterKeyDown"
@blur="onFilterBlur"
@input="onFilterChange"
v-bind="filterInputProps"
v-bind="{ ...filterInputProps, ...ptm('filterInput') }"
/>
<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>
</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 }}
</span>
</div>
<div class="p-dropdown-items-wrapper" :style="{ 'max-height': virtualScrollerDisabled ? scrollHeight : '' }">
<VirtualScroller :ref="virtualScrollerRef" v-bind="virtualScrollerOptions" :items="visibleOptions" :style="{ height: scrollHeight }" :tabindex="-1" :disabled="virtualScrollerDisabled">
<div class="p-dropdown-items-wrapper" :style="{ 'max-height': virtualScrollerDisabled ? scrollHeight : '' }" v-bind="ptm('wrapper')">
<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 }">
<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))">
<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>
</li>
<li
@ -112,14 +119,15 @@
:aria-posinset="getAriaPosInset(getOptionIndex(i, getItemOptions))"
@click="onOptionSelect($event, option)"
@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>
</li>
</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>
</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>
</li>
</ul>
@ -130,13 +138,13 @@
</VirtualScroller>
</div>
<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 }}
</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 }}
</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>
</transition>
</Portal>
@ -145,6 +153,7 @@
<script>
import { FilterService } from 'primevue/api';
import BaseComponent from 'primevue/basecomponent';
import ChevronDownIcon from 'primevue/icons/chevrondown';
import FilterIcon from 'primevue/icons/filter';
import SpinnerIcon from 'primevue/icons/spinner';
@ -157,6 +166,7 @@ import VirtualScroller from 'primevue/virtualscroller';
export default {
name: 'Dropdown',
extends: BaseComponent,
emits: ['update:modelValue', 'change', 'focus', 'blur', 'before-show', 'before-hide', 'show', 'hide', 'filter'],
props: {
modelValue: null,
@ -377,6 +387,15 @@ export default {
getOptionRenderKey(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) {
return this.optionDisabled ? ObjectUtils.resolveFieldData(option, this.optionDisabled) : false;
},