# Column Editors

> Learn how to use configure editors for columns in Infinite Table

Canonical page: https://infinite-table.com/angular/docs/learn/editing/column-editors

For now, Infinite Table comes with a default built-in editor that's rendered when editing starts on any editable cell.

It's very easy to configure columns with your own custom editors via the [`columns.components.Editor`](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.components.Editor) property.

```tsx
const columns: InfiniteTablePropColumns<Developer> = {
  canDesign: {
    field: 'canDesign',
    defaultEditable: true,
    components: {
      // don't forget to provide an implementation
      // for the BoolEditor component
      Editor: BoolEditor,
    },
  },
  id: {
    field: 'id',
  },
};
```

For now, we're not shipping any extra editors with Infinite Table.

There are a few reasons for that:

- we want to keep our bundle size small
- we're aware people have their own preferences - especially **select/combo boxes** and **date pickers** are very complex components on their own and there are many different popular alternatives many teams already use in their projects

So in this page and other parts of the docs, we'll use some popular alternatives, to show how to integrate them with Infinite Table.

## Using Date Editors

A common use-case is integrating date editors. In Angular, a date editor is a [custom editor](https://infinite-table.com/angular/docs/learn/editing/custom-editor.md) like any other: a component that calls [`injectColumnEditor`](https://infinite-table.com/docs/reference/hooks/index.md#useInfiniteColumnEditor) and renders a date input. The following example uses the browser's native `<input type="date">`; the date picker you already use in your project fits in the same way.

Click any cell in the **Birth Date** column to edit it with the browser's date input: pick a day to confirm the edit, press Escape (or click elsewhere) to cancel it.

**Example: Using a native date input for editing dates in the DataGrid**

```ts
import {
  Component,
  ElementRef,
  computed,
  input,
  viewChild,
} from '@angular/core';
import type { AfterViewInit } from '@angular/core';

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

type Developer = {
  birthDate: Date;
  id: number;
  firstName: string;
  country: string;
  city: string;
  currency: string;
  email: string;
  preferredLanguage: string;
  hobby: string;
  salary: number;
};

const pad = (value: number, length = 2) => String(value).padStart(length, '0');

// YYYY-MM-DD - also the value format of <input type="date">
const formatDate = (date: Date) =>
  [
    pad(date.getFullYear(), 4),
    pad(date.getMonth() + 1),
    pad(date.getDate()),
  ].join('-');

const parseDate = (value: string) => {
  const [year, month, day] = value.split('-').map(Number);
  return new Date(year, month - 1, day);
};

@Component({
  selector: 'date-editor',
  host: { style: 'display: block; width: 100%; height: 100%' },
  template: `
    <input
      #input
      type="date"
      style="
        width: 100%;
        height: 100%;
        background: white;
        color: black;
        padding: 3px;
        box-sizing: border-box;
      "
      [value]="initialValue"
      (keydown)="onKeydown($event)"
      (pointerdown)="typing = false"
      (change)="onChange()"
      (blur)="cancel()"
    />
  `,
})
class DateEditor implements AfterViewInit {
  private readonly editor = injectColumnEditor();
  private readonly input =
    viewChild.required<ElementRef<HTMLInputElement>>('input');

  protected readonly initialValue = formatDate(this.editor.initialValue);

  // a day picked in the calendar confirms the edit right away,
  // a date typed with the keyboard waits for Enter
  protected typing = false;
  private done = false;

  ngAfterViewInit() {
    const input = this.input().nativeElement;
    input.focus();
    try {
      // open the calendar right away
      input.showPicker();
    } catch {
      // the browser doesn't allow it here
    }
  }

  protected onKeydown(event: KeyboardEvent) {
    if (event.key === 'Enter') {
      event.stopPropagation();
      this.confirm();
    } else if (event.key === 'Escape') {
      this.cancel();
    } else {
      this.typing = true;
      // keep the arrow keys in the date input, not in the grid
      event.stopPropagation();
    }
  }

  protected onChange() {
    if (!this.typing) {
      this.confirm();
    }
  }

  private confirm() {
    const { value } = this.input().nativeElement;
    if (!value) {
      // not a valid date
      this.cancel();
      return;
    }
    this.done = true;
    this.editor.confirmEdit(parseDate(value));
  }

  protected cancel() {
    if (!this.done) {
      this.done = true;
      this.editor.cancelEdit();
    }
  }
}

@Component({
  selector: 'date-value',
  template: `<b>{{ text() }}</b>`,
})
class DateValue {
  // set from the cell: the inputs named like the render params get their values
  readonly value = input.required<Date>();

  protected readonly text = computed(() => formatDate(this.value()));
}

const columns: InfiniteTablePropColumns<Developer> = {
  firstName: { field: 'firstName', header: 'First Name' },
  birthDate: {
    field: 'birthDate',
    header: 'Birth Date',
    // we need to specify the type of the column as "date"
    type: 'date',
    defaultEditable: true,
    defaultWidth: 200,
    components: {
      Editor: DateEditor,
    },
    style: ({ inEdit }) => {
      return inEdit ? { padding: 0 } : {};
    },
    renderValue: () => renderComponent(DateValue),
  },
  salary: {
    field: 'salary',
    header: 'Salary',
    type: 'number',
  },
  country: { field: 'country', header: 'Country' },
  preferredLanguage: { field: 'preferredLanguage' },
  id: { field: 'id' },
  hobby: { field: 'hobby' },
  city: { field: 'city' },
  currency: { field: 'currency' },
};

const dataSource: Developer[] = [
  {
    id: 0,
    firstName: 'Nya',
    country: 'India',
    city: 'Unnao',
    birthDate: new Date(1997, 0, 1),
    currency: 'JPY',
    preferredLanguage: 'TypeScript',
    salary: 60000,
    hobby: 'sports',
    email: 'Nya44@gmail.com',
  },
  {
    id: 1,
    firstName: 'Axel',
    country: 'Mexico',
    city: 'Cuitlahuac',
    birthDate: new Date(1993, 3, 10),
    currency: 'USD',
    preferredLanguage: 'TypeScript',
    salary: 100000,
    hobby: 'sports',
    email: 'Axel93@hotmail.com',
  },
  {
    id: 2,
    firstName: 'Gonzalo',
    country: 'United Arab Emirates',
    city: 'Fujairah',
    birthDate: new Date(1997, 10, 30),
    currency: 'JPY',
    preferredLanguage: 'Go',
    salary: 120000,
    hobby: 'photography',
    email: 'Gonzalo_McGlynn34@gmail.com',
  },
  {
    id: 3,
    firstName: 'Sherwood',
    country: 'Mexico',
    city: 'Tlacolula de Matamoros',
    birthDate: new Date(1990, 5, 20),
    currency: 'CHF',
    preferredLanguage: 'Rust',
    salary: 99000,
    hobby: 'cooking',
    email: 'Sherwood_McLaughlin65@hotmail.com',
  },
  {
    id: 4,
    firstName: 'Alexandre',
    country: 'France',
    city: 'Persan',
    birthDate: new Date(1990, 3, 20),
    currency: 'EUR',
    preferredLanguage: 'Go',
    salary: 97000,
    hobby: 'reading',
    email: 'Alexandre_Harber@hotmail.com',
  },
  {
    id: 5,
    firstName: 'Mariane',
    country: 'United States',
    city: 'Hays',
    birthDate: new Date(2002, 3, 20),
    currency: 'EUR',
    preferredLanguage: 'TypeScript',

    salary: 58000,
    hobby: 'cooking',
    email: 'Mariane0@hotmail.com',
  },
  {
    id: 6,
    firstName: 'Rosalind',
    country: 'Mexico',
    city: 'Nuevo Casas Grandes',
    birthDate: new Date(1992, 11, 12),
    currency: 'AUD',
    preferredLanguage: 'JavaScript',
    salary: 198000,
    hobby: 'dancing',
    email: 'Rosalind69@gmail.com',
  },
  {
    id: 7,
    firstName: 'Lolita',
    country: 'Sweden',
    city: 'Delsbo',
    birthDate: new Date(1990, 9, 5),
    currency: 'JPY',
    preferredLanguage: 'TypeScript',
    salary: 200000,
    hobby: 'cooking',
    email: 'Lolita.Hayes@hotmail.com',
  },
  {
    id: 8,
    firstName: 'Tre',
    country: 'Germany',
    city: 'Bad Camberg',
    birthDate: new Date(1990, 9, 15),
    currency: 'GBP',
    preferredLanguage: 'TypeScript',
    salary: 200000,
    hobby: 'sports',
    email: 'Tre28@gmail.com',
  },
  {
    id: 9,
    firstName: 'Lurline',
    country: 'Canada',
    city: 'Raymore',
    birthDate: new Date(1990, 4, 18),
    currency: 'EUR',
    preferredLanguage: 'Rust',
    salary: 58000,
    hobby: 'sports',
    email: 'Lurline_Deckow@gmail.com',
  },
];

@Component({
  selector: 'demo-root',
  imports: [DataSource, InfiniteTable],
  template: `
    <infinite-data-source primaryKey="id" [data]="dataSource">
      <infinite-table
        debugId="date-editor-example"
        [columns]="columns"
        [columnDefaultWidth]="120"
      />
    </infinite-data-source>
  `,
})
export default class App {
  protected readonly dataSource = dataSource;
  protected readonly columns = columns;
}
```

## Configure Editors for Column Types

When you have more than one column that needs to use the same editor, you can use the [column types](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columnTypes) and associate the editor with the column type.

After defining your generic column types, make sure you assign them to the columns that need that specific type

This is a basic example integrating with the [MUI X Date Picker](https://mui.com/x/react-date-pickers/date-picker/) - click any cell in the **Birth Date** or **Date Hired** columns to show the date picker.

This example uses the [column types](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columnTypes) to give each date column the same editor and styling.

**Example: Using MUI X Date Picker with custom 'date' type columns**

```ts
import { Component, ElementRef, TemplateRef, viewChild } from '@angular/core';
import type { AfterViewInit } from '@angular/core';

import {
  DataSource,
  InfiniteTable,
  injectColumnEditor,
} from '@infinite-table/infinite-angular';
import type {
  InfiniteTablePropColumnTypes,
  InfiniteTablePropColumns,
} from '@infinite-table/infinite-angular';

type Developer = {
  birthDate: Date;
  dateHired: Date;
  id: number;
  firstName: string;
  country: string;
  city: string;
  currency: string;
  email: string;
  preferredLanguage: string;
  hobby: string;
  salary: number;
};

const pad = (n: number) => String(n).padStart(2, '0');

// YYYY-MM-DD, the format of a native date input
function formatDate(date: Date | null | undefined) {
  if (!date) {
    return '';
  }
  return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(
    date.getDate(),
  )}`;
}

function parseDate(value: string) {
  if (!value) {
    return null;
  }
  const [year, month, day] = value.split('-').map(Number);
  return new Date(year, month - 1, day);
}

// a native date input - picking a date confirms the edit,
// Escape or leaving the editor cancels it
@Component({
  selector: 'date-editor',
  template: `
    <input
      #input
      type="date"
      style="width: 100%; height: 100%; box-sizing: border-box; background: white; color: black; padding: 3px; border: none"
      [value]="initialValue"
      (change)="onChange()"
      (keydown)="onKeydown($event)"
      (blur)="cancel()"
    />
  `,
})
class DateEditor implements AfterViewInit {
  protected readonly editor = injectColumnEditor();
  protected readonly initialValue = formatDate(this.editor.initialValue);

  private readonly input =
    viewChild.required<ElementRef<HTMLInputElement>>('input');

  // true once the user types in the input: then Enter confirms the date
  private typing = false;
  private done = false;

  ngAfterViewInit() {
    const input = this.input().nativeElement;
    input.focus();
    try {
      // open the calendar, as the editor opens
      input.showPicker();
    } catch {
      // no picker (no user activation, or not supported)
    }
  }

  protected onChange() {
    if (!this.typing) {
      // a date picked from the calendar
      this.confirm();
    }
  }

  protected onKeydown(event: KeyboardEvent) {
    const { key } = event;
    if (key === 'Enter') {
      this.confirm();
    } else if (key === 'Escape') {
      this.cancel();
    } else {
      this.typing = true;
    }
    // the keys are for the date input, not for the grid
    event.stopPropagation();
  }

  private confirm() {
    const date = parseDate(this.input().nativeElement.value);
    if (!date) {
      this.cancel();
      return;
    }
    if (!this.done) {
      this.done = true;
      this.editor.confirmEdit(date);
    }
  }

  protected cancel() {
    if (!this.done) {
      this.done = true;
      this.editor.cancelEdit();
    }
  }
}

const columns: InfiniteTablePropColumns<Developer> = {
  firstName: { field: 'firstName', header: 'First Name' },
  birthDate: {
    field: 'birthDate',
    header: 'Birth Date',
    // we need to specify the type of the column as "date"
    type: 'date',
  },
  dateHired: {
    field: 'dateHired',
    header: 'Date Hired',
    // we need to specify the type of the column as "date"
    type: 'date',
  },
  salary: {
    field: 'salary',
    header: 'Salary',
    type: 'number',
  },
  country: { field: 'country', header: 'Country' },
  preferredLanguage: { field: 'preferredLanguage' },
  id: { field: 'id' },
  hobby: { field: 'hobby' },
  city: { field: 'city' },
  currency: { field: 'currency' },
};

@Component({
  selector: 'demo-root',
  imports: [DataSource, InfiniteTable],
  template: `
    <infinite-data-source primaryKey="id" [data]="dataSource">
      <infinite-table
        debugId="column-types-date-editor-example"
        [columnTypes]="columnTypes"
        [columns]="columns"
        [columnDefaultWidth]="120"
      />
    </infinite-data-source>

    <ng-template #dateValue let-value="value">
      <b>{{ formatDate(value) }}</b>
    </ng-template>
  `,
})
export default class App {
  protected readonly dataSource = dataSource;
  protected readonly columns = columns;
  protected readonly formatDate = formatDate;

  private readonly dateValueTemplate =
    viewChild.required<TemplateRef<unknown>>('dateValue');

  protected readonly columnTypes: InfiniteTablePropColumnTypes<Developer> = {
    date: {
      defaultEditable: true,
      components: {
        Editor: DateEditor,
      },
      defaultWidth: 200,
      style: ({ inEdit }) => {
        return inEdit ? { padding: 0 } : {};
      },
      renderValue: () => this.dateValueTemplate(),
    },
  };
}

const dataSource: Developer[] = [
  {
    id: 0,
    firstName: 'Nya',
    country: 'India',
    city: 'Unnao',
    birthDate: new Date(1997, 0, 1),
    dateHired: new Date(2023, 0, 1),
    currency: 'JPY',
    preferredLanguage: 'TypeScript',
    salary: 60000,
    hobby: 'sports',
    email: 'Nya44@gmail.com',
  },
  {
    id: 1,
    firstName: 'Axel',
    country: 'Mexico',
    city: 'Cuitlahuac',
    birthDate: new Date(1993, 3, 10),
    dateHired: new Date(2022, 5, 10),
    currency: 'USD',
    preferredLanguage: 'TypeScript',
    salary: 100000,
    hobby: 'sports',
    email: 'Axel93@hotmail.com',
  },
  {
    id: 2,
    firstName: 'Gonzalo',
    country: 'United Arab Emirates',
    city: 'Fujairah',
    birthDate: new Date(1997, 10, 30),
    dateHired: new Date(2021, 8, 29),
    currency: 'JPY',
    preferredLanguage: 'Go',
    salary: 120000,
    hobby: 'photography',
    email: 'Gonzalo_McGlynn34@gmail.com',
  },
  {
    id: 3,
    firstName: 'Sherwood',
    country: 'Mexico',
    city: 'Tlacolula de Matamoros',
    birthDate: new Date(1990, 5, 20),
    dateHired: new Date(2021, 8, 20),
    currency: 'CHF',
    preferredLanguage: 'Rust',
    salary: 99000,
    hobby: 'cooking',
    email: 'Sherwood_McLaughlin65@hotmail.com',
  },
  {
    id: 4,
    firstName: 'Alexandre',
    country: 'France',
    city: 'Persan',
    birthDate: new Date(1990, 3, 20),
    dateHired: new Date(2023, 11, 12),
    currency: 'EUR',
    preferredLanguage: 'Go',
    salary: 97000,
    hobby: 'reading',
    email: 'Alexandre_Harber@hotmail.com',
  },
  {
    id: 5,
    firstName: 'Mariane',
    country: 'United States',
    city: 'Hays',
    birthDate: new Date(2002, 3, 20),
    dateHired: new Date(2022, 2, 22),
    currency: 'EUR',
    preferredLanguage: 'TypeScript',

    salary: 58000,
    hobby: 'cooking',
    email: 'Mariane0@hotmail.com',
  },
  {
    id: 6,
    firstName: 'Rosalind',
    country: 'Mexico',
    city: 'Nuevo Casas Grandes',
    birthDate: new Date(1992, 11, 12),
    dateHired: new Date(2022, 1, 12),
    currency: 'AUD',
    preferredLanguage: 'JavaScript',
    salary: 198000,
    hobby: 'dancing',
    email: 'Rosalind69@gmail.com',
  },
  {
    id: 7,
    firstName: 'Lolita',
    country: 'Sweden',
    city: 'Delsbo',
    birthDate: new Date(1990, 9, 5),
    dateHired: new Date(2022, 1, 5),
    currency: 'JPY',
    preferredLanguage: 'TypeScript',
    salary: 200000,
    hobby: 'cooking',
    email: 'Lolita.Hayes@hotmail.com',
  },
  {
    id: 8,
    firstName: 'Tre',
    country: 'Germany',
    city: 'Bad Camberg',
    birthDate: new Date(1990, 9, 15),
    dateHired: new Date(2022, 10, 1),
    currency: 'GBP',
    preferredLanguage: 'TypeScript',
    salary: 200000,
    hobby: 'sports',
    email: 'Tre28@gmail.com',
  },
  {
    id: 9,
    firstName: 'Lurline',
    country: 'Canada',
    city: 'Raymore',
    birthDate: new Date(1990, 4, 18),
    dateHired: new Date(2023, 3, 18),
    currency: 'EUR',
    preferredLanguage: 'Rust',
    salary: 58000,
    hobby: 'sports',
    email: 'Lurline_Deckow@gmail.com',
  },
];
```
