Providing a Custom Filter Editor

Almost certainly, our current string and number filters are not enough for you. You will definitely need to write your custom filter editor.
Fortunately, doing this is straightforward - it involves using the useInfiniteColumnFilterEditor composable.
The next snippet shows our implementation of the number filter editor:
export const NumberFilterEditor = defineComponent({
  setup() {
    const { ariaLabel, value, setValue, className, disabled } =
      useInfiniteColumnFilterEditor();

    return () =>
      h('input', {
        'aria-label': ariaLabel.value,
        type: 'number',
        disabled: disabled.value,
        value: value.value ?? '',
        onInput: (event: Event) => {
          const target = event.target as HTMLInputElement;
          const value = isNaN(target.valueAsNumber)
            ? target.value
            : target.valueAsNumber;
          setValue(value);
        },
        class: className,
      });
  },
});
This NumberFilterEditor is configured in the components.FilterEditor property for the number filter type.
If you want to import the NumberFilterEditor, you can do so with the following code:
import {
  NumberFilterEditor,
  StringFilterEditor,
} from '@infinite-table/infinite-vue';
As an exercise, let's write a custom filter editor that shows a checkbox and uses that to filter the values.
First step is to define the bool filter type:
Defining the bool filter type with one emptyValue
// <DataSource :filterTypes="filterTypes" />
const filterTypes: DataSourcePropFilterTypes<Developer> = {
  bool: {
    label: 'Boolean',
    defaultOperator: 'eq',
    // when the filter checkbox is indeterminate state, that's mapped to `null`
    emptyValues: [null],
    operators: [
      // operators will come here
    ],
  },
};
Note in the code above, we have emptyValues: [null] - so when the filter checkbox is in indeterminate state, it should show all the rows.
Now it's time to define the operators - more exactly, just one operator, eq:
Defining the eq operator
// <DataSource :filterTypes="filterTypes" />
const filterTypes: DataSourcePropFilterTypes<Developer> = {
  bool: {
    defaultOperator: 'eq',
    emptyValues: [null],
    operators: [
      {
        name: 'eq',
        label: 'Equals',
        fn: ({ currentValue, filterValue }) => currentValue === filterValue,
      },
    ],
  },
};
The last part of the bool filter type will be to specify the FilterEditor component - this can be either specified as part of the filter type or as part of the operator definition (each operator can override the components.FilterEditor).
Specifying the FilterEditor component
// <DataSource :filterTypes="filterTypes" />
const filterTypes: DataSourcePropFilterTypes<Developer> = {
  bool: {
    defaultOperator: 'eq',
    emptyValues: [null],
    components: {
      FilterEditor: BoolFilterEditor as any,
      FilterOperatorSwitch: () => null,
    },
    operators: [
      {
        name: 'eq',
        label: 'Equals',
        fn: ({ currentValue, filterValue }) => currentValue === filterValue,
      },
    ],
  },
};
Now it's time to write the actual BoolFilterEditor that the bool filter type is using:
BoolFilterEditor
import { defineComponent, h } from 'vue';
import {
  components,
  useInfiniteColumnFilterEditor,
} from '@infinite-table/infinite-vue';

const { CheckBox } = components;

const BoolFilterEditor = defineComponent({
  setup() {
    const { value, setValue, className } =
      useInfiniteColumnFilterEditor<Developer>();

    return () =>
      h('div', { class: className, style: { textAlign: 'center' } }, [
        h(CheckBox, {
          checked: value.value,
          onChange: (newValue) => {
            if (value.value === true) {
              // after the value was true, make it go to indeterminate state
              newValue = null;
            }
            if (value.value === null) {
              // from indeterminate, goto false
              newValue = false;
            }
            setValue(newValue);
          },
        }),
      ]);
  },
});
In the snippet above, note how we're using the useInfiniteColumnFilterEditor composable to get the current value of the filter and also to retrieve the setValue function that we need to call when we want to update filtering.
value is a ref: read value.value inside the render function, so the editor is updated when the filter changes.
#
Writing a bool filter type with a custom filter editor
The canDesign column is using a custom bool filter type with a custom filter editor.
open
loading demo…