@dialpad/dialtone
Version:
Dialpad's Dialtone design system monorepo
219 lines (218 loc) • 6.13 kB
JavaScript
import { LIST_ITEM_TYPES, LIST_ITEM_NAVIGATION_TYPES } from "./list_item_constants.js";
import utils from "../../common/utils.js";
import { DtIconCheck } from "@dialpad/dialtone-icons/vue3";
import { resolveComponent, openBlock, createBlock, resolveDynamicComponent, mergeProps, toHandlers, withCtx, createSlots, renderList, renderSlot, createVNode } from "vue";
import _export_sfc from "../../_virtual/_plugin-vue_export-helper.js";
import DtItemLayout from "../item_layout/item_layout.vue.js";
const ROLES = ["listitem", "menuitem", "option"];
const _sfc_main = {
compatConfig: { MODE: 3 },
name: "DtListItem",
components: {
DtItemLayout,
DtIconCheck
},
/**
* Value provided from keyboard_list_navigation.js using id prop.
*/
inject: {
highlightId: { default: null }
},
props: {
/**
* Id for the item.
*/
id: {
type: String,
default() {
return utils.getUniqueString();
}
},
/**
* String to use for the item's role.
*/
role: {
type: String,
default: "listitem",
validator: (role) => ROLES.includes(role)
},
/**
* HTML element type (tag name) of the content wrapper element.
*/
elementType: {
type: String,
default: "li"
},
/**
* The type of child list item to use.
* @values default, custom
*/
type: {
type: String,
default: LIST_ITEM_TYPES.DEFAULT,
validator: (t) => Object.values(LIST_ITEM_TYPES).includes(t)
},
/**
* The type of navigation that this component should support.
* - "arrow-keys" for items that are navigated with UP/DOWN keys.
* - "tab" for items that are navigated using the TAB key.
* - "none" for static items that are not interactive.
* @values arrow-keys, tab, none
*/
navigationType: {
type: String,
default: LIST_ITEM_NAVIGATION_TYPES.NONE,
validator: (t) => Object.values(LIST_ITEM_NAVIGATION_TYPES).includes(t)
},
/**
* Applies selected styles to the list item
*/
selected: {
type: Boolean,
default: false
}
},
emits: [
/**
* Key down event
*
* @event keydown
* @type {KeyboardEvent}
*/
"keydown",
/**
* Native mouse move event
*
* @event mousemove
* @type {MouseEvent}
*/
"mousemove",
/**
* Native mouse leave event
*
* @event mouseleave
* @type {MouseEvent}
*/
"mouseleave",
/**
* Mouse down event
*
* @event mousedown
*/
"mousedown"
],
data() {
return {
injected: false,
mouseHighlighted: false
};
},
computed: {
isDefaultType() {
return this.type === LIST_ITEM_TYPES.DEFAULT;
},
listItemListeners() {
return {
keydown: (event) => {
if (["enter", "space"].includes(event.code.toLowerCase())) {
this.onClick(event);
}
this.$emit("keydown", event);
},
mousemove: (event) => {
this.onMouseHover(event);
this.$emit("mousemove", event);
},
mouseleave: (event) => {
this.onMouseLeave(event);
this.$emit("mouseleave", event);
}
};
},
/**
* For keyboard navigation, whether this item is currently highlighted.
* An injected highlightId will override the default mouseover highlight.
*/
isHighlighted() {
if (this.isHoverable) {
return this.highlightId && this.highlightId() ? this.id === this.highlightId() : this.mouseHighlighted;
}
return false;
},
isFocusable() {
return this.navigationType === LIST_ITEM_NAVIGATION_TYPES.TAB;
},
/**
* Whether to apply hover styles.
*/
isHoverable() {
return this.navigationType !== LIST_ITEM_NAVIGATION_TYPES.NONE;
}
},
methods: {
onClick(e) {
this.$emit("click", e);
},
onMouseHover() {
this.mouseHighlighted = true;
},
onMouseLeave() {
this.mouseHighlighted = false;
}
}
};
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
const _component_dt_icon_check = resolveComponent("dt-icon-check");
const _component_dt_item_layout = resolveComponent("dt-item-layout");
return openBlock(), createBlock(resolveDynamicComponent($props.elementType), mergeProps({
id: $props.id,
class: [
"d-list-item",
{
"d-list-item--focusable": $options.isFocusable,
"d-list-item--highlighted": $options.isHighlighted,
"d-list-item--static": !$options.isHoverable
}
],
tabindex: $options.isFocusable ? 0 : -1,
role: $props.role,
"aria-selected": $props.role === "listitem" ? void 0 : $options.isHighlighted
}, toHandlers($options.listItemListeners)), {
default: withCtx(() => [
$options.isDefaultType ? (openBlock(), createBlock(_component_dt_item_layout, {
key: 0,
unstyled: "",
class: "d-list-item__wrapper",
"left-class": "d-list-item__left",
"content-class": "d-list-item__content",
"title-class": "d-list-item__title",
"subtitle-class": "d-list-item__subtitle",
"bottom-class": "d-list-item__bottom",
"right-class": "d-list-item__right",
"selected-class": "d-list-item__selected"
}, createSlots({ _: 2 }, [
renderList(_ctx.$slots, (_, slotName) => {
return {
name: slotName,
fn: withCtx(() => [
renderSlot(_ctx.$slots, slotName)
])
};
}),
$props.selected ? {
name: "selected",
fn: withCtx(() => [
createVNode(_component_dt_icon_check, { size: "400" })
]),
key: "0"
} : void 0
]), 1024)) : renderSlot(_ctx.$slots, "default", { key: 1 })
]),
_: 3
}, 16, ["id", "class", "tabindex", "role", "aria-selected"]);
}
const DtListItem = /* @__PURE__ */ _export_sfc(_sfc_main, [["render", _sfc_render]]);
export {
DtListItem as default
};
//# sourceMappingURL=list_item.vue.js.map