feat(176): RuleBuilder component
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
import { cleanup, render, screen, fireEvent } from '@testing-library/react';
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
import { RuleBuilder } from './RuleBuilder';
|
||||
import type { RuleField } from './fieldCatalog';
|
||||
import type { Group } from './types';
|
||||
|
||||
afterEach(() => {
|
||||
cleanup();
|
||||
});
|
||||
|
||||
const FIELDS: RuleField[] = [
|
||||
{ name: 'genre', label: 'Genre', type: 'text', group: 'General', values: [] },
|
||||
{ name: 'type', label: 'Item type', type: 'enum', group: 'General', values: ['movie', 'episode'] }
|
||||
];
|
||||
|
||||
function setup(initial: Group) {
|
||||
const onChange = vi.fn();
|
||||
const utils = render(<RuleBuilder value={initial} onChange={onChange} fields={FIELDS} />);
|
||||
return { onChange, ...utils };
|
||||
}
|
||||
|
||||
describe('RuleBuilder', () => {
|
||||
it('adds a rule', () => {
|
||||
const { onChange } = setup({ match: 'all', children: [] });
|
||||
fireEvent.click(screen.getByText('Add rule'));
|
||||
expect(onChange).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
match: 'all',
|
||||
children: expect.arrayContaining([expect.objectContaining({ field: 'genre' })])
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it('removes a rule', () => {
|
||||
const { onChange } = setup({ match: 'all', children: [{ field: 'genre', operator: 'is', value: 'Horror' }] });
|
||||
fireEvent.click(screen.getByLabelText('Remove rule'));
|
||||
expect(onChange).toHaveBeenCalledWith({ match: 'all', children: [] });
|
||||
});
|
||||
|
||||
it('adds a nested group only at top level', () => {
|
||||
const { onChange } = setup({ match: 'all', children: [] });
|
||||
fireEvent.click(screen.getByText('Add group'));
|
||||
expect(onChange).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ children: expect.arrayContaining([expect.objectContaining({ match: 'any' })]) })
|
||||
);
|
||||
});
|
||||
|
||||
it('shows enum values as a dropdown', () => {
|
||||
setup({ match: 'all', children: [{ field: 'type', operator: 'is', value: 'movie' }] });
|
||||
expect(screen.getByRole('option', { name: 'episode' })).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,212 @@
|
||||
import { Plus, Trash2 } from 'lucide-react';
|
||||
import { Button, IconButton } from '../../components';
|
||||
import type { RuleField } from './fieldCatalog';
|
||||
import { isGroup, OPERATORS_BY_TYPE, type FieldType, type Group, type Operator, type Rule } from './types';
|
||||
|
||||
const OP_LABEL: Record<Operator, string> = {
|
||||
is: 'is',
|
||||
isNot: 'is not',
|
||||
contains: 'contains',
|
||||
startsWith: 'starts with',
|
||||
matches: 'contains',
|
||||
notMatches: 'does not contain',
|
||||
eq: '=',
|
||||
gt: '>',
|
||||
lt: '<',
|
||||
between: 'between',
|
||||
before: 'before',
|
||||
after: 'after'
|
||||
};
|
||||
|
||||
function typeOf(fields: RuleField[], name: string): FieldType {
|
||||
return fields.find((f) => f.name === name)?.type ?? 'text';
|
||||
}
|
||||
|
||||
function defaultRule(fields: RuleField[]): Rule {
|
||||
const field = fields[0]?.name ?? 'title';
|
||||
const type = typeOf(fields, field);
|
||||
return { field, operator: OPERATORS_BY_TYPE[type][0], value: '' };
|
||||
}
|
||||
|
||||
// Note: the shared `Input`/`Select` components (web/src/components/forms.tsx) don't accept an
|
||||
// aria-label prop (only a visible `label`), so rows use native <select>/<input> elements here to
|
||||
// keep the field/operator/value controls addressable by aria-label in tests and assistive tech.
|
||||
function RuleRow({
|
||||
rule,
|
||||
fields,
|
||||
onChange,
|
||||
onRemove
|
||||
}: {
|
||||
rule: Rule;
|
||||
fields: RuleField[];
|
||||
onChange: (r: Rule) => void;
|
||||
onRemove: () => void;
|
||||
}) {
|
||||
const type = typeOf(fields, rule.field);
|
||||
const ops = OPERATORS_BY_TYPE[type];
|
||||
const enumField = fields.find((f) => f.name === rule.field && f.type === 'enum');
|
||||
|
||||
return (
|
||||
<div style={{ display: 'flex', gap: 8, alignItems: 'center', marginBottom: 8 }}>
|
||||
<select
|
||||
aria-label="Field"
|
||||
value={rule.field}
|
||||
onChange={(e) => {
|
||||
const nextType = typeOf(fields, e.target.value);
|
||||
onChange({ field: e.target.value, operator: OPERATORS_BY_TYPE[nextType][0], value: '' });
|
||||
}}
|
||||
>
|
||||
{fields.map((f) => (
|
||||
<option key={f.name} value={f.name}>
|
||||
{f.label}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
|
||||
<select
|
||||
aria-label="Operator"
|
||||
value={rule.operator}
|
||||
onChange={(e) => onChange({ ...rule, operator: e.target.value as Operator })}
|
||||
>
|
||||
{ops.map((op) => (
|
||||
<option key={op} value={op}>
|
||||
{OP_LABEL[op]}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
|
||||
{enumField ? (
|
||||
<select aria-label="Value" value={rule.value} onChange={(e) => onChange({ ...rule, value: e.target.value })}>
|
||||
<option value="">—</option>
|
||||
{enumField.values.map((v) => (
|
||||
<option key={v} value={v}>
|
||||
{v}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
) : (
|
||||
<input
|
||||
aria-label="Value"
|
||||
className="ctv-input"
|
||||
value={rule.value}
|
||||
onChange={(e) => onChange({ ...rule, value: e.target.value })}
|
||||
type={type === 'number' ? 'number' : type === 'date' ? 'date' : 'text'}
|
||||
/>
|
||||
)}
|
||||
|
||||
{rule.operator === 'between' && (
|
||||
<input
|
||||
aria-label="Upper bound"
|
||||
className="ctv-input"
|
||||
value={rule.value2 ?? ''}
|
||||
onChange={(e) => onChange({ ...rule, value2: e.target.value })}
|
||||
type={type === 'number' ? 'number' : 'date'}
|
||||
/>
|
||||
)}
|
||||
|
||||
<IconButton title="Remove rule" onClick={onRemove}>
|
||||
<Trash2 size={14} />
|
||||
</IconButton>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function MatchToggle({ match, onChange }: { match: 'all' | 'any'; onChange: (m: 'all' | 'any') => void }) {
|
||||
return (
|
||||
<div style={{ display: 'flex', gap: 6, alignItems: 'center', marginBottom: 8 }}>
|
||||
<span>Match</span>
|
||||
<select aria-label="Match" value={match} onChange={(e) => onChange(e.target.value as 'all' | 'any')}>
|
||||
<option value="all">All</option>
|
||||
<option value="any">Any</option>
|
||||
</select>
|
||||
<span>of the following:</span>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function GroupEditor({
|
||||
group,
|
||||
fields,
|
||||
depth,
|
||||
onChange,
|
||||
onRemove
|
||||
}: {
|
||||
group: Group;
|
||||
fields: RuleField[];
|
||||
depth: number;
|
||||
onChange: (g: Group) => void;
|
||||
onRemove?: () => void;
|
||||
}) {
|
||||
const setChild = (i: number, child: Rule | Group) => {
|
||||
const children = group.children.slice();
|
||||
children[i] = child;
|
||||
onChange({ ...group, children });
|
||||
};
|
||||
const removeChild = (i: number) => onChange({ ...group, children: group.children.filter((_, j) => j !== i) });
|
||||
|
||||
return (
|
||||
<div
|
||||
style={{
|
||||
border: depth > 0 ? '1px solid var(--ctv-border, #333)' : 'none',
|
||||
borderRadius: 8,
|
||||
padding: depth > 0 ? 12 : 0,
|
||||
marginBottom: 8
|
||||
}}
|
||||
>
|
||||
<MatchToggle match={group.match} onChange={(m) => onChange({ ...group, match: m })} />
|
||||
{group.children.map((child, i) =>
|
||||
isGroup(child) ? (
|
||||
<GroupEditor
|
||||
key={i}
|
||||
group={child}
|
||||
fields={fields}
|
||||
depth={depth + 1}
|
||||
onChange={(g) => setChild(i, g)}
|
||||
onRemove={() => removeChild(i)}
|
||||
/>
|
||||
) : (
|
||||
<RuleRow key={i} rule={child} fields={fields} onChange={(r) => setChild(i, r)} onRemove={() => removeChild(i)} />
|
||||
)
|
||||
)}
|
||||
<div style={{ display: 'flex', gap: 8 }}>
|
||||
<Button
|
||||
size="sm"
|
||||
variant="secondary"
|
||||
startIcon={<Plus size={14} />}
|
||||
onClick={() => onChange({ ...group, children: [...group.children, defaultRule(fields)] })}
|
||||
>
|
||||
Add rule
|
||||
</Button>
|
||||
{depth === 0 && (
|
||||
<Button
|
||||
size="sm"
|
||||
variant="secondary"
|
||||
startIcon={<Plus size={14} />}
|
||||
onClick={() =>
|
||||
onChange({ ...group, children: [...group.children, { match: 'any', children: [defaultRule(fields)] }] })
|
||||
}
|
||||
>
|
||||
Add group
|
||||
</Button>
|
||||
)}
|
||||
{onRemove && (
|
||||
<IconButton title="Remove group" onClick={onRemove}>
|
||||
<Trash2 size={14} />
|
||||
</IconButton>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export function RuleBuilder({
|
||||
value,
|
||||
onChange,
|
||||
fields
|
||||
}: {
|
||||
value: Group;
|
||||
onChange: (g: Group) => void;
|
||||
fields: RuleField[];
|
||||
}) {
|
||||
return <GroupEditor group={value} fields={fields} depth={0} onChange={onChange} />;
|
||||
}
|
||||
Reference in New Issue
Block a user