Refactor #3879 - For Chip

pull/3892/head
Tuğçe Küçükoğlu 2023-04-21 16:24:22 +03:00
parent c50efe4bbf
commit b228f30c52
3 changed files with 74 additions and 6 deletions

View File

@ -28,6 +28,12 @@ const ChipProps = [
type: 'string',
default: 'pi pi-times-circle',
description: 'Icon of the remove element.'
},
{
name: 'pt',
type: 'any',
default: 'null',
description: 'Uses to pass attributes to DOM elements inside the component.'
}
];

View File

@ -10,6 +10,61 @@
import { VNode } from 'vue';
import { ClassComponent, GlobalComponentConstructor } from '../ts-helpers';
export declare type ChipPassThroughOptionType = ChipPassThroughAttributes | ((options: ChipPassThroughMethodOptions) => ChipPassThroughAttributes) | null | undefined;
/**
* Custom passthrough(pt) option method.
*/
export interface ChipPassThroughMethodOptions {
props: ChipProps;
state: ChipState;
}
/**
* Custom passthrough(pt) options.
* @see {@link ChipProps.pt}
*/
export interface ChipPassThroughOptions {
/**
* Uses to pass attributes to the root's DOM element.
*/
root?: ChipPassThroughOptionType;
/**
* Uses to pass attributes to the image's DOM element.
*/
image?: ChipPassThroughOptionType;
/**
* Uses to pass attributes to the icon's DOM element.
*/
icon?: ChipPassThroughOptionType;
/**
* Uses to pass attributes to the label' DOM element.
*/
label?: ChipPassThroughOptionType;
/**
* Uses to pass attributes to the removeIcon's DOM element.
*/
removeIcon?: ChipPassThroughOptionType;
}
/**
* Custom passthrough attributes for each DOM elements
*/
export interface ChipPassThroughAttributes {
[key: string]: any;
}
/**
* Defines current inline state in Chip component.
*/
export interface ChipState {
/**
* Current visible state as a boolean.
* @defaultValue true
*/
visible: boolean;
}
/**
* Defines valid properties in Chip component.
*/
@ -37,6 +92,11 @@ export interface ChipProps {
* @deprecated since v3.27.0. Use 'removeicon' slot.
*/
removeIcon?: string;
/**
* Uses to pass attributes to DOM elements inside the component.
* @type {ChipPassThroughOptions}
*/
pt?: ChipPassThroughOptions;
}
/**

View File

@ -1,23 +1,25 @@
<template>
<div v-if="visible" :class="containerClass" :aria-label="label">
<div v-if="visible" :class="containerClass" :aria-label="label" v-bind="ptm('root')">
<slot></slot>
<template v-if="!$slots.default">
<img v-if="image" :src="image" />
<component v-else-if="$slots.icon" :is="$slots.icon" class="p-chip-icon" />
<span v-else-if="icon" :class="['p-chip-icon', icon]" />
<div v-if="label" class="p-chip-text">{{ label }}</div>
<img v-if="image" :src="image" v-bind="ptm('image')" />
<component v-else-if="$slots.icon" :is="$slots.icon" class="p-chip-icon" v-bind="ptm('icon')" />
<span v-else-if="icon" :class="['p-chip-icon', icon]" v-bind="ptm('icon')" />
<div v-if="label" class="p-chip-text" v-bind="ptm('label')">{{ label }}</div>
</template>
<slot v-if="removable" name="removeicon" :onClick="close" :onKeydown="onKeydown">
<component :is="removeIcon ? 'span' : 'TimesCircleIcon'" tabindex="0" :class="['p-chip-remove-icon', removeIcon]" @click="close" @keydown="onKeydown"></component>
<component :is="removeIcon ? 'span' : 'TimesCircleIcon'" tabindex="0" :class="['p-chip-remove-icon', removeIcon]" @click="close" @keydown="onKeydown" v-bind="ptm('removeIcon')"></component>
</slot>
</div>
</template>
<script>
import BaseComponent from 'primevue/basecomponent';
import TimesCircleIcon from 'primevue/icons/timescircle';
export default {
name: 'Chip',
extends: BaseComponent,
emits: ['remove'],
props: {
label: {