From 097ff875ef05b00960a73574ab1a99f2d62f53f4 Mon Sep 17 00:00:00 2001 From: JF-Cozy Date: Thu, 3 Oct 2024 16:51:50 +0200 Subject: [PATCH] feat(SearchBar): Add `auto` size and possibility to control it in the app --- react/SearchBar/Readme.md | 39 ++++++++++++++++++++++++ react/SearchBar/index.jsx | 53 ++++++++++++++++++++++----------- react/SearchBar/locales/en.json | 2 +- react/SearchBar/locales/fr.json | 2 +- 4 files changed, 77 insertions(+), 19 deletions(-) diff --git a/react/SearchBar/Readme.md b/react/SearchBar/Readme.md index b9e3c3c801..df1e049e9f 100644 --- a/react/SearchBar/Readme.md +++ b/react/SearchBar/Readme.md @@ -4,8 +4,14 @@ import Variants from 'cozy-ui/docs/components/Variants' import SearchBar from 'cozy-ui/transpiled/react/SearchBar' import Typography from 'cozy-ui/transpiled/react/Typography' import CloudIcon from 'cozy-ui/transpiled/react/Icons/Cloud' +import FormControlLabel from 'cozy-ui/transpiled/react/FormControlLabel' +import RadioGroup from 'cozy-ui/transpiled/react/RadioGroup' +import Radio from 'cozy-ui/transpiled/react/Radios' +import FormControl from 'cozy-ui/transpiled/react/FormControl' +import FormLabel from 'cozy-ui/transpiled/react/FormLabel' const initialVariants = [{ elevation: true, button: false, customIcon: false, disabledClear: false }] +initialState = { size: 'small' } ; @@ -13,9 +19,41 @@ const initialVariants = [{ elevation: true, button: false, customIcon: false, di {variant => ( <> + + Size + { setState({ size: event.target.value }) }} + > + } + /> + } + /> + } + /> + } + /> + + Normal ({ @@ -30,7 +38,7 @@ const useStyles = makeStyles(theme => ({ alignItems: 'center', height: ({ size }) => sizeToPixel[size], flex: 1, - borderRadius: 99, + borderRadius: ({ size }) => radiusBySize[size], borderStyle: 'solid', borderWidth: 1, borderColor: 'transparent', @@ -110,6 +118,7 @@ const SearchBar = forwardRef( disabledClear, className, defaultValue, + value, elevation, disabled, onChange, @@ -124,20 +133,21 @@ const SearchBar = forwardRef( const [currentValue, setCurrentValue] = useState(defaultValue) const [isFocused, setIsFocused] = useState(false) - const placeholder = placeholderProp || t('search.placeholder') - const label = labelProp || t('search.placeholder') + const placeholder = placeholderProp || t('SearchBar.placeholder') + const label = labelProp || t('SearchBar.placeholder') + const spreadValue = value || currentValue + const isSelfControlledComp = typeof value === 'undefined' - const delayedOnChange = useMemo( - () => debounce(event => onChange(event), 375), - [onChange] - ) + const delayedOnChange = useMemo(() => debounce(onChange, 375), [onChange]) const handleChange = ev => { - const value = ev.target.value + if (!isSelfControlledComp) return onChange(ev) + + const _value = ev.target.value - if (value.length >= 1) { + if (_value.length >= 1) { delayedOnChange(ev) - setCurrentValue(value) + setCurrentValue(_value) } else { handleClear(ev) } @@ -187,7 +197,7 @@ const SearchBar = forwardRef( {...componentsProps?.inputBase} className={classes.inputBase} placeholder={disabled ? null : placeholder} - value={disabled ? placeholder : currentValue} + value={disabled ? placeholder : spreadValue} disabled={disabled} aria-label={placeholder} onChange={handleChange} @@ -196,7 +206,7 @@ const SearchBar = forwardRef( /> )} - {currentValue && !disabledClear && ( + {spreadValue && !disabledClear && ( @@ -230,11 +240,14 @@ SearchBar.propTypes = { className: PropTypes.string, type: PropTypes.oneOf(['button', 'search']), icon: iconPropType, - size: PropTypes.oneOf(['small', 'medium', 'large']), + size: PropTypes.oneOf(['small', 'medium', 'large', 'auto']), componentsProps: PropTypes.shape({ /** Props spread to InputBase component */ inputBase: PropTypes.object }), + /** Used to control the component outside of it */ + value: PropTypes.string, + /** Used only with self-controlled component */ defaultValue: PropTypes.string, disabledClear: PropTypes.bool, elevation: PropTypes.bool, @@ -250,4 +263,10 @@ SearchBar.propTypes = { onBlur: PropTypes.func } -export default withOnlyLocales(SearchBar) +const SearchBarWithLocales = props => { + useExtendI18n(locales) + + return +} + +export default SearchBarWithLocales diff --git a/react/SearchBar/locales/en.json b/react/SearchBar/locales/en.json index d401ba754a..366af73b22 100644 --- a/react/SearchBar/locales/en.json +++ b/react/SearchBar/locales/en.json @@ -1,5 +1,5 @@ { - "search": { + "SearchBar": { "placeholder": "Search" } } diff --git a/react/SearchBar/locales/fr.json b/react/SearchBar/locales/fr.json index 11b81493c5..604fb46f22 100644 --- a/react/SearchBar/locales/fr.json +++ b/react/SearchBar/locales/fr.json @@ -1,5 +1,5 @@ { - "search": { + "SearchBar": { "placeholder": "Rechercher" } }