Rule Builder¶
Rules of field, operator, and value under a choice of matching all of them or any one. It is a Repeater whose value control follows the chosen field: a text box, a select, a number box, or chips. A warning row under the rules says when they need attention.
Decision¶
| When | Use |
|---|---|
| A filter the reader writes, such as which items a sync takes or which events send a notification | Rule Builder |
| Filters the reader picks from fixed choices | Filter Panel |
| Rows of fields that do not depend on each other | Repeater |
Usage¶
import { createRuleBuilder } from './configurationpage?name=myplugin_stolon.js';
var rules = createRuleBuilder({
fields: [
{ key: 'title', label: 'Title' },
{ key: 'year', label: 'Year', type: 'number' },
{ key: 'type', label: 'Type', type: 'select', options: ['Movie', 'Series'] },
{ key: 'genres', label: 'Genres', type: 'chips' }
],
value: config.Rules,
warn: function (value) { return value.rules.length ? '' : 'Every item matches.'; },
onChange: function (value) { config.Rules = value; }
});
view.querySelector('#rulesHost').appendChild(rules.element);
Classes: stolon-rule-builder, stolon-rule-match, stolon-rule-rows, stolon-rule-value, stolon-rule-warning. The handle is { element, getValue, setValue, isValid, setWarning, destroy }.
States and Modifiers¶
- The value is
{ match, rules }, wherematchis'all'or'any'and each rule is{ field, operator, value }. A chips value is a list of text. - A field is
{ key, label, type, options, operators, placeholder }.typeistext(default),select,number, orchips.operatorsreplaces the type's own list, given inRULE_OPERATORS. - Changing a rule's field swaps its operators and its value control, and keeps the value while the type stays the same.
- The match choice is a Pill Group, "Match all" and "Match any" by
allTextandanyText. warn(value)returns a warning sentence after each change, shown under the rules as a warning Field Message.setWarning(text)shows one from code, and an empty value clears it.- A rule's value is required.
isValid()checks every rule and moves focus to the first invalid one. - The rows, their limits, and their labels come from the Repeater.
addTextis "Add rule",rowNameis "Rule", and the column labels are "Field", "Operator", and "Value".
Reference¶
Generated from the code by scripts/build-docs.mjs. Edit the code, not this section.
RULE_OPERATORS¶
var RULE_OPERATORS: Record<string, Array<{ value: string; label: string; }>>
createRuleBuilder¶
function createRuleBuilder(options: RuleBuilderOptions): RuleBuilderHandle
Rows of field, operator, and value, with a choice of matching all rules or any one. It is a Repeater whose value control follows the chosen field: a text box, a select, a number box, or chips. A warning row under the rules says when they need attention.
| Option | Type | What it does |
|---|---|---|
fields (required) |
RuleBuilderField[] |
array of { key, label, type ('text', 'select', 'number', or 'chips'), options, operators, placeholder } [required] |
value |
RuleBuilderValue | null |
{ match ('all' or 'any'), rules: [{ field, operator, value }] } [optional] |
allText |
string |
|
anyText |
string |
|
matchLabel |
string |
aria-label of the match choice (default 'Match') |
fieldLabel |
string |
|
operatorLabel |
string |
|
valueLabel |
string |
|
addText |
string |
text of the add button (default 'Add rule') |
removeText |
string |
aria-label of a remove button, where {value} is the rule's name (default 'Remove {value}') |
rowName |
string |
prefix of a rule's name (default 'Rule') |
maxRows |
number |
|
minRows |
number |
|
limitText |
string |
|
emptyText |
string |
|
size |
'small' | 'medium' | 'large' |
'small', 'medium', or 'large' for its fields (default 'medium') |
warn |
(value: RuleBuilderValue): string | null | undefined |
function(value) returning a warning sentence, or an empty value for none [optional] |
onChange |
(value: RuleBuilderValue): void |
function(value) after each change [optional] |
| Handle member | Type |
|---|---|
getValue |
(): RuleBuilderValue |
setValue |
(next: RuleBuilderValue | null): void |
isValid |
(): boolean |
setWarning |
(text: string | null | undefined): void |
destroy |
(): void |
| Type | What it is |
|---|---|
RuleBuilderField |
A field a rule can test. type picks the value control: a text box, a select of options, a number box, or chips. operators replaces the type's own list. |
RuleBuilderRule |
One rule: the field it tests, its operator, and the value, which is a list of text for a chips field. |
RuleBuilderValue |
The rules and whether an item must match all of them or any one. |
| Class | What it looks like |
|---|---|
stolon-rule-builder |
Rules of field, operator, and value under a choice of matching all or any, a Repeater with a warning row under the rules |
stolon-rule-value |
The value control of one rule, which follows the type of the rule's field |
stolon-rule-warning |
The warning under the rules, a Field Message in the warning state |
Example¶
Example Code¶
<!-- title: Rule Builder -->
<div id="rules"></div>
<script type="module">
const base = await window.__baseReady;
const rules = base.createRuleBuilder({
fields: [
{ key: 'title', label: 'Title', placeholder: 'Alien' },
{ key: 'year', label: 'Year', type: 'number', placeholder: '1979' },
{ key: 'type', label: 'Type', type: 'select', options: ['Movie', 'Series', 'Episode'] },
{ key: 'genres', label: 'Genres', type: 'chips', placeholder: 'Drama' }
],
value: { match: 'all', rules: [{ field: 'type', operator: 'is', value: 'Movie' }, { field: 'year', operator: 'gt', value: 2000 }, { field: 'genres', operator: 'any', value: ['Drama', 'Comedy'] }] },
warn: function (value) {
var years = value.rules.filter(function (r) { return r.field === 'year'; });
return value.match === 'all' && years.length > 1 ? 'Two year rules may never both match.' : '';
}
});
document.getElementById('rules').appendChild(rules.element);
</script>