# FilterEditor

Card that edits one filter condition: the field name with its icon, the operators of the field (side by side when they are up to four, otherwise a select) and the input of the value, which depends on the operator (text, number with unit, range, date, date range, period, last N days, options from a list, a search or countries, yes/no, or the app's input). Every change calls `onchange`; with `confirm` it shows Cancel and Apply instead, and Enter applies. `Filters` shows it under a pill or after "Add filter"; it can also be used alone, for example in a page of settings.

```ts
import { FilterEditor } from '@likable-hair/svelte';
```

Category: composed. Source: `src/lib/components/composed/search/FilterEditor.svelte`.

## Examples

### Edit one condition

The card on its own: the operators of the field side by side (up to four), then the input of the value. Every change calls `onchange`; here the condition is shown below as JSON, the shape stored in the `Filters` tree.

```svelte
<script lang="ts">
	import { mdiCashMultiple } from '@mdi/js';
	import { FilterEditor, filterCondition, type FilterField } from '@likable-hair/svelte';

	const field: FilterField = {
		name: 'amount',
		label: 'Amount',
		icon: mdiCashMultiple,
		type: 'number',
		unit: '€',
		operators: ['gte', 'lte', 'eq', 'between']
	};

	let condition = $state(filterCondition('amount', 'gte', 50));
</script>

<div class="example">
	<div class="card">
		<FilterEditor {field} bind:condition />
	</div>
	<pre>{JSON.stringify({ operator: condition.operator, value: condition.value })}</pre>
</div>

<style>
	.example {
		display: grid;
		justify-items: start;
		gap: 12px;
	}

	.card {
		border: var(--global-border-width) solid var(--global-color-border-strong);
		border-radius: var(--global-radius-xl);
		background: var(--global-color-surface-pop);
		box-shadow: var(--global-shadow-lg);
	}

	pre {
		margin: 0;
		color: var(--global-color-text-2);
		font-size: 12px;
	}
</style>
```

### Cancel and Apply

With `confirm` changes stay in the card until Apply (or Enter); Apply is disabled while the value is incomplete. `removable` adds Remove for a condition that is already applied, `onclose` a close button.

```svelte
<script lang="ts">
	import { mdiContentCut } from '@mdi/js';
	import { FilterEditor, filterCondition, type FilterField, type FilterCondition } from '@likable-hair/svelte';

	const field: FilterField = {
		name: 'service',
		label: 'Service',
		icon: mdiContentCut,
		type: 'select',
		items: [
			{ value: 1, label: 'Cut' },
			{ value: 2, label: 'Color' },
			{ value: 3, label: 'Balayage' },
			{ value: 4, label: 'Blow-dry' }
		]
	};

	let condition = $state(filterCondition('service', 'in', [{ value: 3, label: 'Balayage' }]));
	let applied = $state<FilterCondition>();
	let message = $state('Nothing applied yet');

	function describe(item: FilterCondition) {
		const items = (item.value as { label: string }[] | undefined) ?? [];
		return `${item.operator === 'in' ? 'is' : 'is not'} ${items.map((entry) => entry.label).join(', ')}`;
	}
</script>

<div class="example">
	<div class="card">
		<FilterEditor
			{field}
			bind:condition
			confirm
			removable={!!applied}
			onapply={(next) => {
				applied = next;
				message = `Applied: ${field.label} ${describe(next)}`;
			}}
			oncancel={() => {
				condition = applied ?? filterCondition('service', 'in');
				message = 'Cancelled';
			}}
			onremove={() => {
				applied = undefined;
				condition = filterCondition('service', 'in');
				message = 'Removed';
			}}
			onclose={() => (message = 'Closed')}
		/>
	</div>
	<p>{message}</p>
</div>

<style>
	.example {
		display: grid;
		justify-items: start;
		gap: 12px;
	}

	.card {
		border: var(--global-border-width) solid var(--global-color-border-strong);
		border-radius: var(--global-radius-xl);
		background: var(--global-color-surface-pop);
		box-shadow: var(--global-shadow-lg);
	}

	p {
		margin: 0;
		color: var(--global-color-text-2);
		font-size: 13px;
	}
</style>
```

## Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `field` (required) | `FilterField` |  | The field of the condition. |
| `condition` (bindable) (required) | `FilterCondition` |  | The condition being edited. A new one is a condition with the field's default operator and no value. |
| `confirm` | `boolean` | `false` | Shows Cancel and Apply: changes reach `onapply` only when the user applies. Without it every change calls `onchange`. |
| `operatorSet` | `'basic' \| 'standard' \| 'all'` | `'standard'` | Operators offered when the field has no `operators`: `basic`, `standard` (default) or `all`. |
| `typeOperators` | `Partial<Record<'string' \| 'number' \| 'date' \| 'select' \| 'bool' \| 'custom', string[]>>` |  | Own operator list per field type, instead of `operatorSet`. |
| `removable` | `boolean` | `false` | Shows a Remove button (for a condition already in the filters). |
| `labels` | `FilterLabelsInput` |  | Texts to change (see `FilterLabels`), on top of those of `locale`. |
| `locale` | `string` |  | BCP 47 locale: the language of every text (`it` and `en` are built in, others fall back to English) and the format of dates and numbers. Defaults to the one set with `setDefaultLocale`, else `en`. |
| `autofocus` | `boolean` | `false` | Moves the focus to the value input when the card appears. |
| `class` | `string` |  | Extra classes on the card. |

## Snippets

| Name | Parameters | Description |
| --- | --- | --- |
| `inputSnippet` | `{ field: FilterField; operator: string; value: unknown; setValue: (value: unknown) => void }` | Input of the value for `custom` fields and for operators with `input: 'custom'`. Call `setValue` with the new value. |

## Events

| Name | Type | Description |
| --- | --- | --- |
| `onchange` | `(condition: FilterCondition) => void` | Called at every change of operator or value (without `confirm`). |
| `onapply` | `(condition: FilterCondition) => void` | Called by Apply and by Enter, with the edited condition (with `confirm`), or by Enter alone. |
| `oncancel` | `() => void` | Called by Cancel. |
| `onremove` | `() => void` | Called by Remove. |
| `onclose` | `() => void` | Shows a close button in the header and calls this when it is clicked. |

## Types

Exported by the package: `import type { FilterField, FilterCondition, FilterLabelsInput, StringFilterField, NumberFilterField, DateFilterField, SelectFilterField, BoolFilterField, CustomFilterField, FilterConditionValue, FilterLabels, FilterPeriod, FilterTimeUnit, FilterJoin, FilterThrough, FilterOperatorDefinition, FilterOperatorSet, FilterItem, FilterInput, Item } from '@likable-hair/svelte';`

### FilterField

A filterable field: what the app declares for each column the user can filter.

`type FilterField = StringFilterField \| NumberFilterField \| DateFilterField \| SelectFilterField \| BoolFilterField \| CustomFilterField`

### FilterCondition

A condition of the tree: a field, an operator and a value.

`interface FilterCondition`

| Name | Type | Description |
| --- | --- | --- |
| `id` (required) | `string` | Identity inside the tree, made by the helpers. Not shown anywhere. |
| `field` (required) | `string` | `name` of the field. |
| `operator` (required) | `string` | Name of the operator, one of the field's. |
| `value` | `FilterConditionValue` | Value for the operator. |
| `locked` | `boolean` | The user sees the condition but cannot change or remove it. |
| `preset` | `string` | `id` of the preset that added the condition: the preset chip is on while its conditions are in the tree. |

### FilterLabelsInput

Texts to change: any of `FilterLabels`, with `operators`, `periods` and `units` partial too.

`type FilterLabelsInput = Partial<Omit<FilterLabels, 'operators' \| 'periods' \| 'units'>> & { … }`

| Name | Type | Description |
| --- | --- | --- |
| `operators` | `Partial<Record<string, string>>` |  |
| `periods` | `Partial<Record<FilterPeriod, string>>` |  |
| `units` | `Partial<Record<FilterTimeUnit, string>>` |  |

### StringFilterField

A text field.

`interface StringFilterField extends FieldBase`

| Name | Type | Description |
| --- | --- | --- |
| `name` (required) | `string` | Identity of the field, stored in the conditions. |
| `label` (required) | `string` | Name shown to the user. |
| `icon` | `string` | SVG path of an icon shown in the list, the card and the pill. |
| `category` | `string` | Groups the fields in the list of "Add filter". |
| `description` | `string` | Secondary text in the list of fields. |
| `column` | `string` | Column of the query. Defaults to `name`. |
| `joins` | `FilterJoin[]` | Joins added to the query when the field has a condition. |
| `through` | `FilterThrough \| FilterThrough[]` | The condition is checked in a subquery on a related table (`column` is a column of the last hop). Several hops nest. |
| `operators` | `FilterOperatorSet \| (string \| FilterOperatorDefinition)[]` | Operators offered, in this order: names of the built-in ones of the type, definitions of new ones, or a whole set (`basic`, `standard`, `all`). Defaults to the set chosen on `Filters` (`standard`). |
| `defaultOperator` | `string` | Operator of a new condition. Defaults to the first one. |
| `pinned` | `boolean` | Always shown as a pill in the bar, even without a condition. |
| `hidden` | `boolean` | Never shown: its conditions (usually `locked`) only reach the query. |
| `defaultValue` | `FilterConditionValue` | Value of a new condition on this field (the card and the panel start with it). |
| `isComplete` | `(condition: FilterCondition) => boolean` | Decides when a condition on this field is complete (it reaches the query and gets a pill). Defaults to a check on the value for the operator. |
| `apply` | `(builder: FilterBuilder, condition: FilterCondition) => void` | Replaces the whole query of a condition on this field. |
| `whenAbsent` | `(builder: FilterBuilder) => void` | Query added when the field has no condition at all (for example "hide archived" until the user asks for them). |
| `format` | `(condition: FilterCondition) => string` | Replaces the text of the value in the pill. |
| `negativesIncludeEmpty` | `boolean` | Negative operators (`isNot`, `notContains`, `ne`, `notIn`, `noneOf`, `notBetween`) also keep the rows where the column is empty: "surname is not Rossi" shows the clients without a surname too. On by default (option of `Filters` / `filtersToBuilder`); this one wins. Needs a backend that knows `null` modifiers: pass `false` for backends E and E'. |
| `json` | `{ column: string; key: string; cast?: 'text' \| 'numeric' \| 'date' \| 'timestamp' \| 'boolean' }` | The value lives in a key of a JSONB column (`customFields.color`): equalities use `whereJsonSuperset`; with `cast` every operator works through a parametrized `whereRaw` (accepted only by backends that allow raw conditions). |
| `type` (required) | `'string'` |  |
| `columns` | `string[]` | Several columns: a condition holds when any of them matches (`contains`), or when none does (`isNot`, `notContains`). |
| `words` | `boolean` | Splits the text into words, for names split over `columns`: `contains` wants every word in some column; `is` wants every word as a whole word of some column, ignoring case unless `caseSensitive` ("mario rossi" finds Mario Rossi, Rossi Mario and Maria Grazia De Rossi with "maria rossi", not Mariolina Rossini); `notContains` and `isNot` are their exact opposites (some word is missing). The other operators compare the whole text. |
| `caseSensitive` | `boolean` | Tells upper and lower case apart. Without it every text operator ignores case: `contains` with `ilike`, `is`, `isNot`, `anyOf`, `noneOf`, `startsWith` and `endsWith` with a Postgres regex (`~*`). With it the equalities use `=`, `<>` and `in`, as in v4, which can use the column's index. |
| `suggest` | `(params: { searchText: string; signal: AbortSignal }) => Promise<FilterItem[]>` | Suggestions while typing, for the `match` operator: picking one filters on its exact value (on `suggestColumn`), keeping the typed text filters with `contains`. |
| `suggestColumn` | `string` | Column compared with the value of a picked suggestion. Defaults to `column`. |
| `blankIsEmpty` | `boolean` | With `empty` / `notEmpty`, an empty string counts as empty too. Default `true`. |
| `placeholder` | `string` | Placeholder of the input. |

### NumberFilterField

A number field.

`interface NumberFilterField extends FieldBase`

| Name | Type | Description |
| --- | --- | --- |
| `name` (required) | `string` | Identity of the field, stored in the conditions. |
| `label` (required) | `string` | Name shown to the user. |
| `icon` | `string` | SVG path of an icon shown in the list, the card and the pill. |
| `category` | `string` | Groups the fields in the list of "Add filter". |
| `description` | `string` | Secondary text in the list of fields. |
| `column` | `string` | Column of the query. Defaults to `name`. |
| `joins` | `FilterJoin[]` | Joins added to the query when the field has a condition. |
| `through` | `FilterThrough \| FilterThrough[]` | The condition is checked in a subquery on a related table (`column` is a column of the last hop). Several hops nest. |
| `operators` | `FilterOperatorSet \| (string \| FilterOperatorDefinition)[]` | Operators offered, in this order: names of the built-in ones of the type, definitions of new ones, or a whole set (`basic`, `standard`, `all`). Defaults to the set chosen on `Filters` (`standard`). |
| `defaultOperator` | `string` | Operator of a new condition. Defaults to the first one. |
| `pinned` | `boolean` | Always shown as a pill in the bar, even without a condition. |
| `hidden` | `boolean` | Never shown: its conditions (usually `locked`) only reach the query. |
| `defaultValue` | `FilterConditionValue` | Value of a new condition on this field (the card and the panel start with it). |
| `isComplete` | `(condition: FilterCondition) => boolean` | Decides when a condition on this field is complete (it reaches the query and gets a pill). Defaults to a check on the value for the operator. |
| `apply` | `(builder: FilterBuilder, condition: FilterCondition) => void` | Replaces the whole query of a condition on this field. |
| `whenAbsent` | `(builder: FilterBuilder) => void` | Query added when the field has no condition at all (for example "hide archived" until the user asks for them). |
| `format` | `(condition: FilterCondition) => string` | Replaces the text of the value in the pill. |
| `negativesIncludeEmpty` | `boolean` | Negative operators (`isNot`, `notContains`, `ne`, `notIn`, `noneOf`, `notBetween`) also keep the rows where the column is empty: "surname is not Rossi" shows the clients without a surname too. On by default (option of `Filters` / `filtersToBuilder`); this one wins. Needs a backend that knows `null` modifiers: pass `false` for backends E and E'. |
| `json` | `{ column: string; key: string; cast?: 'text' \| 'numeric' \| 'date' \| 'timestamp' \| 'boolean' }` | The value lives in a key of a JSONB column (`customFields.color`): equalities use `whereJsonSuperset`; with `cast` every operator works through a parametrized `whereRaw` (accepted only by backends that allow raw conditions). |
| `type` (required) | `'number'` |  |
| `unit` | `string` | Text after the value (`€`, `kg`). |
| `scale` | `number` | Multiplies the typed value before the query (for example `1000` to type tons of a column in kilograms). |
| `step` | `number` | `step` of the input. |
| `compareWith` | `{ column: string; label: string }[]` | Other columns the value can be compared with (`quantity < minimum quantity`): adds the column operators. |

### DateFilterField

A date field.

`interface DateFilterField extends FieldBase`

| Name | Type | Description |
| --- | --- | --- |
| `name` (required) | `string` | Identity of the field, stored in the conditions. |
| `label` (required) | `string` | Name shown to the user. |
| `icon` | `string` | SVG path of an icon shown in the list, the card and the pill. |
| `category` | `string` | Groups the fields in the list of "Add filter". |
| `description` | `string` | Secondary text in the list of fields. |
| `column` | `string` | Column of the query. Defaults to `name`. |
| `joins` | `FilterJoin[]` | Joins added to the query when the field has a condition. |
| `through` | `FilterThrough \| FilterThrough[]` | The condition is checked in a subquery on a related table (`column` is a column of the last hop). Several hops nest. |
| `operators` | `FilterOperatorSet \| (string \| FilterOperatorDefinition)[]` | Operators offered, in this order: names of the built-in ones of the type, definitions of new ones, or a whole set (`basic`, `standard`, `all`). Defaults to the set chosen on `Filters` (`standard`). |
| `defaultOperator` | `string` | Operator of a new condition. Defaults to the first one. |
| `pinned` | `boolean` | Always shown as a pill in the bar, even without a condition. |
| `hidden` | `boolean` | Never shown: its conditions (usually `locked`) only reach the query. |
| `defaultValue` | `FilterConditionValue` | Value of a new condition on this field (the card and the panel start with it). |
| `isComplete` | `(condition: FilterCondition) => boolean` | Decides when a condition on this field is complete (it reaches the query and gets a pill). Defaults to a check on the value for the operator. |
| `apply` | `(builder: FilterBuilder, condition: FilterCondition) => void` | Replaces the whole query of a condition on this field. |
| `whenAbsent` | `(builder: FilterBuilder) => void` | Query added when the field has no condition at all (for example "hide archived" until the user asks for them). |
| `format` | `(condition: FilterCondition) => string` | Replaces the text of the value in the pill. |
| `negativesIncludeEmpty` | `boolean` | Negative operators (`isNot`, `notContains`, `ne`, `notIn`, `noneOf`, `notBetween`) also keep the rows where the column is empty: "surname is not Rossi" shows the clients without a surname too. On by default (option of `Filters` / `filtersToBuilder`); this one wins. Needs a backend that knows `null` modifiers: pass `false` for backends E and E'. |
| `json` | `{ column: string; key: string; cast?: 'text' \| 'numeric' \| 'date' \| 'timestamp' \| 'boolean' }` | The value lives in a key of a JSONB column (`customFields.color`): equalities use `whereJsonSuperset`; with `cast` every operator works through a parametrized `whereRaw` (accepted only by backends that allow raw conditions). |
| `type` (required) | `'date'` |  |
| `dateOnly` | `boolean` | The column is a `date` (no time): the query compares `yyyy-MM-dd` strings. Otherwise it compares timestamps from the start to the end of the local days. |
| `periods` | `FilterPeriod[]` | Periods offered by the `period` operator, in this order. |
| `withTime` | `boolean` | The value has a time too: `before`, `after`, `between` and the relative operators compare instants (`yyyy-MM-ddTHH:mm`), and minutes and hours become available. |
| `compareWith` | `{ column: string; label: string }[]` | Other columns the value can be compared with (`deliveredAt after promisedAt`): adds the column operators. |

### SelectFilterField

A field whose value is chosen among options.

`interface SelectFilterField extends FieldBase`

| Name | Type | Description |
| --- | --- | --- |
| `name` (required) | `string` | Identity of the field, stored in the conditions. |
| `label` (required) | `string` | Name shown to the user. |
| `icon` | `string` | SVG path of an icon shown in the list, the card and the pill. |
| `category` | `string` | Groups the fields in the list of "Add filter". |
| `description` | `string` | Secondary text in the list of fields. |
| `column` | `string` | Column of the query. Defaults to `name`. |
| `joins` | `FilterJoin[]` | Joins added to the query when the field has a condition. |
| `through` | `FilterThrough \| FilterThrough[]` | The condition is checked in a subquery on a related table (`column` is a column of the last hop). Several hops nest. |
| `operators` | `FilterOperatorSet \| (string \| FilterOperatorDefinition)[]` | Operators offered, in this order: names of the built-in ones of the type, definitions of new ones, or a whole set (`basic`, `standard`, `all`). Defaults to the set chosen on `Filters` (`standard`). |
| `defaultOperator` | `string` | Operator of a new condition. Defaults to the first one. |
| `pinned` | `boolean` | Always shown as a pill in the bar, even without a condition. |
| `hidden` | `boolean` | Never shown: its conditions (usually `locked`) only reach the query. |
| `defaultValue` | `FilterConditionValue` | Value of a new condition on this field (the card and the panel start with it). |
| `isComplete` | `(condition: FilterCondition) => boolean` | Decides when a condition on this field is complete (it reaches the query and gets a pill). Defaults to a check on the value for the operator. |
| `apply` | `(builder: FilterBuilder, condition: FilterCondition) => void` | Replaces the whole query of a condition on this field. |
| `whenAbsent` | `(builder: FilterBuilder) => void` | Query added when the field has no condition at all (for example "hide archived" until the user asks for them). |
| `format` | `(condition: FilterCondition) => string` | Replaces the text of the value in the pill. |
| `negativesIncludeEmpty` | `boolean` | Negative operators (`isNot`, `notContains`, `ne`, `notIn`, `noneOf`, `notBetween`) also keep the rows where the column is empty: "surname is not Rossi" shows the clients without a surname too. On by default (option of `Filters` / `filtersToBuilder`); this one wins. Needs a backend that knows `null` modifiers: pass `false` for backends E and E'. |
| `json` | `{ column: string; key: string; cast?: 'text' \| 'numeric' \| 'date' \| 'timestamp' \| 'boolean' }` | The value lives in a key of a JSONB column (`customFields.color`): equalities use `whereJsonSuperset`; with `cast` every operator works through a parametrized `whereRaw` (accepted only by backends that allow raw conditions). |
| `type` (required) | `'select'` |  |
| `items` | `FilterItem[]` | The options, when they are known. |
| `searcher` | `(params: { searchText: string; signal: AbortSignal }) => Promise<FilterItem[]>` | Loads the options matching the typed text (from an API), instead of `items`. |
| `view` | `'autocomplete' \| 'toggle' \| 'countries'` | How the options are picked: an autocomplete (default), toggle buttons (few options), or countries with flags. |
| `shortcuts` | `FilterItem[]` | Options added at the top of the list, such as "Me" for the current user. |
| `locale` | `string` | Locale of the country names with `view: 'countries'`. |
| `placeholder` | `string` | Placeholder of the input. |
| `array` | `boolean` | The column (or the JSON key, with `json`) holds a list of values: `in` = has any of (`&&`), `notIn` = has none of, `all` = has all of (`@>`), `empty` = null or `{}`. |

### BoolFilterField

A yes/no field.

`interface BoolFilterField extends FieldBase`

| Name | Type | Description |
| --- | --- | --- |
| `name` (required) | `string` | Identity of the field, stored in the conditions. |
| `label` (required) | `string` | Name shown to the user. |
| `icon` | `string` | SVG path of an icon shown in the list, the card and the pill. |
| `category` | `string` | Groups the fields in the list of "Add filter". |
| `description` | `string` | Secondary text in the list of fields. |
| `column` | `string` | Column of the query. Defaults to `name`. |
| `joins` | `FilterJoin[]` | Joins added to the query when the field has a condition. |
| `through` | `FilterThrough \| FilterThrough[]` | The condition is checked in a subquery on a related table (`column` is a column of the last hop). Several hops nest. |
| `operators` | `FilterOperatorSet \| (string \| FilterOperatorDefinition)[]` | Operators offered, in this order: names of the built-in ones of the type, definitions of new ones, or a whole set (`basic`, `standard`, `all`). Defaults to the set chosen on `Filters` (`standard`). |
| `defaultOperator` | `string` | Operator of a new condition. Defaults to the first one. |
| `pinned` | `boolean` | Always shown as a pill in the bar, even without a condition. |
| `hidden` | `boolean` | Never shown: its conditions (usually `locked`) only reach the query. |
| `defaultValue` | `FilterConditionValue` | Value of a new condition on this field (the card and the panel start with it). |
| `isComplete` | `(condition: FilterCondition) => boolean` | Decides when a condition on this field is complete (it reaches the query and gets a pill). Defaults to a check on the value for the operator. |
| `apply` | `(builder: FilterBuilder, condition: FilterCondition) => void` | Replaces the whole query of a condition on this field. |
| `whenAbsent` | `(builder: FilterBuilder) => void` | Query added when the field has no condition at all (for example "hide archived" until the user asks for them). |
| `format` | `(condition: FilterCondition) => string` | Replaces the text of the value in the pill. |
| `negativesIncludeEmpty` | `boolean` | Negative operators (`isNot`, `notContains`, `ne`, `notIn`, `noneOf`, `notBetween`) also keep the rows where the column is empty: "surname is not Rossi" shows the clients without a surname too. On by default (option of `Filters` / `filtersToBuilder`); this one wins. Needs a backend that knows `null` modifiers: pass `false` for backends E and E'. |
| `json` | `{ column: string; key: string; cast?: 'text' \| 'numeric' \| 'date' \| 'timestamp' \| 'boolean' }` | The value lives in a key of a JSONB column (`customFields.color`): equalities use `whereJsonSuperset`; with `cast` every operator works through a parametrized `whereRaw` (accepted only by backends that allow raw conditions). |
| `type` (required) | `'bool'` |  |
| `trueLabel` | `string` | Text of `isTrue` in the pill and the card. Default `Yes`. |
| `falseLabel` | `string` | Text of `isFalse`. Default `No`. |
| `whenTrue` | `(builder: FilterBuilder) => void` | Query of `isTrue`, when it is not `column = true` (for example `whereColumn('quantity', '<', 'minQuantity')`). |
| `whenFalse` | `(builder: FilterBuilder) => void` | Query of `isFalse`, when it is not `column = false`. |
| `nullable` | `boolean` | The column can be null: adds `empty` and `notEmpty`. |

### CustomFilterField

A field with an input of the app.

`interface CustomFilterField extends FieldBase`

| Name | Type | Description |
| --- | --- | --- |
| `name` (required) | `string` | Identity of the field, stored in the conditions. |
| `label` (required) | `string` | Name shown to the user. |
| `icon` | `string` | SVG path of an icon shown in the list, the card and the pill. |
| `category` | `string` | Groups the fields in the list of "Add filter". |
| `description` | `string` | Secondary text in the list of fields. |
| `column` | `string` | Column of the query. Defaults to `name`. |
| `joins` | `FilterJoin[]` | Joins added to the query when the field has a condition. |
| `through` | `FilterThrough \| FilterThrough[]` | The condition is checked in a subquery on a related table (`column` is a column of the last hop). Several hops nest. |
| `operators` | `FilterOperatorSet \| (string \| FilterOperatorDefinition)[]` | Operators offered, in this order: names of the built-in ones of the type, definitions of new ones, or a whole set (`basic`, `standard`, `all`). Defaults to the set chosen on `Filters` (`standard`). |
| `defaultOperator` | `string` | Operator of a new condition. Defaults to the first one. |
| `pinned` | `boolean` | Always shown as a pill in the bar, even without a condition. |
| `hidden` | `boolean` | Never shown: its conditions (usually `locked`) only reach the query. |
| `defaultValue` | `FilterConditionValue` | Value of a new condition on this field (the card and the panel start with it). |
| `isComplete` | `(condition: FilterCondition) => boolean` | Decides when a condition on this field is complete (it reaches the query and gets a pill). Defaults to a check on the value for the operator. |
| `apply` | `(builder: FilterBuilder, condition: FilterCondition) => void` | Replaces the whole query of a condition on this field. |
| `whenAbsent` | `(builder: FilterBuilder) => void` | Query added when the field has no condition at all (for example "hide archived" until the user asks for them). |
| `format` | `(condition: FilterCondition) => string` | Replaces the text of the value in the pill. |
| `negativesIncludeEmpty` | `boolean` | Negative operators (`isNot`, `notContains`, `ne`, `notIn`, `noneOf`, `notBetween`) also keep the rows where the column is empty: "surname is not Rossi" shows the clients without a surname too. On by default (option of `Filters` / `filtersToBuilder`); this one wins. Needs a backend that knows `null` modifiers: pass `false` for backends E and E'. |
| `json` | `{ column: string; key: string; cast?: 'text' \| 'numeric' \| 'date' \| 'timestamp' \| 'boolean' }` | The value lives in a key of a JSONB column (`customFields.color`): equalities use `whereJsonSuperset`; with `cast` every operator works through a parametrized `whereRaw` (accepted only by backends that allow raw conditions). |
| `type` (required) | `'custom'` |  |

### FilterConditionValue

Value of a condition. Its shape depends on the operator (see `FilterInput`). Dates are `yyyy-MM-dd` strings, so the whole tree is plain JSON.

`type FilterConditionValue = string \| number \| boolean \| { from?: number \| string; to?: number \| string } \| FilterItem[] \| FilterPeriod \| unknown`

### FilterLabels

Every text of the filters, to translate them. Pass the ones to change: the others keep the English default.

`interface FilterLabels`

| Name | Type | Description |
| --- | --- | --- |
| `filters` (required) | `string` |  |
| `addFilter` (required) | `string` |  |
| `addNestedFilter` (required) | `string` |  |
| `clear` (required) | `string` |  |
| `clearAll` (required) | `string` |  |
| `clearGroup` (required) | `string` |  |
| `where` (required) | `string` |  |
| `and` (required) | `string` |  |
| `or` (required) | `string` |  |
| `searchFields` (required) | `string` |  |
| `noFields` (required) | `string` |  |
| `field` (required) | `string` |  |
| `operator` (required) | `string` |  |
| `value` (required) | `string` |  |
| `apply` (required) | `string` |  |
| `cancel` (required) | `string` |  |
| `remove` (required) | `string` |  |
| `close` (required) | `string` |  |
| `showResults` (required) | `string` |  |
| `editFilter` (required) | `string` |  |
| `removeFilter` (required) | `string` |  |
| `from` (required) | `string` |  |
| `to` (required) | `string` |  |
| `days` (required) | `string` |  |
| `yes` (required) | `string` |  |
| `no` (required) | `string` |  |
| `more` (required) | `string` |  |
| `conditions` (required) | `string` |  |
| `anyOf` (required) | `string` |  |
| `allOf` (required) | `string` |  |
| `addValue` (required) | `string` |  |
| `choose` (required) | `string` |  |
| `search` (required) | `string` |  |
| `amount` (required) | `string` |  |
| `showLess` (required) | `string` |  |
| `loading` (required) | `string` | Text of the list while options load. |
| `noResults` (required) | `string` | Text of the list when no option matches. |
| `searchError` (required) | `string` | Text of the list when a search fails. |
| `typeMore` (required) | `string` | Text of the list before a search starts; `{count}` is the number of characters. |
| `invalidDate` (required) | `string` | Error of a date field with a date that does not exist. |
| `chooseDate` (required) | `string` | Accessible name of the calendar button of a date field. |
| `previousMonth` (required) | `string` |  |
| `nextMonth` (required) | `string` |  |
| `previousYear` (required) | `string` |  |
| `nextYear` (required) | `string` |  |
| `dateLetters` (required) | `string` | Letters of day, month and year in the format hint of date fields: `dmy` shows `dd/mm/yyyy`, `gma` shows `gg/mm/aaaa`. |
| `invalidDateTime` (required) | `string` | Error of a date and time field with a date or a time that does not exist. |
| `chooseDateTime` (required) | `string` | Accessible name of the calendar button of a date and time field. |
| `hours` (required) | `string` | Accessible name of the hours column of a date and time field. |
| `minutes` (required) | `string` | Accessible name of the minutes column of a date and time field. |
| `amPm` (required) | `string` | Accessible name of the AM/PM column of a date and time field on a 12-hour clock. |
| `compareWith` (required) | `string` |  |
| `savedFilters` (required) | `string` |  |
| `saveFilters` (required) | `string` |  |
| `saveAs` (required) | `string` |  |
| `save` (required) | `string` |  |
| `deleteSaved` (required) | `string` |  |
| `noSaved` (required) | `string` |  |
| `unit` (required) | `string` |  |
| `units` (required) | `Record<FilterTimeUnit, string>` |  |
| `operators` (required) | `Record<string, string>` |  |
| `periods` (required) | `Record<FilterPeriod, string>` |  |

### FilterPeriod

A period relative to today, for the date operator `period`.

`type FilterPeriod = 'today' \| 'yesterday' \| 'tomorrow' \| 'thisWeek' \| 'lastWeek' \| 'nextWeek' \| 'thisMonth' \| 'lastMonth' \| 'nextMonth' \| 'thisQuarter' \| 'lastQuarter' \| 'nextQuarter' \| 'thisYear' \| 'lastYear' \| 'nextYear' \| 'monthToDate' \| 'quarterToDate' \| 'yearToDate'`

### FilterTimeUnit

Unit of a relative date (`inLast`, `inNext`, `olderThan`, `laterThan`). Minutes and hours need `withTime`.

`type FilterTimeUnit = 'minutes' \| 'hours' \| 'days' \| 'weeks' \| 'months' \| 'years'`

### FilterJoin

A join the field needs, added once per query however many conditions use it.

`interface FilterJoin`

| Name | Type | Description |
| --- | --- | --- |
| `table` (required) | `string` | Table, with an alias if needed (`suppliers as primary_supplier`). |
| `on` (required) | `[string, string] \| [string, string, string]` | `[from, to]` (equality) or `[from, operator, to]`. |
| `kind` | `'left' \| 'inner'` | Default `left`, so the join itself filters nothing. |

### FilterThrough

A hop of a relation reached through a subquery: `key IN (SELECT select FROM from WHERE ...)`.

`interface FilterThrough`

| Name | Type | Description |
| --- | --- | --- |
| `key` (required) | `string` | Column of the outer query compared with the subquery (`users.id`). |
| `from` (required) | `string` | Table of the subquery (`user_residences`). |
| `select` (required) | `string` | Column selected by the subquery (`userId`). |
| `joins` | `FilterJoin[]` | Joins inside the subquery. |
| `where` | `(builder: FilterBuilder) => void` | Fixed conditions inside the subquery. |

### FilterOperatorDefinition

An operator defined by the app for one field.

`interface FilterOperatorDefinition`

| Name | Type | Description |
| --- | --- | --- |
| `name` (required) | `string` | Name stored in the conditions. |
| `label` (required) | `string` | Text between the field and the value (`is`, `≥`, `has tag`). |
| `input` (required) | `FilterInput` | Input shown for the value. `custom` uses the field's `inputSnippet`. |
| `apply` | `(builder: FilterBuilder, condition: FilterCondition) => void` | Adds the query for a condition with this operator. Without it the field's `apply` is used. |

### FilterOperatorSet

How many operators the fields offer: `basic` the essential ones, `standard` the common ones (default), `all` every built-in one.

`type FilterOperatorSet = 'basic' \| 'standard' \| 'all'`

### FilterItem

An option of a select field: an `Item`, optionally standing for several values in the query.

`type FilterItem<Data = unknown> = Item<Data> & { … }`

| Name | Type | Description |
| --- | --- | --- |
| `values` | `(string \| number)[]` | Values the option stands for in the query (for example "In progress" = four statuses). Defaults to `[value]`. |

### FilterInput

Kind of value an operator asks for, which decides the input shown to the user.

`type FilterInput = 'none' \| 'text' \| 'texts' \| 'number' \| 'numbers' \| 'numberRange' \| 'date' \| 'dateRange' \| 'datetime' \| 'datetimeRange' \| 'days' \| 'relative' \| 'period' \| 'items' \| 'column' \| 'suggest' \| 'custom'`

### Item

An option of a picker (Autocomplete, Select, ...).

`type Item<Data = unknown> = { … }`

| Name | Type | Description |
| --- | --- | --- |
| `value` (required) | `string \| number` | Identity of the option: two items are the same option when their `value`s are equal (`===`). |
| `label` | `string \| number` | Text shown and searched. Defaults to `value`. |
| `icon` | `string` | SVG path of an icon shown before the label, for example from `@mdi/js`. |
| `data` | `Data` | Any extra data, available in snippets and callbacks. |

## CSS variables

For a single instance, pass them as style props: `<FilterEditor --filter-editor-color="tomato">`. The `--filter-editor-default-*` defaults apply to the whole app: override them in `:root` or in a theme.

| Per-instance variable | Default |
| --- | --- |
| `--filter-editor-gap` | `--filter-editor-default-gap`: `12px` |
| `--filter-editor-width` | `--filter-editor-default-width`: `320px` |
| `--filter-editor-padding` | `--filter-editor-default-padding`: `12px` |
| `--filter-editor-color` | `--filter-editor-default-color`: `var(--global-color-text)` |
| `--filter-editor-title-font-size` | `--filter-editor-default-title-font-size`: `14px` |
| `--filter-editor-title-font-weight` | `--filter-editor-default-title-font-weight`: `600` |
| `--filter-editor-icon-color` | `--filter-editor-default-icon-color`: `var(--global-color-primary)` |
| `--filter-editor-operators-border-radius` | `--filter-editor-default-operators-border-radius`: `var(--global-radius-md)` |
| `--filter-editor-operators-background` | `--filter-editor-default-operators-background`: `var(--global-color-surface-2)` |
| `--filter-editor-operator-color` | `--filter-editor-default-operator-color`: `var(--global-color-text-2)` |
| `--filter-editor-operator-selected-background` | `--filter-editor-default-operator-selected-background`: `var(--global-color-surface-pop)` |
| `--filter-editor-operator-selected-color` | `--filter-editor-default-operator-selected-color`: `var(--global-color-primary)` |
| `--filter-editor-operator-selected-border-color` | `--filter-editor-default-operator-selected-border-color`: `var(--global-color-border-strong)` |
