Beautiful Open-Source Form Builder
Update VSelect component to integrate vue-virtual for improved perfor… (#884)
Chirag Chhatralab359737d61b2
diff --git a/client/components/forms/core/components/VSelect.vue b/client/components/forms/core/components/VSelect.vueindex 5601beb33..7bda86010 100644--- a/client/components/forms/core/components/VSelect.vue+++ b/client/components/forms/core/components/VSelect.vue@@ -100,6 +100,7 @@<ultabindex="-1"role="listbox"+ ref="scrollRef":class="variantSlots.dropdown()"><div@@ -143,28 +144,44 @@v-if="filteredOptions.length > 0":class="variantSlots.optionsContainer()">- <li- v-for="item in filteredOptions"- :key="item[optionKey]"- role="option"- :style="optionStyle"- :class="[- variantSlots.option(),- dropdownClass,- { 'pr-9': multiple},- {- 'opacity-50 cursor-not-allowed': disabledOptionsMap[item[optionKey]],- 'cursor-pointer hover:bg-gray-100 dark:hover:bg-gray-900': !disabledOptionsMap[item[optionKey]]- }- ]"- @click.stop="select(item)"+ <div+ v-if="virtualizer"+ :style="{ height: virtualizer.getTotalSize() + 'px', width: '100%', position: 'relative' }">- <slot- name="option"- :option="item"- :selected="isSelected(item)"- />- </li>+ <li+ v-for="virtualItem in virtualizer.getVirtualItems()"+ :key="filteredOptions[virtualItem.index] ? filteredOptions[virtualItem.index][optionKey] : virtualItem.index"+ role="option"+ :style="[+ optionStyle,+ {+ position: 'absolute',+ top: '0px',+ left: '0px',+ width: '100%',+ height: virtualItem.size + 'px',+ transform: `translateY(${virtualItem.start}px)`+ }+ ]"+ :class="[+ variantSlots.option(),+ dropdownClass,+ { 'pr-9': multiple},+ {+ 'opacity-50 cursor-not-allowed': filteredOptions[virtualItem.index] && disabledOptionsMap[filteredOptions[virtualItem.index][optionKey]],+ 'cursor-pointer hover:bg-gray-100 dark:hover:bg-gray-900': filteredOptions[virtualItem.index] && !disabledOptionsMap[filteredOptions[virtualItem.index][optionKey]]+ }+ ]"+ @click.stop="filteredOptions[virtualItem.index] && select(filteredOptions[virtualItem.index])"+ >+ <slot+ v-if="filteredOptions[virtualItem.index]"+ name="option"+ :option="filteredOptions[virtualItem.index]"+ :selected="isSelected(filteredOptions[virtualItem.index])"+ />+ </li>+ </div></div><slotv-else-if="!loading && !(allowCreation && searchTerm)"@@ -207,6 +224,7 @@ import debounce from 'debounce'import Fuse from 'fuse.js'import { tv } from "tailwind-variants"import { vSelectTheme } from "~/lib/forms/themes/v-select.theme.js"+import { useVirtualizer } from '@tanstack/vue-virtual'export default {name: 'VSelect',@@ -231,7 +249,6 @@ export default {placeholder: { type: String, default: null },uppercaseLabels: { type: Boolean, default: true },showClearButton: { type: Boolean, default: true },- // Theme configuration as strings for tailwind-variantstheme: {type: String, default: null},size: {type: String, default: null},borderRadius: {type: String, default: null},@@ -240,6 +257,9 @@ export default {disabled: { type: Boolean, default: false },minSelection: { type: Number, default: null },maxSelection: { type: Number, default: null },+ fuseOptions: { type: Object, default: () => ({}) },+ searchDebounceMs: { type: Number, default: 150 },+ minSearchLength: { type: Number, default: 1 },popoverWidth: { type: String, default: null }},emits: ['update:modelValue', 'update-options', 'focus', 'blur'],@@ -247,8 +267,13 @@ export default {return {isOpen: false,searchTerm: '',+ debouncedTerm: '',defaultValue: this.modelValue ?? null,- isFocused: false+ isFocused: false,+ virtualizer: null,+ fuse: null,+ fuseIndex: null,+ updateDebouncedTerm: null}},computed: {@@ -305,18 +330,24 @@ export default {},filteredOptions () {if (!this.data) return []- if (!this.searchable || this.remote || this.searchTerm === '') {++ // If not searchable, remote search is used, or term too short/empty, return raw data+ if (!this.searchable || this.remote) {return this.data}- // Fuse search- const fuzeOptions = {- keys: this.searchKeys+ const term = this.debouncedTerm+ if (!term || term.length < this.minSearchLength) {+ return this.data}- const fuse = new Fuse(this.data, fuzeOptions)- return fuse.search(this.searchTerm).map((res) => {- return res.item- })++ // Ensure Fuse is ready+ if (!this.fuse) {+ this.buildFuse()+ }+ if (!this.fuse) return this.data++ return this.fuse.search(term).map((res) => res.item)},isSearchable () {return this.searchable || this.remote !== null || this.allowCreation@@ -345,13 +376,99 @@ export default {},watch: {searchTerm (val) {- if (!this.debouncedRemote) return- if ((this.remote && val) || (val === '' && !this.modelValue) || (val === '' && this.isOpen)) {- return this.debouncedRemote(val)+ // Remote search path+ if (this.debouncedRemote) {+ if ((this.remote && val) || (val === '' && !this.modelValue) || (val === '' && this.isOpen)) {+ this.debouncedRemote(val)+ }+ } else {+ // Local search path: debounce updates+ if (this.updateDebouncedTerm) this.updateDebouncedTerm(val)+ }+ },+ data () {+ // Only (re)build fuse when using local search+ if (this.searchable && !this.remote) {+ this.buildFuse()+ } else {+ this.fuse = null+ this.fuseIndex = null}+ },+ isOpen (val) {+ if (val) {+ this.$nextTick(() => {+ this.setupVirtualizer()+ })+ }+ },+ filteredOptions () {+ if (this.isOpen) {+ this.$nextTick(() => {+ this.setupVirtualizer()+ })+ }+ }+ },+ mounted () {+ // Initialize fuse for local search and debounce handler+ this.buildFuse()+ this.updateDebouncedTerm = debounce((val) => {+ this.debouncedTerm = val+ }, this.searchDebounceMs)+ },+ beforeUnmount () {+ // Clean up virtualizer if it exists+ if (this.virtualizer) {+ this.virtualizer = null+ }+ if (this.updateDebouncedTerm && this.updateDebouncedTerm.clear) {+ this.updateDebouncedTerm.clear()}},methods: {+ buildFuse () {+ if (!this.data || !Array.isArray(this.data) || this.data.length === 0) {+ this.fuse = null+ this.fuseIndex = null+ return+ }++ const options = Object.assign({+ keys: this.searchKeys,+ threshold: 0.3,+ ignoreLocation: true,+ includeScore: false+ }, this.fuseOptions || {})++ try {+ const index = Fuse.createIndex(options.keys, this.data)+ this.fuseIndex = index+ this.fuse = new Fuse(this.data, options, index)+ } catch {+ // Fallback without precomputed index+ this.fuse = new Fuse(this.data, options)+ this.fuseIndex = null+ }+ },+ setupVirtualizer () {+ if (!this.$refs.scrollRef || !this.filteredOptions || this.filteredOptions.length === 0) {+ this.virtualizer = null+ return+ }++ // Clean up existing virtualizer+ if (this.virtualizer) {+ this.virtualizer = null+ }++ this.virtualizer = useVirtualizer({+ count: this.filteredOptions.length,+ getScrollElement: () => this.$refs.scrollRef,+ estimateSize: () => 40,+ overscan: 5+ })+ },isSelected (value) {if (!this.modelValue) return false@@ -445,5 +562,4 @@ export default {}}}-</script>-+</script>\ No newline at end of filediff --git a/client/components/open/editors/GoogleFontPicker.vue b/client/components/open/editors/GoogleFontPicker.vueindex 14f87967c..d77f2b055 100644--- a/client/components/open/editors/GoogleFontPicker.vue+++ b/client/components/open/editors/GoogleFontPicker.vue@@ -54,8 +54,8 @@<script setup>import { defineEmits } from "vue"-import Fuse from "fuse.js"import { refDebounced, useElementVisibility } from "@vueuse/core"+import { useFuse } from '@vueuse/integrations/useFuse'import FontCard from './FontCard.vue'import { contentApi } from "~/api"@@ -87,6 +87,18 @@ const fonts = ref([])const selectedFont = ref(props.font || null)const search = ref("")const debouncedSearch = refDebounced(search, 500)+const { results: fuseResults } = useFuse(+ debouncedSearch,+ computed(() => Object.values(fonts.value || [])),+ {+ fuseOptions: {+ threshold: 0.3,+ ignoreLocation: true,+ includeScore: false,+ },+ matchAllWhenSearchEmpty: true,+ }+)const scrollContainer = ref(null)const fontRefs = new Map()const visible = ref([])@@ -129,14 +141,8 @@ watch(() => props.show, fetchFonts)const enrichedFonts = computed(() => {- if (search.value === "" || search.value === null) {- return fonts.value- }-- // Fuze search- const fuse = new Fuse(Object.values(fonts.value))- return fuse.search(debouncedSearch.value).map((res) => {- return res.item- })+ return fuseResults.value && fuseResults.value.length > 0+ ? fuseResults.value.map((res) => res.item)+ : (fonts.value || [])})</script>diff --git a/client/components/pages/templates/TemplatesList.vue b/client/components/pages/templates/TemplatesList.vueindex 917b60e2b..2ab858877 100644--- a/client/components/pages/templates/TemplatesList.vue+++ b/client/components/pages/templates/TemplatesList.vue@@ -176,132 +176,111 @@</div></template>-<script>-import { computed } from "vue"-import Fuse from "fuse.js"+<script setup>+import { useFuse } from '@vueuse/integrations/useFuse'import SingleTemplate from "./SingleTemplate.vue"import { refDebounced } from "@vueuse/core"import { useTemplateMeta } from "~/composables/data/useTemplateMeta"-export default {- name: "TemplatesList",- components: { SingleTemplate },- props: {- templates: {- type: Array,- required: true,- },- loading: {- type: Boolean,- default: false,- },- showTypes: {- type: Boolean,- default: true,- },- filterTypes: {- type: Boolean,- default: true,- },- showIndustries: {- type: Boolean,- default: true,- },- filterIndustries: {- type: Boolean,- default: true,- },- gridClasses: {- type: String,- default: "grid-cols-1 mt-8 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4",- },+const props = defineProps({+ templates: {+ type: Array,+ required: true,},-- setup() {- const { industries, types } = useTemplateMeta()- const search = ref("")- const debouncedSearch = refDebounced(search, 500)- return {- search,- debouncedSearch,- user: computed(() => useAuth().user().data.value),- industries: computed(() => [...industries.values()]),- types: computed(() => [...types.values()]),- }+ loading: {+ type: Boolean,+ default: false,+ },+ showTypes: {+ type: Boolean,+ default: true,+ },+ filterTypes: {+ type: Boolean,+ default: true,+ },+ showIndustries: {+ type: Boolean,+ default: true,},+ filterIndustries: {+ type: Boolean,+ default: true,+ },+ gridClasses: {+ type: String,+ default: "grid-cols-1 mt-8 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4",+ },+})- data: () => ({- selectedType: "all",- selectedIndustry: "all",- }),+const { industries: industriesMap, types: typesMap } = useTemplateMeta()- computed: {- industriesOptions() {- return [{ name: "All Industries", value: "all" }].concat(- Object.values(this.industries).map((industry) => {- return {- name: industry.name,- value: industry.slug,- }- }),- )- },- typesOptions() {- return [{ name: "All Types", value: "all" }].concat(- Object.values(this.types).map((type) => {- return {- name: type.name,- value: type.slug,- }- }),- )- },- enrichedTemplates() {- let enrichedTemplates = this.templates+const industries = computed(() => [...industriesMap.value.values()])+const types = computed(() => [...typesMap.value.values()])++const search = ref("")+const debouncedSearch = refDebounced(search, 500)++const selectedType = ref("all")+const selectedIndustry = ref("all")- // Filter by Selected Type- if (- this.filterTypes &&- this.selectedType &&- this.selectedType !== "all"- ) {- enrichedTemplates = enrichedTemplates.filter((item) => {- return item.types && item.types.length > 0- ? item.types.includes(this.selectedType)- : false- })- }+const industriesOptions = computed(() => {+ return [{ name: "All Industries", value: "all" }].concat(+ industries.value.map((industry) => ({+ name: industry.name,+ value: industry.slug,+ })),+ )+})- // Filter by Selected Industry- if (- this.filterIndustries &&- this.selectedIndustry &&- this.selectedIndustry !== "all"- ) {- enrichedTemplates = enrichedTemplates.filter((item) => {- return item.industries && item.industries.length > 0- ? item.industries.includes(this.selectedIndustry)- : false- })- }+const typesOptions = computed(() => {+ return [{ name: "All Types", value: "all" }].concat(+ types.value.map((type) => ({+ name: type.name,+ value: type.slug,+ })),+ )+})- if (- !this.debouncedSearch ||- this.debouncedSearch === "" ||- this.debouncedSearch === null- ) {- return enrichedTemplates- }+const filteredBase = computed(() => {+ let list = props.templates- // Fuze search- const fuzeOptions = {- keys: ["name", "slug", "description", "short_description"],- }- const fuse = new Fuse(enrichedTemplates, fuzeOptions)- return fuse.search(this.debouncedSearch).map((res) => {- return res.item- })+ if (props.filterTypes && selectedType.value && selectedType.value !== "all") {+ list = list.filter((item) => {+ return item.types && item.types.length > 0+ ? item.types.includes(selectedType.value)+ : false+ })+ }++ if (props.filterIndustries && selectedIndustry.value && selectedIndustry.value !== "all") {+ list = list.filter((item) => {+ return item.industries && item.industries.length > 0+ ? item.industries.includes(selectedIndustry.value)+ : false+ })+ }++ return list+})++const { results: fuseResults } = useFuse(+ debouncedSearch,+ filteredBase,+ {+ fuseOptions: {+ keys: ["name", "slug", "description", "short_description"],+ threshold: 0.3,+ ignoreLocation: true,+ includeScore: false,},- },-}+ matchAllWhenSearchEmpty: true,+ }+)++const enrichedTemplates = computed(() => {+ const base = filteredBase.value+ const results = fuseResults.value+ return results && results.length > 0 ? results.map(r => r.item) : base+})</script>diff --git a/client/package-lock.json b/client/package-lock.jsonindex fbf7f620f..f54def8a7 100644--- a/client/package-lock.json+++ b/client/package-lock.json@@ -20,6 +20,7 @@"@tanstack/query-devtools": "^5.81.2","@tanstack/vue-query": "^5.81.5","@tanstack/vue-query-devtools": "^5.81.5",+ "@tanstack/vue-virtual": "^3.13.12","@vueuse/components": "^11.2.0","@vueuse/core": "^13.1.0","@vueuse/integrations": "^11.2.0",@@ -8202,7 +8203,6 @@"version": "3.13.12","resolved": "https://registry.npmjs.org/@tanstack/vue-virtual/-/vue-virtual-3.13.12.tgz","integrity": "sha512-vhF7kEU9EXWXh+HdAwKJ2m3xaOnTTmgcdXcF2pim8g4GvI7eRrk2YRuV5nUlZnd/NbCIX4/Ja2OZu5EjJL06Ww==",- "license": "MIT","dependencies": {"@tanstack/virtual-core": "3.13.12"},diff --git a/client/package.json b/client/package.jsonindex a47ec6b84..643337cdf 100644--- a/client/package.json+++ b/client/package.json@@ -49,6 +49,7 @@"@tanstack/query-devtools": "^5.81.2","@tanstack/vue-query": "^5.81.5","@tanstack/vue-query-devtools": "^5.81.5",+ "@tanstack/vue-virtual": "^3.13.12","@vueuse/components": "^11.2.0","@vueuse/core": "^13.1.0","@vueuse/integrations": "^11.2.0",@@ -72,7 +73,6 @@"pinia": "^3.0.2","prismjs": "^1.29.0","qrcode": "^1.5.4",-"quill": "^2.0.2","tailwind-merge": "^2.5.4","tailwindcss": "^4.1.10",diff --git a/client/pages/home.vue b/client/pages/home.vueindex ca971c3c1..624cb5945 100644--- a/client/pages/home.vue+++ b/client/pages/home.vue@@ -127,7 +127,7 @@</template><script setup>-import Fuse from "fuse.js"+import { useFuse } from '@vueuse/integrations/useFuse'import FormCard from '~/components/pages/home/FormCard.vue'import FormCardSkeleton from '~/components/pages/home/FormCardSkeleton.vue'import TrackClick from '~/components/global/TrackClick.vue'@@ -198,27 +198,37 @@ const allTags = computed(() => {const tagOptions = computed(() => allTags.value.map(tag => ({ label: tag, value: tag })))-const enrichedForms = computed(() => {+const baseForms = computed(() => {if (!forms.value) return []-- const enriched = forms.value.filter((form) => {- if (selectedTags.value.length === 0) {- return true- }- return form.tags && form.tags.length ? selectedTags.value.every(r => form.tags.includes(r.value)) : false+ return forms.value.filter((form) => {+ if (selectedTags.value.length === 0) return true+ const selectedTagStrings = selectedTags.value+ .map(t => typeof t === 'string' ? t : t?.value)+ .filter(Boolean)+ return form.tags && form.tags.length+ ? selectedTagStrings.every(tag => form.tags.includes(tag))+ : false})+})- if (!isFilteringForms.value || search.value === "" || search.value === null) {- return enriched+const { results: fuseResults } = useFuse(+ debouncedSearch,+ baseForms,+ {+ fuseOptions: {+ keys: ["title", "slug", "tags"],+ threshold: 0.3,+ ignoreLocation: true,+ includeScore: false,+ },+ matchAllWhenSearchEmpty: true,}+)- // Fuze search- const fuzeOptions = {- keys: ["title", "slug", "tags"],- }- const fuse = new Fuse(enriched, fuzeOptions)- return fuse.search(debouncedSearch.value).map((res) => {- return res.item- })+const enrichedForms = computed(() => {+ const base = baseForms.value+ if (!base || base.length === 0) return []+ const results = fuseResults.value+ return results && results.length > 0 ? results.map(r => r.item) : base})</script>