Skip to content

ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.

2.4 KB
Raw Blame
import { X } from 'lucide-react';
import { byKey, columnLabel, opLabel, ruleText } from './fields';
import { useTranslation } from 'react-i18next';
import { Button as SharedButton } from '@/components/ui/button';

// Active filter rules, spelled out above the grid. A filter you cannot see is
// a filter you will blame the data for — every rule gets a chip, and every chip
// can be removed on its own.
export function GridChips({ cfg, columns, onRemove, onClear, onMatchChange }) {
    const { t } = useTranslation();
    const rules = cfg.filters.rules;
    if (!rules.length) return null;
    const map = byKey(columns);

    return (
        <div className="sk-gridchips">
            {rules.length > 1 && (
                <SharedButton variant="unstyled"
                    type="button"
                    className="sk-gridchip sk-gridchip--match"
                    onClick={() => onMatchChange(cfg.filters.match === 'all' ? 'any' : 'all')}
                    title={t('app.gridChips.toggleWhetherRowsMustMatchEvery', 'Toggle whether rows must match every condition or any one')}
                >
                    match {cfg.filters.match}
                </SharedButton>
            )}
            {rules.map((rule) => {
                const column = map.get(rule.field);
                if (!column) return null;
                return (
                    <span key={rule.id} className="sk-gridchip">
                        <span className="sk-gridchip__k">{columnLabel(column)}</span>
                        <span className="sk-gridchip__op">{opLabel(column.type, rule.op)}</span>
                        <span className="sk-gridchip__v">{ruleText(rule, columns)}</span>
                        <SharedButton variant="unstyled"
                            type="button"
                            className="sk-gridchip__x"
                            onClick={() => onRemove(rule.id)}
                            aria-label={t('app.gridChips.removeFilter', 'Remove {{value}} filter', { value: columnLabel(column) })}
                        >
                            <X size={12} />
                        </SharedButton>
                    </span>
                );
            })}
            <SharedButton variant="unstyled" type="button" className="sk-gridchips__clear" onClick={onClear}>{t('app.gridChips.clearAll', 'Clear all')}</SharedButton>
        </div>
    );
}

export default GridChips;