/**
* Field group — a bordered card wrapping one named cluster of controls: icon
* tile, title and a one-line description, then the fields themselves below.
*
* Generalized from Product Catalog's "General" tab (the first to need it —
* Catalog Display, Layout, Grid, Toolbar, Sale Badge, Sale Price, Catalog
* Mode are each their own card rather than an unbordered `
`-labelled
* stack), but deliberately generic: any tab whose controls split into a
* handful of named groups can reach for this instead of `.aureon-admin__row`.
*/
import type { ReactNode } from 'react';
import { FormToggle } from '@wordpress/components';
import { useId, useState } from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';
import { ChevronDown, CircleQuestionMark as HelpCircle } from '../icons';
import type { PanelIcon } from '../module-icons';
/**
* The master switch a whole group hangs off — Cart's "Free Shipping" is the
* first: the card is one progress meter, and its remaining controls only mean
* anything once the meter is on. Drawn in the card's head rather than as the
* first row of its body, so the switch reads as *the* subject of the card and
* the body can be hidden behind it without leaving an orphan toggle.
*/
interface FieldGroupToggle {
checked: boolean;
onChange: ( checked: boolean ) => void;
disabled?: boolean;
/** Accessible name for the switch; defaults to the group's title. */
label?: string;
/**
* Whether switching off hides the body. Default true, which is the original
* behaviour: the switch owns the controls below it.
*
* False for a switch that governs something *other* than its own card —
* Social Login's provider toggles, which decide whether a provider appears
* on the front-end login form, not whether its credentials are editable.
* Hiding the fields there would be backwards: filling them in is exactly
* what you do before turning the provider on.
*/
hidesBody?: boolean;
/**
* Visible text beside the switch. Without it the switch is labelled by the
* card's title for assistive tech and shows nothing — right for a card whose
* title already *is* the thing being switched, wrong for one where the switch
* governs a named behaviour ("Enable restriction") the title doesn't state.
*/
text?: string;
}
/**
* A read-only state pill for the end of the head row, stating where the card's
* subject stands without spending a line of the body on it. Social Login's
* provider cards are the first: "Connected" once a provider's credentials are
* filled in and its switch is on, and what's missing when they aren't.
*
* Sits beside a `toggle` rather than instead of one, since the two say
* different things — the switch is what you set, the pill is what resulted.
*/
interface FieldGroupBadge {
text: string;
/** One of the panel's tone slugs; drives the dot and text colour. */
tone?: string;
}
interface FieldGroupProps {
/** Optional — a group that opens a section (Dark Mode's) can go without. */
icon?: PanelIcon;
/**
* `brand` drops the tinted, bordered tile the line icons sit in, for a card
* whose icon is a multi-colour brand mark that supplies its own shape and
* colour — see `brand-icons.tsx`.
*/
iconStyle?: 'default' | 'brand';
/**
* A step number in place of the icon, for a card that is one stage of an
* ordered setup. Takes precedence over `icon` — the head has room for one
* tile, and a numbered step makes a stronger claim than a glyph.
*/
step?: number;
title: string;
description?: string;
toggle?: FieldGroupToggle;
badge?: FieldGroupBadge;
/**
* A link at the end of the head row — "where do I find this?" beside the
* field that wants it. Sits where a badge would; a card wanting both would
* be saying too much in one row, so the badge wins.
*/
headLink?: { label: string; url: string };
/**
* Rendered under the head whatever the toggle's state, for the case a
* disabled group still has to say *why* — Cart's Free Shipping, whose
* switch greys out until the store configures an amount-gated
* free-shipping method, is the first. The body below stays hidden, so a
* note here is the only thing a reader would otherwise have to guess at.
*/
notice?: ReactNode;
/**
* Extra class on the card, for a caller whose groups need styling of their
* own — the declarative renderer's `aureon-panel-group`, which has to space
* its own body since its children aren't a hand-written section's.
*/
className?: string;
/**
* Fold the body away behind a chevron at the end of the head — for a view
* made of several long cards (Content Templates' Post Feed is the first),
* where a reader working on one group wants the others out of the way.
* Starts open; the fold is per-visit state, not a setting.
*/
collapsible?: boolean;
children: ReactNode;
}
export default function FieldGroup( {
icon: Icon,
iconStyle = 'default',
step,
title,
description,
toggle,
badge,
headLink,
notice,
className,
collapsible = false,
children,
}: FieldGroupProps ) {
const titleId = `aureon-fieldgroup-${ useId() }`;
const toggleId = `${ titleId }-toggle`;
const bodyId = `${ titleId }-body`;
const [ open, setOpen ] = useState( true );
const folded = collapsible && ! open;
return (