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.
Convert radio-control-accordion component to TypeScript (#7849)
- Loading branch information
Tarun Vijwani
authored
Dec 6, 2022
1 parent
39da1b1
commit fc47e16
Showing
3 changed files
with
89 additions
and
75 deletions.
There are no files selected for viewing
74 changes: 0 additions & 74 deletions
74
assets/js/base/components/radio-control-accordion/index.js
This file was deleted.
Oops, something went wrong.
88 changes: 88 additions & 0 deletions
88
assets/js/base/components/radio-control-accordion/index.tsx
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,88 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import classnames from 'classnames'; | ||
import { withInstanceId } from '@wordpress/compose'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import RadioControlOption from '../radio-control/option'; | ||
|
||
interface RadioControlAccordionProps { | ||
className?: string; | ||
instanceId: number; | ||
id: string; | ||
onChange: ( value: string ) => void; | ||
options: Array< { | ||
value: string; | ||
label: string | JSX.Element; | ||
onChange?: ( value: string ) => void; | ||
name: string; | ||
content: JSX.Element; | ||
} >; | ||
selected: string | null; | ||
} | ||
|
||
const RadioControlAccordion = ( { | ||
className, | ||
instanceId, | ||
id, | ||
selected, | ||
onChange, | ||
options = [], | ||
}: RadioControlAccordionProps ): JSX.Element | null => { | ||
const radioControlId = id || instanceId; | ||
|
||
if ( ! options.length ) { | ||
return null; | ||
} | ||
return ( | ||
<div | ||
className={ classnames( | ||
'wc-block-components-radio-control', | ||
className | ||
) } | ||
> | ||
{ options.map( ( option ) => { | ||
const hasOptionContent = | ||
typeof option === 'object' && 'content' in option; | ||
const checked = option.value === selected; | ||
return ( | ||
<div | ||
className="wc-block-components-radio-control-accordion-option" | ||
key={ option.value } | ||
> | ||
<RadioControlOption | ||
name={ `radio-control-${ radioControlId }` } | ||
checked={ checked } | ||
option={ option } | ||
onChange={ ( value ) => { | ||
onChange( value ); | ||
if ( typeof option.onChange === 'function' ) { | ||
option.onChange( value ); | ||
} | ||
} } | ||
/> | ||
{ hasOptionContent && checked && ( | ||
<div | ||
className={ classnames( | ||
'wc-block-components-radio-control-accordion-content', | ||
{ | ||
'wc-block-components-radio-control-accordion-content-hide': | ||
! checked, | ||
} | ||
) } | ||
> | ||
{ option.content } | ||
</div> | ||
) } | ||
</div> | ||
); | ||
} ) } | ||
</div> | ||
); | ||
}; | ||
|
||
export default withInstanceId( RadioControlAccordion ); | ||
export { RadioControlAccordion }; |
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