2022-11-15 17:49:02 +01:00
import React from 'react' ;
import PropTypes from 'prop-types' ;
import classnames from 'classnames' ;
2023-02-02 21:15:26 +01:00
import { Size } from '../../../helpers/constants/design-system' ;
2022-11-15 17:49:02 +01:00
2023-04-05 18:11:10 +02:00
import { ButtonIcon , Icon , IconName , IconSize } from '..' ;
2023-02-16 00:43:51 +01:00
import { TextField , TEXT _FIELD _TYPES } from '../text-field' ;
2022-11-15 17:49:02 +01:00
export const TextFieldSearch = ( {
2023-02-16 00:43:51 +01:00
className ,
showClearButton = true , // only works with a controlled input
2022-11-15 17:49:02 +01:00
clearButtonOnClick ,
clearButtonProps ,
2023-02-16 00:43:51 +01:00
endAccessory ,
inputProps ,
value ,
onChange ,
2022-11-15 17:49:02 +01:00
... props
} ) => (
< TextField
className = { classnames ( 'mm-text-field-search' , className ) }
value = { value }
onChange = { onChange }
2023-02-16 00:43:51 +01:00
type = { TEXT _FIELD _TYPES . SEARCH }
endAccessory = {
value && showClearButton ? (
< >
< ButtonIcon
className = "mm-text-field__button-clear"
ariaLabel = "Clear" // TODO: i18n
2023-04-05 18:11:10 +02:00
iconName = { IconName . Close }
2023-02-16 00:43:51 +01:00
size = { Size . SM }
onClick = { clearButtonOnClick }
{ ... clearButtonProps }
/ >
{ endAccessory }
< / >
) : (
endAccessory
)
}
2023-04-05 18:11:10 +02:00
startAccessory = { < Icon name = { IconName . Search } size = { IconSize . Sm } / > }
2023-02-16 00:43:51 +01:00
inputProps = { {
marginRight : showClearButton ? 6 : 0 ,
... inputProps ,
} }
2022-11-15 17:49:02 +01:00
{ ... props }
/ >
) ;
TextFieldSearch . propTypes = {
/ * *
* The value of the TextFieldSearch
* /
2023-02-16 00:43:51 +01:00
value : TextField . propTypes . value ,
2022-11-15 17:49:02 +01:00
/ * *
* The onChange handler of the TextFieldSearch
* /
2023-02-16 00:43:51 +01:00
onChange : TextField . propTypes . onChange ,
/ * *
* The clear button for the TextFieldSearch .
* Defaults to true
* /
showClearButton : PropTypes . bool ,
2022-11-15 17:49:02 +01:00
/ * *
* The onClick handler for the clear button
2022-12-01 22:48:53 +01:00
* Required unless showClearButton is false
*
* @ param { object } props - The props passed to the component .
* @ param { string } propName - The prop name in this case 'id' .
* @ param { string } componentName - The name of the component .
2022-11-15 17:49:02 +01:00
* /
2022-12-01 22:48:53 +01:00
clearButtonOnClick : ( props , propName , componentName ) => {
if (
props . showClearButton &&
( ! props [ propName ] || ! props . clearButtonProps ? . onClick )
) {
return new Error (
` ${ propName } is required unless showClearButton is false. Warning coming from ${ componentName } ui/components/component-library/text-field-search/text-field-search.js ` ,
) ;
}
return null ;
} ,
2022-11-15 17:49:02 +01:00
/ * *
* The props to pass to the clear button
* /
clearButtonProps : PropTypes . shape ( ButtonIcon . PropTypes ) ,
/ * *
* An additional className to apply to the TextFieldSearch
* /
className : PropTypes . string ,
2023-02-16 00:43:51 +01:00
/ * *
* Component to appear on the right side of the input
* /
endAccessory : PropTypes . node ,
/ * *
* Attributes applied to the ` input ` element .
* /
inputProps : PropTypes . object ,
/ * *
* FormTextField accepts all the props from TextField and Box
* /
... TextField . propTypes ,
2022-11-15 17:49:02 +01:00
} ;
TextFieldSearch . displayName = 'TextFieldSearch' ;