/** * External dependencies */ import classnames from 'classnames'; import { __ } from '@wordpress/i18n'; import { CartResponseItem } from '@woocommerce/type-defs/cart-response'; import { createRef, useEffect, useRef } from '@wordpress/element'; import type { RefObject } from 'react'; /** * Internal dependencies */ import CartLineItemRow from './cart-line-item-row'; const placeholderRows = [ ...Array( 3 ) ].map( ( _x, i ) => ( ) ); interface CartLineItemsTableProps { lineItems: CartResponseItem[]; isLoading: boolean; className?: string; } const setRefs = ( lineItems: CartResponseItem[] ) => { const refs = {} as Record< string, RefObject< HTMLTableRowElement > >; lineItems.forEach( ( { key } ) => { refs[ key ] = createRef(); } ); return refs; }; const CartLineItemsTable = ( { lineItems = [], isLoading = false, className, }: CartLineItemsTableProps ): JSX.Element => { const tableRef = useRef< HTMLTableElement | null >( null ); const rowRefs = useRef( setRefs( lineItems ) ); useEffect( () => { rowRefs.current = setRefs( lineItems ); }, [ lineItems ] ); const onRemoveRow = ( nextItemKey: string | null ) => () => { if ( rowRefs?.current && nextItemKey && rowRefs.current[ nextItemKey ].current instanceof HTMLElement ) { ( rowRefs.current[ nextItemKey ].current as HTMLElement ).focus(); } else if ( tableRef.current instanceof HTMLElement ) { tableRef.current.focus(); } }; const products = isLoading ? placeholderRows : lineItems.map( ( lineItem, i ) => { const nextItemKey = lineItems.length > i + 1 ? lineItems[ i + 1 ].key : null; return ( ); } ); return ( { products }
{ __( 'Product', 'woo-gutenberg-products-block' ) } { __( 'Details', 'woo-gutenberg-products-block' ) } { __( 'Total', 'woo-gutenberg-products-block' ) }
); }; export default CartLineItemsTable;