2022-11-15 17:49:02 +01:00
import React from 'react' ;
import PropTypes from 'prop-types' ;
import classnames from 'classnames' ;
2023-04-12 17:55:24 +02:00
import { ButtonIcon , ButtonIconSize , Icon , IconName , IconSize } from '..' ;
2023-02-16 00:43:51 +01:00
import { TextField , TEXT _FIELD _TYPES } from '../text-field' ;
2023-05-16 22:15:13 +02:00
import { useI18nContext } from '../../../hooks/useI18nContext' ;
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
2023-05-16 22:15:13 +02:00
} ) => {
const t = useI18nContext ( ) ;
return (
< TextField
className = { classnames ( 'mm-text-field-search' , className ) }
value = { value }
onChange = { onChange }
type = { TEXT _FIELD _TYPES . SEARCH }
endAccessory = {
value && showClearButton ? (
< >
< ButtonIcon
className = "mm-text-field__button-clear"
ariaLabel = { t ( 'clear' ) }
iconName = { IconName . Close }
size = { ButtonIconSize . Sm }
onClick = { clearButtonOnClick }
{ ... clearButtonProps }
/ >
{ endAccessory }
< / >
) : (
endAccessory
)
}
startAccessory = { < Icon name = { IconName . Search } size = { IconSize . Sm } / > }
inputProps = { {
marginRight : showClearButton ? 6 : 0 ,
... inputProps ,
} }
{ ... props }
/ >
) ;
} ;
2022-11-15 17:49:02 +01:00
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
* /
2023-07-14 19:59:30 +02:00
clearButtonProps : PropTypes . shape ( ButtonIcon . propTypes ) ,
2022-11-15 17:49:02 +01:00
/ * *
* 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' ;