@scalar/api-reference
Version:
Generate beautiful API references from OpenAPI documents
103 lines (102 loc) • 4.65 kB
JavaScript
import { defineComponent, computed, ref, createElementBlock, createCommentVNode, openBlock, createElementVNode, Fragment, renderList, createBlock, createVNode, unref, withCtx, createTextVNode, normalizeClass, toDisplayString } from "vue";
import { ScalarButton } from "@scalar/components";
import { ScalarIconPlus } from "@scalar/icons";
import { getResolvedRef } from "@scalar/workspace-store/helpers/get-resolved-ref";
import { isArraySchema } from "@scalar/workspace-store/schemas/v3.1/strict/type-guards";
import SchemaEnumPropertyItem from "./SchemaEnumPropertyItem.vue.js";
const _hoisted_1 = {
key: 0,
class: "property-enum"
};
const _hoisted_2 = { class: "property-enum-values" };
const _hoisted_3 = { key: 1 };
const ENUM_DISPLAY_THRESHOLD = 9;
const INITIAL_VISIBLE_COUNT = 5;
const THIN_SPACE = " ";
const _sfc_main = /* @__PURE__ */ defineComponent({
__name: "SchemaEnumValues",
props: {
value: {}
},
setup(__props) {
const enumValues = computed(() => {
if (!__props.value) {
return [];
}
return __props.value.enum || isArraySchema(__props.value) && getResolvedRef(__props.value.items)?.enum || [];
});
const shouldUseLongListDisplay = computed(
() => enumValues.value.length > ENUM_DISPLAY_THRESHOLD
);
const initialVisibleCount = computed(
() => shouldUseLongListDisplay.value ? INITIAL_VISIBLE_COUNT : enumValues.value.length
);
const visibleEnumValues = computed(
() => enumValues.value.slice(0, initialVisibleCount.value)
);
const hiddenEnumValues = computed(
() => enumValues.value.slice(initialVisibleCount.value)
);
const getEnumValueDescription = (enumValue, index) => {
const descriptions = __props.value?.["x-enumDescriptions"] ?? __props.value?.["x-enum-descriptions"];
if (!descriptions) {
return void 0;
}
if (Array.isArray(descriptions)) {
return descriptions[index];
}
if (typeof descriptions === "object" && descriptions !== null) {
return descriptions[String(enumValue)];
}
return void 0;
};
const formatEnumValueWithName = (enumValue, index) => {
const varNames = __props.value?.["x-enum-varnames"] ?? __props.value?.["x-enumNames"];
const varName = Array.isArray(varNames) ? varNames[index] : void 0;
return varName ? `${enumValue}${THIN_SPACE}=${THIN_SPACE}${varName}` : String(enumValue);
};
const isExpanded = ref(false);
const toggleExpanded = () => {
isExpanded.value = !isExpanded.value;
};
return (_ctx, _cache) => {
return enumValues.value.length > 0 ? (openBlock(), createElementBlock("div", _hoisted_1, [
createElementVNode("ul", _hoisted_2, [
(openBlock(true), createElementBlock(Fragment, null, renderList(visibleEnumValues.value, (enumValue, index) => {
return openBlock(), createBlock(SchemaEnumPropertyItem, {
key: String(enumValue),
description: getEnumValueDescription(enumValue, index),
label: formatEnumValueWithName(enumValue, index)
}, null, 8, ["description", "label"]);
}), 128)),
shouldUseLongListDisplay.value && isExpanded.value ? (openBlock(true), createElementBlock(Fragment, { key: 0 }, renderList(hiddenEnumValues.value, (enumValue, index) => {
return openBlock(), createBlock(SchemaEnumPropertyItem, {
key: String(enumValue),
description: getEnumValueDescription(enumValue, initialVisibleCount.value + index),
label: formatEnumValueWithName(enumValue, initialVisibleCount.value + index)
}, null, 8, ["description", "label"]);
}), 128)) : createCommentVNode("", true),
shouldUseLongListDisplay.value ? (openBlock(), createElementBlock("li", _hoisted_3, [
createVNode(unref(ScalarButton), {
class: "enum-toggle-button my-2 flex h-fit gap-1 rounded-full border py-1.5 pr-2.5 pl-2 leading-none",
variant: "ghost",
onClick: toggleExpanded
}, {
default: withCtx(() => [
createVNode(unref(ScalarIconPlus), {
class: normalizeClass({ "rotate-45": isExpanded.value }),
weight: "bold"
}, null, 8, ["class"]),
createTextVNode(" " + toDisplayString(isExpanded.value ? "Hide values" : "Show all values"), 1)
]),
_: 1
})
])) : createCommentVNode("", true)
])
])) : createCommentVNode("", true);
};
}
});
export {
_sfc_main as default
};