Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Remove useSelectShippingRate hook and adjust how local state works …
Browse files Browse the repository at this point in the history
…for `PackageRates` (#5802)

* Convert radio component to TS and support uncontrolled components

* Further radio control to typescript changes

* Combine useSelectShippingRate and useSelectShippingRates

* Remove useSelectShippingRate hook

* Move local radio checked state to PackageRates

* This is a Controlled component - update inline docs

* useSelectShippingRates -> useSelectShippingRate rename
  • Loading branch information
mikejolley authored Feb 10, 2022
1 parent bc4c471 commit 5545c38
Show file tree
Hide file tree
Showing 11 changed files with 182 additions and 186 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,15 @@ interface PackageProps {

export const ShippingRatesControlPackage = ( {
packageId,
className,
className = '',
noResultsMessage,
renderOption,
packageData,
collapsible = false,
collapse = false,
showItems = false,
}: PackageProps ): ReactElement => {
const { selectShippingRate, selectedShippingRate } = useSelectShippingRate(
packageId,
packageData.shipping_rates
);
const { selectShippingRate } = useSelectShippingRate();

const header = (
<>
Expand Down Expand Up @@ -117,8 +114,12 @@ export const ShippingRatesControlPackage = ( {
className={ className }
noResultsMessage={ noResultsMessage }
rates={ packageData.shipping_rates }
onSelectRate={ selectShippingRate }
selected={ selectedShippingRate }
onSelectRate={ ( newShippingRateId ) =>
selectShippingRate( newShippingRateId, packageId )
}
selectedRate={ packageData.shipping_rates.find(
( rate ) => rate.selected
) }
renderOption={ renderOption }
/>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/**
* External dependencies
*/
import { useState, useEffect } from '@wordpress/element';
import RadioControl, {
RadioControlOptionLayout,
} from '@woocommerce/base-components/radio-control';
import type { PackageRateOption } from '@woocommerce/type-defs/shipping';
import type { ReactElement } from 'react';
import type { CartShippingPackageShippingRate } from '@woocommerce/type-defs/cart';

/**
Expand All @@ -20,18 +20,30 @@ interface PackageRates {
option: CartShippingPackageShippingRate
) => PackageRateOption;
className?: string;
noResultsMessage: ReactElement;
selected?: string;
noResultsMessage: JSX.Element;
selectedRate: CartShippingPackageShippingRate | undefined;
}

const PackageRates = ( {
className,
className = '',
noResultsMessage,
onSelectRate,
rates,
renderOption = renderPackageRateOption,
selected,
}: PackageRates ): ReactElement => {
selectedRate,
}: PackageRates ): JSX.Element => {
const selectedRateId = selectedRate?.rate_id || '';

// Store selected rate ID in local state so shipping rates changes are shown in the UI instantly.
const [ selectedOption, setSelectedOption ] = useState( selectedRateId );

// Update the selected option if cart state changes in the data stores.
useEffect( () => {
if ( selectedRateId ) {
setSelectedOption( selectedRateId );
}
}, [ selectedRateId ] );

if ( rates.length === 0 ) {
return noResultsMessage;
}
Expand All @@ -40,10 +52,11 @@ const PackageRates = ( {
return (
<RadioControl
className={ className }
onChange={ ( selectedRateId: string ) => {
onSelectRate( selectedRateId );
onChange={ ( value: string ) => {
setSelectedOption( value );
onSelectRate( value );
} }
selected={ selected }
selected={ selectedOption }
options={ rates.map( renderOption ) }
/>
);
Expand Down
52 changes: 0 additions & 52 deletions assets/js/base/components/radio-control/index.js

This file was deleted.

55 changes: 55 additions & 0 deletions assets/js/base/components/radio-control/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* External dependencies
*/
import classnames from 'classnames';
import { useInstanceId } from '@wordpress/compose';

/**
* Internal dependencies
*/
import RadioControlOption from './option';
import type { RadioControlProps } from './types';
import './style.scss';

const RadioControl = ( {
className = '',
id,
selected,
onChange = () => void 0,
options = [],
}: RadioControlProps ): JSX.Element | null => {
const instanceId = useInstanceId( RadioControl );
const radioControlId = id || instanceId;

if ( ! options.length ) {
return null;
}

return (
<div
className={ classnames(
'wc-block-components-radio-control',
className
) }
>
{ options.map( ( option ) => (
<RadioControlOption
key={ `${ radioControlId }-${ option.value }` }
name={ `radio-control-${ radioControlId }` }
checked={ option.value === selected }
option={ option }
onChange={ ( value: string ) => {
onChange( value );
if ( typeof option.onChange === 'function' ) {
option.onChange( value );
}
} }
/>
) ) }
</div>
);
};

export default RadioControl;
export { default as RadioControlOption } from './option';
export { default as RadioControlOptionLayout } from './option-layout';
7 changes: 3 additions & 4 deletions assets/js/base/components/radio-control/option-layout.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
/**
* External dependencies
* Internal dependencies
*/
import type { ReactElement } from 'react';
import type { PackageRateOption } from '@woocommerce/type-defs/shipping';
import type { RadioControlOptionLayout } from './types';

const OptionLayout = ( {
label,
secondaryLabel,
description,
secondaryDescription,
id,
}: Partial< PackageRateOption > ): ReactElement => {
}: RadioControlOptionLayout ): JSX.Element => {
return (
<div className="wc-block-components-radio-control__option-layout">
<div className="wc-block-components-radio-control__label-group">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,23 @@ import classnames from 'classnames';
* Internal dependencies
*/
import OptionLayout from './option-layout';
import type { RadioControlOptionProps } from './types';

const Option = ( { checked, name, onChange, option } ) => {
const Option = ( {
checked,
name,
onChange,
option,
}: RadioControlOptionProps ): JSX.Element => {
const {
value,
label,
description,
secondaryLabel,
secondaryDescription,
} = option;
const onChangeValue = ( event ) => onChange( event.target.value );
const onChangeValue = ( event: React.ChangeEvent< HTMLInputElement > ) =>
onChange( event.target.value );

return (
<label
Expand Down
40 changes: 40 additions & 0 deletions assets/js/base/components/radio-control/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* External dependencies
*/
import type { ReactElement } from 'react';

export interface RadioControlProps {
// Class name for control.
className?: string;
// ID for the control.
id?: string;
// The selected option. This is a controlled component.
selected: string;
// Fired when an option is changed.
onChange: ( value: string ) => void;
// List of radio control options.
options: RadioControlOption[];
}

export interface RadioControlOptionProps {
checked: boolean;
name?: string;
onChange: ( value: string ) => void;
option: RadioControlOption;
}

interface RadioControlOptionContent {
label: string;
description?: string | ReactElement | undefined;
secondaryLabel?: string | ReactElement | undefined;
secondaryDescription?: string | undefined;
}

export interface RadioControlOption extends RadioControlOptionContent {
value: string;
onChange?: ( value: string ) => void;
}

export interface RadioControlOptionLayout extends RadioControlOptionContent {
id?: string;
}
1 change: 0 additions & 1 deletion assets/js/base/context/hooks/shipping/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './use-select-shipping-rate';
export * from './use-select-shipping-rates';
Loading

0 comments on commit 5545c38

Please sign in to comment.