2020-02-14 03:43:13 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import RadioControl, {
|
|
|
|
RadioControlOptionLayout,
|
|
|
|
} from '@woocommerce/base-components/radio-control';
|
2021-03-09 10:55:24 +00:00
|
|
|
import type { Rate, PackageRateOption } from '@woocommerce/type-defs/shipping';
|
|
|
|
import type { ReactElement } from 'react';
|
2020-02-14 03:43:13 +00:00
|
|
|
|
2021-01-28 14:24:01 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { renderPackageRateOption } from './render-package-rate-option';
|
|
|
|
|
2021-03-09 10:55:24 +00:00
|
|
|
interface PackageRates {
|
|
|
|
onSelectRate: ( selectedRateId: string ) => void;
|
|
|
|
rates: Rate[];
|
|
|
|
renderOption?: ( option: Rate ) => PackageRateOption;
|
|
|
|
className?: string;
|
|
|
|
noResultsMessage: ReactElement;
|
|
|
|
selected?: string;
|
|
|
|
}
|
|
|
|
|
2021-01-28 14:24:01 +00:00
|
|
|
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-03-09 10:55:24 +00:00
|
|
|
}: PackageRates ): ReactElement => {
|
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-03-09 10:55:24 +00:00
|
|
|
onChange={ ( selectedRateId: string ) => {
|
2021-01-28 14:24:01 +00:00
|
|
|
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
|
|
|
export default PackageRates;
|