2019-11-11 10:32:56 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2019-12-19 09:22:53 +00:00
|
|
|
import { __, _n, sprintf } from '@wordpress/i18n';
|
2024-08-12 16:33:17 +00:00
|
|
|
import { useMemo, useState } from '@wordpress/element';
|
2024-05-31 03:49:36 +00:00
|
|
|
import clsx from 'clsx';
|
2019-11-11 10:32:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2024-08-12 16:33:17 +00:00
|
|
|
import { CheckboxListOptionControl } from './checkbox-list-option-control';
|
|
|
|
import type { CheckboxListOptions } from './types';
|
2019-11-11 10:32:56 +00:00
|
|
|
import './style.scss';
|
2021-12-03 17:23:25 +00:00
|
|
|
|
2023-11-08 10:55:48 +00:00
|
|
|
export interface CheckboxListProps {
|
2023-10-17 09:47:34 +00:00
|
|
|
className?: string | undefined;
|
|
|
|
isLoading?: boolean | undefined;
|
|
|
|
isDisabled?: boolean | undefined;
|
|
|
|
limit?: number | undefined;
|
|
|
|
checked?: string[] | undefined;
|
|
|
|
onChange: ( value: string ) => void | undefined;
|
|
|
|
options?: CheckboxListOptions[] | undefined;
|
2021-12-03 17:23:25 +00:00
|
|
|
}
|
|
|
|
|
2019-11-11 10:32:56 +00:00
|
|
|
/**
|
|
|
|
* Component used to show a list of checkboxes in a group.
|
2020-09-20 23:54:08 +00:00
|
|
|
*
|
2022-04-08 13:47:19 +00:00
|
|
|
* @param {Object} props Incoming props for the component.
|
|
|
|
* @param {string} props.className CSS class used.
|
|
|
|
* @param {function(string):any} props.onChange Function called when inputs change.
|
|
|
|
* @param {Array} props.options Options for list.
|
|
|
|
* @param {Array} props.checked Which items are checked.
|
|
|
|
* @param {boolean} props.isLoading If loading or not.
|
|
|
|
* @param {boolean} props.isDisabled If inputs are disabled or not.
|
|
|
|
* @param {number} props.limit Whether to limit the number of inputs showing.
|
2019-11-11 10:32:56 +00:00
|
|
|
*/
|
|
|
|
const CheckboxList = ( {
|
|
|
|
className,
|
2022-07-01 17:50:44 +00:00
|
|
|
onChange,
|
2019-11-11 10:32:56 +00:00
|
|
|
options = [],
|
2019-11-15 14:41:23 +00:00
|
|
|
checked = [],
|
2019-11-11 10:32:56 +00:00
|
|
|
isLoading = false,
|
2019-11-15 14:41:23 +00:00
|
|
|
isDisabled = false,
|
2019-11-11 10:32:56 +00:00
|
|
|
limit = 10,
|
2021-12-03 17:23:25 +00:00
|
|
|
}: CheckboxListProps ): JSX.Element => {
|
2019-11-11 10:32:56 +00:00
|
|
|
const [ showExpanded, setShowExpanded ] = useState( false );
|
|
|
|
|
|
|
|
const placeholder = useMemo( () => {
|
|
|
|
return [ ...Array( 5 ) ].map( ( x, i ) => (
|
|
|
|
<li
|
|
|
|
key={ i }
|
|
|
|
style={ {
|
|
|
|
/* stylelint-disable */
|
|
|
|
width: Math.floor( Math.random() * 75 ) + 25 + '%',
|
|
|
|
} }
|
2023-11-08 10:55:48 +00:00
|
|
|
>
|
|
|
|
{ /* The is required to give the placeholder content and therefore height, without it the placeholder rows do not render */ }
|
|
|
|
|
|
|
|
</li>
|
2019-11-11 10:32:56 +00:00
|
|
|
) );
|
|
|
|
}, [] );
|
|
|
|
|
|
|
|
const renderedShowMore = useMemo( () => {
|
|
|
|
const optionCount = options.length;
|
2019-12-19 09:22:53 +00:00
|
|
|
const remainingOptionsCount = optionCount - limit;
|
2019-11-11 10:32:56 +00:00
|
|
|
return (
|
|
|
|
! showExpanded && (
|
|
|
|
<li key="show-more" className="show-more">
|
|
|
|
<button
|
|
|
|
onClick={ () => {
|
|
|
|
setShowExpanded( true );
|
|
|
|
} }
|
|
|
|
aria-expanded={ false }
|
|
|
|
aria-label={ sprintf(
|
2021-02-19 11:58:44 +00:00
|
|
|
/* translators: %s is referring the remaining count of options */
|
2019-12-19 09:22:53 +00:00
|
|
|
_n(
|
|
|
|
'Show %s more option',
|
2019-11-11 10:32:56 +00:00
|
|
|
'Show %s more options',
|
2019-12-19 09:22:53 +00:00
|
|
|
remainingOptionsCount,
|
2023-12-12 23:05:20 +00:00
|
|
|
'woocommerce'
|
2019-11-11 10:32:56 +00:00
|
|
|
),
|
2019-12-19 09:22:53 +00:00
|
|
|
remainingOptionsCount
|
2019-11-11 10:32:56 +00:00
|
|
|
) }
|
|
|
|
>
|
2019-12-19 09:22:53 +00:00
|
|
|
{ sprintf(
|
2021-02-19 11:58:44 +00:00
|
|
|
/* translators: %s number of options to reveal. */
|
2019-12-19 09:22:53 +00:00
|
|
|
_n(
|
2019-11-11 10:32:56 +00:00
|
|
|
'Show %s more',
|
2019-12-19 09:22:53 +00:00
|
|
|
'Show %s more',
|
|
|
|
remainingOptionsCount,
|
2023-12-12 23:05:20 +00:00
|
|
|
'woocommerce'
|
2019-11-11 10:32:56 +00:00
|
|
|
),
|
2019-12-19 09:22:53 +00:00
|
|
|
remainingOptionsCount
|
2019-11-11 10:32:56 +00:00
|
|
|
) }
|
|
|
|
</button>
|
|
|
|
</li>
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}, [ options, limit, showExpanded ] );
|
|
|
|
|
|
|
|
const renderedShowLess = useMemo( () => {
|
|
|
|
return (
|
|
|
|
showExpanded && (
|
|
|
|
<li key="show-less" className="show-less">
|
|
|
|
<button
|
|
|
|
onClick={ () => {
|
|
|
|
setShowExpanded( false );
|
|
|
|
} }
|
|
|
|
aria-expanded={ true }
|
2023-12-12 23:05:20 +00:00
|
|
|
aria-label={ __( 'Show less options', 'woocommerce' ) }
|
2019-11-11 10:32:56 +00:00
|
|
|
>
|
2023-12-12 23:05:20 +00:00
|
|
|
{ __( 'Show less', 'woocommerce' ) }
|
2019-11-11 10:32:56 +00:00
|
|
|
</button>
|
|
|
|
</li>
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}, [ showExpanded ] );
|
|
|
|
|
|
|
|
const renderedOptions = useMemo( () => {
|
|
|
|
// Truncate options if > the limit + 5.
|
|
|
|
const optionCount = options.length;
|
|
|
|
const shouldTruncateOptions = optionCount > limit + 5;
|
|
|
|
return (
|
2020-12-14 11:54:34 +00:00
|
|
|
<>
|
2019-11-11 10:32:56 +00:00
|
|
|
{ options.map( ( option, index ) => (
|
2024-08-12 16:33:17 +00:00
|
|
|
<CheckboxListOptionControl
|
|
|
|
key={ option.value }
|
|
|
|
option={ option }
|
|
|
|
shouldTruncateOptions={ shouldTruncateOptions }
|
|
|
|
showExpanded={ showExpanded }
|
|
|
|
index={ index }
|
|
|
|
limit={ limit }
|
|
|
|
checked={ checked.includes( option.value ) }
|
|
|
|
disabled={ isDisabled }
|
|
|
|
renderedShowMore={ renderedShowMore }
|
|
|
|
onChange={ onChange }
|
|
|
|
/>
|
2019-11-11 10:32:56 +00:00
|
|
|
) ) }
|
|
|
|
{ shouldTruncateOptions && renderedShowLess }
|
2020-12-14 11:54:34 +00:00
|
|
|
</>
|
2019-11-11 10:32:56 +00:00
|
|
|
);
|
|
|
|
}, [
|
|
|
|
options,
|
2020-10-27 15:39:08 +00:00
|
|
|
onChange,
|
2019-11-11 10:32:56 +00:00
|
|
|
checked,
|
|
|
|
showExpanded,
|
|
|
|
limit,
|
|
|
|
renderedShowLess,
|
|
|
|
renderedShowMore,
|
2019-11-15 14:41:23 +00:00
|
|
|
isDisabled,
|
2019-11-11 10:32:56 +00:00
|
|
|
] );
|
|
|
|
|
2024-05-31 03:49:36 +00:00
|
|
|
const classes = clsx(
|
2019-11-11 10:32:56 +00:00
|
|
|
'wc-block-checkbox-list',
|
2020-06-17 09:53:42 +00:00
|
|
|
'wc-block-components-checkbox-list',
|
2019-11-11 10:32:56 +00:00
|
|
|
{
|
|
|
|
'is-loading': isLoading,
|
|
|
|
},
|
|
|
|
className
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ul className={ classes }>
|
|
|
|
{ isLoading ? placeholder : renderedOptions }
|
|
|
|
</ul>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default CheckboxList;
|