Skip to content

Commit

Permalink
add missing components
Browse files Browse the repository at this point in the history
  • Loading branch information
radusuciu committed Jul 1, 2024
1 parent d5cc780 commit 02bd4e7
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
34 changes: 34 additions & 0 deletions examples/vue/filters/src/DebouncedInput.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<script lang="ts" setup>
import { computed, onBeforeUnmount, ref } from 'vue'
const props = defineProps({
modelValue: {
type: [String, Number],
required: true,
},
debounce: {
type: Number,
default: 500,
},
})
const emit = defineEmits(['update:modelValue'])
const timeout = ref<number>()
const localValue = computed({
get() {
return props.modelValue
},
set(newValue) {
if (timeout.value) {
clearTimeout(timeout.value)
}
timeout.value = setTimeout(
() => emit('update:modelValue', newValue),
props.debounce
)
},
})
onBeforeUnmount(() => clearTimeout(timeout.value))
</script>

<template>
<input v-model="localValue" v-bind="$attrs" />
</template>
82 changes: 82 additions & 0 deletions examples/vue/filters/src/Filter.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<script lang="ts" setup>
import type { Column, Table } from '@tanstack/vue-table'
import { computed, PropType } from 'vue'
import DebouncedInput from './DebouncedInput.vue'
const props = defineProps({
column: {
type: Object as PropType<Column<any, unknown>>,
required: true,
},
table: {
type: Object as PropType<Table<any>>,
required: true,
},
})
const firstValue = computed(() =>
props.table.getPreFilteredRowModel().flatRows[0]?.getValue(props.column.id)
)
const columnFilterValue = computed(() => props.column.getFilterValue())
const sortedUniqueValues = computed(() =>
typeof firstValue.value === 'number'
? []
: Array.from(props.column.getFacetedUniqueValues().keys()).sort()
)
</script>

<template>
<div v-if="typeof firstValue === 'number'">
<div class="flex space-x-2">
<DebouncedInput
type="number"
:min="Number(column.getFacetedMinMaxValues()?.[0] ?? '')"
:max="Number(column.getFacetedMinMaxValues()?.[1] ?? '')"
:modelValue="(columnFilterValue as [number, number])?.[0] ?? ''"
@update:modelValue="
value =>
column.setFilterValue((old: [number, number]) => [value, old?.[1]])
"
:placeholder="`Min ${
column.getFacetedMinMaxValues()?.[0]
? `(${column.getFacetedMinMaxValues()?.[0]})`
: ''
}`"
class="w-24 border shadow rounded"
/>
<DebouncedInput
type="number"
:min="Number(column.getFacetedMinMaxValues()?.[0] ?? '')"
:max="Number(column.getFacetedMinMaxValues()?.[1] ?? '')"
:modelValue="(columnFilterValue as [number, number])?.[1] ?? ''"
@update:modelValue="
value =>
column.setFilterValue((old: [number, number]) => [old?.[0], value])
"
:placeholder="`Max ${
column.getFacetedMinMaxValues()?.[1]
? `(${column.getFacetedMinMaxValues()?.[1]})`
: ''
}`"
class="w-24 border shadow rounded"
/>
</div>
<div class="h-1" />
</div>
<div v-else>
<datalist :id="column.id + 'list'">
<option
v-for="value in sortedUniqueValues.slice(0, 5000)"
:key="value"
:value="value"
/>
</datalist>
<DebouncedInput
type="text"
:modelValue="(columnFilterValue ?? '') as string"
@update:modelValue="value => column.setFilterValue(value)"
:placeholder="`Search... (${column.getFacetedUniqueValues().size})`"
class="w-36 border shadow rounded"
:list="column.id + 'list'"
/>
<div class="h-1" />
</div>
</template>

0 comments on commit 02bd4e7

Please sign in to comment.