2020-02-14 03:43:13 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import RadioControl, {
|
|
|
|
RadioControlOptionLayout,
|
|
|
|
} from '@woocommerce/base-components/radio-control';
|
|
|
|
|
2021-01-28 14:24:01 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { renderPackageRateOption } from './render-package-rate-option';
|
|
|
|
|
|
|
|
const PackageRates = ( {
|
2020-02-14 03:43:13 +00:00
|
|
|
className,
|
|
|
|
noResultsMessage,
|
2021-01-28 14:24:01 +00:00
|
|
|
onSelectRate,
|
|
|
|
rates,
|
|
|
|
renderOption = renderPackageRateOption,
|
2020-02-14 03:43:13 +00:00
|
|
|
selected,
|
|
|
|
} ) => {
|
2021-01-28 14:24:01 +00:00
|
|
|
if ( rates.length === 0 ) {
|
2020-11-17 11:58:38 +00:00
|
|
|
return noResultsMessage;
|
2020-02-14 03:43:13 +00:00
|
|
|
}
|
|
|
|
|
2021-01-28 14:24:01 +00:00
|
|
|
if ( rates.length > 1 ) {
|
2020-02-14 03:43:13 +00:00
|
|
|
return (
|
|
|
|
<RadioControl
|
|
|
|
className={ className }
|
2021-01-28 14:24:01 +00:00
|
|
|
onChange={ ( selectedRateId ) => {
|
|
|
|
onSelectRate( selectedRateId );
|
|
|
|
} }
|
2020-02-14 03:43:13 +00:00
|
|
|
selected={ selected }
|
2021-01-28 14:24:01 +00:00
|
|
|
options={ rates.map( renderOption ) }
|
2020-02-14 03:43:13 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const {
|
|
|
|
label,
|
|
|
|
secondaryLabel,
|
|
|
|
description,
|
|
|
|
secondaryDescription,
|
2021-01-28 14:24:01 +00:00
|
|
|
} = renderOption( rates[ 0 ] );
|
2020-02-14 03:43:13 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<RadioControlOptionLayout
|
|
|
|
label={ label }
|
|
|
|
secondaryLabel={ secondaryLabel }
|
|
|
|
description={ description }
|
|
|
|
secondaryDescription={ secondaryDescription }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-01-28 14:24:01 +00:00
|
|
|
PackageRates.propTypes = {
|
|
|
|
onSelectRate: PropTypes.func.isRequired,
|
|
|
|
rates: PropTypes.arrayOf( PropTypes.object ).isRequired,
|
|
|
|
renderOption: PropTypes.func,
|
2020-02-14 03:43:13 +00:00
|
|
|
className: PropTypes.string,
|
2020-11-17 11:58:38 +00:00
|
|
|
noResultsMessage: PropTypes.node,
|
2020-02-14 03:43:13 +00:00
|
|
|
selected: PropTypes.string,
|
|
|
|
};
|
|
|
|
|
2021-01-28 14:24:01 +00:00
|
|
|
export default PackageRates;
|