# Providing a Custom Filter Editor

> Writing a custom filter editor for a column in Infinite Table is straightforward.

Canonical page: https://infinite-table.com/angular/docs/learn/filtering/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 writing a component that uses the [`injectColumnFilterEditor`](https://infinite-table.com/docs/reference/hooks/index.md#useInfiniteColumnFilterEditor) inject function.

The next snippet shows our implementation of the `number` filter editor:

```ts
@Component({
  // the host element is the input
  selector: 'input[numberFilterEditor]',
  host: {
    type: 'number',
    '[attr.aria-label]': 'editor.ariaLabel()',
    '[disabled]': '!!editor.disabled()',
    '[value]': 'value()',
    '[class]': 'editor.className',
    '(input)': 'onInput($event)',
  },
  template: '',
})
export class NumberFilterEditor {
  protected readonly editor = injectColumnFilterEditor();

  protected readonly value = computed(() => this.editor.value() ?? '');

  protected onInput(event: Event) {
    const target = event.target as HTMLInputElement;
    const value = isNaN(target.valueAsNumber)
      ? target.value
      : target.valueAsNumber;
    this.editor.setValue(value);
  }
}
```

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:

```ts
import {
  NumberFilterEditor,
  StringFilterEditor,
} from '@infinite-table/infinite-angular';
```

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:

```ts
// <infinite-data-source [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`:

```ts
// <infinite-data-source [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`).

```ts
// <infinite-data-source [filterTypes]="filterTypes" />
const filterTypes: DataSourcePropFilterTypes<Developer> = {
  bool: {
    defaultOperator: 'eq',
    emptyValues: [null],
    components: {
      FilterEditor: () => renderComponent(BoolFilterEditor),
      FilterOperatorSwitch: () => null,
    },
    operators: [
      {
        name: 'eq',
        label: 'Equals',
        fn: ({ currentValue, filterValue }) => currentValue === filterValue,
      },
    ],
  },
};
```

`components.FilterEditor` is a function that returns what to render - for a component, `renderComponent(BoolFilterEditor)`. The component is created with the injector of the filter cell, so it can use [`injectColumnFilterEditor`](https://infinite-table.com/docs/reference/hooks/index.md#useInfiniteColumnFilterEditor).

Now it's time to write the actual `BoolFilterEditor` that the `bool` filter type is using:

```ts
import { Component } from '@angular/core';
import {
  InfiniteCheckBox,
  injectColumnFilterEditor,
} from '@infinite-table/infinite-angular';

@Component({
  selector: 'div[boolFilterEditor]',
  imports: [InfiniteCheckBox],
  host: {
    '[class]': 'editor.className',
    style: 'text-align: center',
  },
  template: `
    <input
      infiniteCheckBox
      [checked]="editor.value()"
      (checkedChange)="onChange($event)"
    />
  `,
})
export class BoolFilterEditor {
  protected readonly editor = injectColumnFilterEditor<Developer>();

  protected onChange(newValue: boolean | null) {
    const value = this.editor.value();
    if (value === true) {
      // after the value was true, make it go to indeterminate state
      newValue = null;
    }
    if (value === null) {
      // from indeterminate, goto false
      newValue = false;
    }
    this.editor.setValue(newValue);
  }
}
```

In the snippet above, note how we're using the [`injectColumnFilterEditor`](https://infinite-table.com/docs/reference/hooks/index.md#useInfiniteColumnFilterEditor) inject function 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 signal: read it in the template (`editor.value()`), so the editor is updated when the filter changes.

**Example: 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.

```ts
import { Component } from '@angular/core';

import {
  DataSource,
  InfiniteCheckBox,
  InfiniteTable,
  injectColumnFilterEditor,
  renderComponent,
} from '@infinite-table/infinite-angular';
import type {
  DataSourcePropFilterTypes,
  InfiniteTablePropColumns,
} from '@infinite-table/infinite-angular';

type Developer = {
  id: number;
  firstName: string;
  canDesign: boolean;
  stack: string;
  hobby: string;
};
const dataSource: Developer[] = [
  {
    id: 1,
    firstName: 'John',

    canDesign: true,
    stack: 'frontend',
    hobby: 'gaming',
  },
  {
    id: 2,
    firstName: 'Jane',

    canDesign: false,
    stack: 'backend',
    hobby: 'reading',
  },
  {
    id: 3,
    firstName: 'Jack',

    canDesign: true,
    stack: 'frontend',
    hobby: 'gaming',
  },
  {
    id: 4,
    firstName: 'Jill',

    canDesign: false,
    stack: 'backend',
    hobby: 'reading',
  },
  {
    id: 5,
    firstName: 'Seb',

    canDesign: false,
    stack: 'backend',
    hobby: 'reading',
  },
];

const columns: InfiniteTablePropColumns<Developer> = {
  id: {
    field: 'id',
    type: 'number',
    defaultWidth: 100,
  },
  canDesign: {
    field: 'canDesign',
    filterType: 'bool',
    renderValue: ({ value }) => (value ? 'Yes' : 'No'),
  },
  firstName: {
    field: 'firstName',
  },
  stack: { field: 'stack' },
};

const domProps = {
  style: {
    height: '100%',
  },
};

@Component({
  selector: 'div[boolFilterEditor]',
  imports: [InfiniteCheckBox],
  host: {
    '[class]': 'editor.className',
    style: 'text-align: center',
  },
  template: `
    <input
      infiniteCheckBox
      [checked]="editor.value()"
      (checkedChange)="onChange($event)"
    />
  `,
})
class BoolFilterEditor {
  protected readonly editor = injectColumnFilterEditor<Developer>();

  protected onChange(newValue: boolean | null) {
    const value = this.editor.value();
    if (value === true) {
      // after the value was true, make it go to indeterminate state
      newValue = null;
    }
    if (value === null) {
      // from indeterminate, goto false
      newValue = false;
    }
    this.editor.setValue(newValue);
  }
}

const filterTypes: DataSourcePropFilterTypes<Developer> = {
  bool: {
    defaultOperator: 'eq',
    emptyValues: [null],
    components: {
      FilterEditor: () => renderComponent(BoolFilterEditor),
      FilterOperatorSwitch: () => null,
    },
    operators: [
      {
        name: 'eq',
        label: 'Equals',
        fn: ({ currentValue, filterValue }) => currentValue === filterValue,
      },
    ],
  },
};

@Component({
  selector: 'demo-root',
  imports: [DataSource, InfiniteTable],
  template: `
    <infinite-data-source
      [data]="dataSource"
      primaryKey="id"
      [defaultFilterValue]="[]"
      [filterDelay]="0"
      [filterTypes]="filterTypes"
    >
      <infinite-table
        debugId="checkbox-filter-editor-example"
        [domProps]="domProps"
        [columnDefaultWidth]="150"
        [columnMinWidth]="50"
        [columns]="columns"
      />
    </infinite-data-source>
  `,
})
export default class App {
  protected readonly dataSource = dataSource;
  protected readonly filterTypes = filterTypes;
  protected readonly domProps = domProps;
  protected readonly columns = columns;
}
```
