/** * Checkout preview cards — a radio group whose options are drawn as mini * wireframes of the checkout they produce, rather than as icons or segments. * The third of its kind in the admin, after `SidebarLayoutCards` and * `CartStyleCards`, and for the same reason: both choices here are *shapes* * (where the summary sits; what the step strip looks like), and a shape reads * faster as a picture than as a word. * * The same goes for Visual style's three measures — form width, field * spacing, input style — which are proportions and treatments a customer * sees, so they get wireframes too. * * One component rather than several files because the card chrome — ring, tick * badge, roving-focus arrow keys — is identical for all; only the contents of * the thumbnail differ, which is what `shape` selects. Unrecognised values fall * back to a blank thumbnail with the label still readable, so a style added * server-side later renders a card instead of breaking the grid. */ import { __ } from '@wordpress/i18n'; import { Check } from 'lucide-react'; export interface CheckoutPreviewOption { value: string; label: string; description?: string; } interface CheckoutPreviewCardsProps { label?: string; /** Short line under the label saying what the choice controls. */ description?: string; value: string; options: CheckoutPreviewOption[]; onChange: ( value: string ) => void; /** Which family of wireframe to draw inside each card's thumbnail. */ shape: 'layout' | 'steps' | 'width' | 'density' | 'input'; disabled?: boolean; help?: string; } /** * The page-layout thumbnails: a form column of text stubs, with the order * summary either beside it (two columns) or absent (one column, where the * summary stacks below the fold and so isn't part of the shape being chosen). * * @param value The layout value. */ function layoutThumb( value: string ) { const lines = ( count: number ) => Array.from( { length: count } ).map( ( _, index ) => ( // eslint-disable-next-line react/no-array-index-key ) ); if ( 'one-column' === value ) { return ( { lines( 4 ) } ); } return ( <> { lines( 4 ) } ); } /** * The step-strip thumbnails: three markers in a row, the first one current. * `numbers` draws them as numbered pills, anything else as plain dots. * * @param value The step-style value. */ function stepsThumb( value: string ) { const numbered = 'numbers' === value; return ( { [ 1, 2, 3 ].map( ( step ) => ( { numbered ? step : null } ) ) } ); } /** * The form-width thumbnails: a browser window with a content sheet whose * width is the measure being chosen. * * @param value The form-width value. */ function widthThumb( value: string ) { return ( ); } /** * The field-spacing thumbnails: three field bars, with the gap between them * being the density. * * @param value The density value. */ function densityThumb( value: string ) { return ( ); } /** * The input-style thumbnails: one sample field drawn in the chosen treatment. * * @param value The input-style value. */ function inputThumb( value: string ) { return ( { __( 'Email address', 'aureon' ) } ); } const THUMBS: Record< CheckoutPreviewCardsProps[ 'shape' ], ( value: string ) => JSX.Element > = { layout: layoutThumb, steps: stepsThumb, width: widthThumb, density: densityThumb, input: inputThumb, }; export default function CheckoutPreviewCards( { label, description, value, options, onChange, shape, disabled = false, help, }: CheckoutPreviewCardsProps ) { const move = ( delta: number ) => { const index = options.findIndex( ( option ) => option.value === value ); const next = options[ ( index + delta + options.length ) % options.length ]; if ( next ) { onChange( next.value ); } }; return (
{ label && ( { label } ) } { description && (

{ description }

) }
{ options.map( ( option ) => { const checked = option.value === value; return ( ); } ) }
{ help && (

{ help }

) }
); }