Refactor #3922 - For TreeSelect

pull/3938/head
Tuğçe Küçükoğlu 2023-05-07 22:51:06 +03:00
parent 6382a616fa
commit c70b5703cd
4 changed files with 117 additions and 13 deletions

View File

@ -101,6 +101,12 @@ const TreeSelectProps = [
type: 'string', type: 'string',
default: 'null', default: 'null',
description: 'Used to define a string that labels the element.' description: 'Used to define a string that labels the element.'
},
{
name: 'pt',
type: 'any',
default: 'null',
description: 'Uses to pass attributes to DOM elements inside the component.'
} }
]; ];

View File

@ -64,6 +64,7 @@ import { TieredMenuPassThroughOptions } from '../tieredmenu';
import { ToastPassThroughOptions } from '../toast'; import { ToastPassThroughOptions } from '../toast';
import { ToolbarPassThroughOptions } from '../toolbar'; import { ToolbarPassThroughOptions } from '../toolbar';
import { TreePassThroughOptions } from '../tree'; import { TreePassThroughOptions } from '../tree';
import { TreeSelectPassThroughOptions } from '../treeselect';
import { VirtualScrollerPassThroughOptions } from '../virtualscroller'; import { VirtualScrollerPassThroughOptions } from '../virtualscroller';
interface PrimeVueConfiguration { interface PrimeVueConfiguration {
@ -150,6 +151,7 @@ interface PrimeVuePTOptions {
toast?: ToastPassThroughOptions; toast?: ToastPassThroughOptions;
toolbar?: ToolbarPassThroughOptions; toolbar?: ToolbarPassThroughOptions;
tree?: TreePassThroughOptions; tree?: TreePassThroughOptions;
treeselect?: TreeSelectPassThroughOptions;
virtualscroller?: VirtualScrollerPassThroughOptions; virtualscroller?: VirtualScrollerPassThroughOptions;
} }

View File

@ -8,9 +8,97 @@
* *
*/ */
import { InputHTMLAttributes, VNode } from 'vue'; import { InputHTMLAttributes, VNode } from 'vue';
import { TreeNode } from '../tree'; import { TreeNode, TreePassThroughOptionType } from '../tree';
import { ClassComponent, GlobalComponentConstructor } from '../ts-helpers'; import { ClassComponent, GlobalComponentConstructor } from '../ts-helpers';
export declare type TreeSelectPassThroughOptionType = TreeSelectPassThroughAttributes | ((options: TreeSelectPassThroughMethodOptions) => TreeSelectPassThroughAttributes) | null | undefined;
/**
* Custom passthrough(pt) option method.
*/
export interface TreeSelectPassThroughMethodOptions {
props: TreeSelectProps;
state: TreeSelectState;
}
/**
* Custom passthrough(pt) options.
* @see {@link TreeSelectProps.pt}
*/
export interface TreeSelectPassThroughOptions {
/**
* Uses to pass attributes to the root's DOM element.
*/
root?: TreeSelectPassThroughOptionType;
/**
* Uses to pass attributes to the label container's DOM element.
*/
labelContainer?: TreeSelectPassThroughOptionType;
/**
* Uses to pass attributes to the label's DOM element.
*/
label?: TreeSelectPassThroughOptionType;
/**
* Uses to pass attributes to the token's DOM element.
*/
token?: TreeSelectPassThroughOptionType;
/**
* Uses to pass attributes to the token label's DOM element.
*/
tokenLabel?: TreeSelectPassThroughOptionType;
/**
* Uses to pass attributes to the trigger's DOM element.
*/
trigger?: TreeSelectPassThroughOptionType;
/**
* Uses to pass attributes to the trigger icon's DOM element.
*/
triggerIcon?: TreeSelectPassThroughOptionType;
/**
* Uses to pass attributes to the panel's DOM element.
*/
panel?: TreeSelectPassThroughOptionType;
/**
* Uses to pass attributes to the wrapper's DOM element.
*/
wrapper?: TreeSelectPassThroughOptionType;
/**
* Uses to pass attributes to the trigger's DOM element.
* @see {@link TreePassThroughOptionType}
*/
tree?: TreePassThroughOptionType;
/**
* Uses to pass attributes to the empty message's DOM element.
*/
emptyMessage?: TreeSelectPassThroughOptionType;
/**
* Uses to pass attributes to the hidden input wrapper's DOM element.
*/
hiddenInputWrapper?: TreeSelectPassThroughOptionType;
/**
* Uses to pass attributes to the hidden input's DOM element.
*/
hiddenInput?: TreeSelectPassThroughOptionType;
}
/**
* Custom passthrough attributes for each DOM elements
*/
export interface TreeSelectPassThroughAttributes {
[key: string]: any;
}
/**
* Defines current inline state in TreeSelect component.
*/
export interface TreeSelectState {
/**
* Current collapsed state as a boolean.
* @defaultValue false
*/
d_collapsed: boolean;
}
/** /**
* Defines valid properties in TreeSelect component. * Defines valid properties in TreeSelect component.
*/ */
@ -94,6 +182,11 @@ export interface TreeSelectProps {
* Establishes a string value that labels the component. * Establishes a string value that labels the component.
*/ */
'aria-label'?: string | undefined; 'aria-label'?: string | undefined;
/**
* Uses to pass attributes to DOM elements inside the component.
* @type {TreeSelectPassThroughOptions}
*/
pt?: TreeSelectPassThroughOptions;
} }
/** /**

View File

@ -1,6 +1,6 @@
<template> <template>
<div ref="container" :class="containerClass" @click="onClick"> <div ref="container" :class="containerClass" @click="onClick" v-bind="ptm('root')">
<div class="p-hidden-accessible"> <div class="p-hidden-accessible" v-bind="ptm('hiddenInputWrapper')">
<input <input
ref="focusInput" ref="focusInput"
:id="inputId" :id="inputId"
@ -19,34 +19,34 @@
@focus="onFocus($event)" @focus="onFocus($event)"
@blur="onBlur($event)" @blur="onBlur($event)"
@keydown="onKeyDown($event)" @keydown="onKeyDown($event)"
v-bind="inputProps" v-bind="{ ...inputProps, ...ptm('hiddenInput') }"
/> />
</div> </div>
<div class="p-treeselect-label-container"> <div class="p-treeselect-label-container" v-bind="ptm('labelContainer')">
<div :class="labelClass"> <div :class="labelClass" v-bind="ptm('label')">
<slot name="value" :value="selectedNodes" :placeholder="placeholder"> <slot name="value" :value="selectedNodes" :placeholder="placeholder">
<template v-if="display === 'comma'"> <template v-if="display === 'comma'">
{{ label || 'empty' }} {{ label || 'empty' }}
</template> </template>
<template v-else-if="display === 'chip'"> <template v-else-if="display === 'chip'">
<div v-for="node of selectedNodes" :key="node.key" class="p-treeselect-token"> <div v-for="node of selectedNodes" :key="node.key" class="p-treeselect-token" v-bind="ptm('token')">
<span class="p-treeselect-token-label">{{ node.label }}</span> <span class="p-treeselect-token-label" v-bind="ptm('tokenLabel')">{{ node.label }}</span>
</div> </div>
<template v-if="emptyValue">{{ placeholder || 'empty' }}</template> <template v-if="emptyValue">{{ placeholder || 'empty' }}</template>
</template> </template>
</slot> </slot>
</div> </div>
</div> </div>
<div class="p-treeselect-trigger" role="button" aria-haspopup="tree" :aria-expanded="overlayVisible"> <div class="p-treeselect-trigger" role="button" aria-haspopup="tree" :aria-expanded="overlayVisible" v-bind="ptm('trigger')">
<slot name="triggericon"> <slot name="triggericon">
<component :is="'ChevronDownIcon'" class="p-treeselect-trigger-icon" /> <component :is="'ChevronDownIcon'" class="p-treeselect-trigger-icon" v-bind="ptm('triggerIcon')" />
</slot> </slot>
</div> </div>
<Portal :appendTo="appendTo"> <Portal :appendTo="appendTo">
<transition name="p-connected-overlay" @enter="onOverlayEnter" @leave="onOverlayLeave" @after-leave="onOverlayAfterLeave"> <transition name="p-connected-overlay" @enter="onOverlayEnter" @leave="onOverlayLeave" @after-leave="onOverlayAfterLeave">
<div v-if="overlayVisible" :ref="overlayRef" @click="onOverlayClick" :class="panelStyleClass" @keydown="onOverlayKeydown" v-bind="panelProps"> <div v-if="overlayVisible" :ref="overlayRef" @click="onOverlayClick" :class="panelStyleClass" @keydown="onOverlayKeydown" v-bind="{ ...panelProps, ...ptm('panel') }">
<slot name="header" :value="modelValue" :options="options"></slot> <slot name="header" :value="modelValue" :options="options"></slot>
<div class="p-treeselect-items-wrapper" :style="{ 'max-height': scrollHeight }"> <div class="p-treeselect-items-wrapper" :style="{ 'max-height': scrollHeight }" v-bind="ptm('wrapper')">
<TSTree <TSTree
ref="tree" ref="tree"
:id="listId" :id="listId"
@ -62,6 +62,7 @@
@node-select="onNodeSelect" @node-select="onNodeSelect"
@node-unselect="onNodeUnselect" @node-unselect="onNodeUnselect"
:level="0" :level="0"
:pt="ptm('tree')"
> >
<template v-if="$slots.itemtogglericon" #togglericon="iconProps"> <template v-if="$slots.itemtogglericon" #togglericon="iconProps">
<slot name="itemtogglericon" :node="iconProps.node" :expanded="iconProps.expanded" :class="iconProps.class" /> <slot name="itemtogglericon" :node="iconProps.node" :expanded="iconProps.expanded" :class="iconProps.class" />
@ -70,7 +71,7 @@
<slot name="itemcheckboxicon" :checked="iconProps.checked" :partialChecked="iconProps.partialChecked" :class="iconProps.class" /> <slot name="itemcheckboxicon" :checked="iconProps.checked" :partialChecked="iconProps.partialChecked" :class="iconProps.class" />
</template> </template>
</TSTree> </TSTree>
<div v-if="emptyOptions" class="p-treeselect-empty-message"> <div v-if="emptyOptions" class="p-treeselect-empty-message" v-bind="ptm('emptyMessage')">
<slot name="empty">{{ emptyMessageText }}</slot> <slot name="empty">{{ emptyMessageText }}</slot>
</div> </div>
</div> </div>
@ -82,6 +83,7 @@
</template> </template>
<script> <script>
import BaseComponent from 'primevue/basecomponent';
import ChevronDownIcon from 'primevue/icons/chevrondown'; import ChevronDownIcon from 'primevue/icons/chevrondown';
import OverlayEventBus from 'primevue/overlayeventbus'; import OverlayEventBus from 'primevue/overlayeventbus';
import Portal from 'primevue/portal'; import Portal from 'primevue/portal';
@ -91,6 +93,7 @@ import { ConnectedOverlayScrollHandler, DomHandler, UniqueComponentId, ZIndexUti
export default { export default {
name: 'TreeSelect', name: 'TreeSelect',
extends: BaseComponent,
emits: ['update:modelValue', 'before-show', 'before-hide', 'change', 'show', 'hide', 'node-select', 'node-unselect', 'node-expand', 'node-collapse', 'focus', 'blur'], emits: ['update:modelValue', 'before-show', 'before-hide', 'change', 'show', 'hide', 'node-select', 'node-unselect', 'node-expand', 'node-collapse', 'focus', 'blur'],
props: { props: {
modelValue: null, modelValue: null,