2019-08-23 16:35:34 +00:00
/ * *
* External dependencies
* /
import classNames from 'classnames' ;
2021-08-05 09:26:00 +00:00
import { withInstanceId } from '@wordpress/compose' ;
2023-03-02 14:26:00 +00:00
import type { ChangeEventHandler } from 'react' ;
2019-08-23 16:35:34 +00:00
/ * *
* Internal dependencies
* /
import './style.scss' ;
2023-10-26 11:55:34 +00:00
import Label from '../label' ; // Imported like this because importing from the components package loads the data stores unnecessarily - not a problem in the front end but would require a lot of unit test rewrites to prevent breaking tests due to incorrect mocks.
2019-08-23 16:35:34 +00:00
2023-11-08 11:13:49 +00:00
export interface SortSelectProps {
2022-02-07 09:34:30 +00:00
/ * *
* Unique id for component instance .
* /
instanceId : number ;
/ * *
* CSS class used .
* /
className? : string ;
/ * *
* Label for the select .
* /
label? : string ;
/ * *
* Function to call on the change event .
* /
2023-11-08 11:13:49 +00:00
onChange : ChangeEventHandler < HTMLSelectElement > ;
2022-02-07 09:34:30 +00:00
/ * *
* Option values for the select .
* /
options : {
key : string ;
label : string ;
} [ ] ;
/ * *
* Screen reader label .
* /
screenReaderLabel : string ;
/ * *
* The selected value .
* /
2022-07-01 17:50:44 +00:00
value? : string ;
2022-12-21 13:09:48 +00:00
/ * *
* Whether the select is read only .
* /
readOnly? : boolean ;
2022-02-07 09:34:30 +00:00
}
2019-08-23 16:35:34 +00:00
/ * *
* Component used for 'Order by' selectors , which renders a label
* and a < select > with the options provided in the props .
* /
2023-11-08 11:13:49 +00:00
export const SortSelect = ( {
2019-09-05 15:09:31 +00:00
className ,
2020-03-10 14:40:30 +00:00
instanceId ,
2022-02-07 09:34:30 +00:00
label = '' ,
2019-09-05 15:09:31 +00:00
onChange ,
options ,
screenReaderLabel ,
2022-07-01 17:50:44 +00:00
value = '' ,
2023-11-08 11:13:49 +00:00
readOnly = false ,
2022-02-07 09:34:30 +00:00
} : SortSelectProps ) : JSX . Element = > {
2020-06-11 17:38:31 +00:00
const selectId = ` wc-block-components-sort-select__select- ${ instanceId } ` ;
2019-08-23 16:35:34 +00:00
return (
2020-06-11 17:38:31 +00:00
< div
className = { classNames (
'wc-block-sort-select' ,
'wc-block-components-sort-select' ,
className
) }
>
2019-08-23 16:35:34 +00:00
< Label
label = { label }
screenReaderLabel = { screenReaderLabel }
wrapperElement = "label"
wrapperProps = { {
2020-06-11 17:38:31 +00:00
className :
'wc-block-sort-select__label wc-block-components-sort-select__label' ,
2019-08-23 16:35:34 +00:00
htmlFor : selectId ,
} }
/ >
2023-11-08 11:13:49 +00:00
< select
disabled = { ! ! readOnly }
2019-08-23 16:35:34 +00:00
id = { selectId }
2020-06-11 17:38:31 +00:00
className = "wc-block-sort-select__select wc-block-components-sort-select__select"
2019-08-23 16:35:34 +00:00
onChange = { onChange }
value = { value }
>
2022-02-07 09:34:30 +00:00
{ options &&
options . map ( ( option ) = > (
< option key = { option . key } value = { option . key } >
{ option . label }
< / option >
) ) }
2019-08-23 16:35:34 +00:00
< / select >
2019-10-28 13:53:09 +00:00
< / div >
2019-08-23 16:35:34 +00:00
) ;
} ;
2020-03-10 14:40:30 +00:00
export default withInstanceId ( SortSelect ) ;