This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove
useSelectShippingRate
hook and adjust how local state works …
…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
1 parent
bc4c471
commit 5545c38
Showing
11 changed files
with
182 additions
and
186 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
Oops, something went wrong.