Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some props on BottomSheet, CozyDialogs and SearchBar #2699

Merged
merged 6 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion react/BottomSheet/BottomSheet.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ export const defaultBottomSheetSpringConfig = {
const defaultSettings = {
mediumHeightRatio: 0.75,
mediumHeight: null,
hasMinHeightOffset: false,
isOpenMin: false
}

Expand All @@ -142,7 +143,7 @@ const BottomSheet = memo(
offset,
children
}) => {
const { mediumHeightRatio, mediumHeight, isOpenMin } = {
const { mediumHeightRatio, mediumHeight, isOpenMin, hasMinHeightOffset } = {
...defaultSettings,
...settings
}
Expand Down Expand Up @@ -244,6 +245,7 @@ const BottomSheet = memo(
isClosable,
isOpenMin,
headerRef,
offset: hasMinHeightOffset ? offset : 0,
actionButtonsHeight,
actionButtonsBottomMargin
})
Expand Down Expand Up @@ -418,6 +420,8 @@ BottomSheet.propTypes = {
mediumHeight: PropTypes.number,
/** Height of the middle snap point, expressed as a percentage of the viewport height */
mediumHeightRatio: PropTypes.number,
/** To include the offset in the min height value */
hasMinHeightOffset: PropTypes.bool,
/** To open the BottomSheet at the minimum height, if have an header */
isOpenMin: PropTypes.bool
}),
Expand Down
2 changes: 2 additions & 0 deletions react/BottomSheet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ import BottomSheet, { BottomSheetItem, BottomSheetTitle } from 'cozy-ui/transp
* **settings** : `<object>` – Settings that can be modified
* **mediumHeight** : `<number>` – Height in pixel of the middle snap point
* **mediumHeightRatio** : `<number>` – Height of the middle snap point, expressed as a percentage of the viewport height
* **isOpenMin** : `<boolean>` – To open the BottomSheet at the minimum height, if have an header
* **hasMinHeightOffset** : `<boolean>` – To include the offset in the min height value
* **backdrop** : `<boolean>` – To add a backdrop
* **skipAnimation** : `<boolean>` – To remove animations
* **offset** : `<number>` – Add an offset at the bottom
Expand Down
2 changes: 2 additions & 0 deletions react/BottomSheet/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,15 @@ export const computeMinHeight = ({
isClosable,
isOpenMin,
headerRef,
offset = 0,
actionButtonsHeight,
actionButtonsBottomMargin
}) => {
if (isClosable && !isOpenMin) return 0

return (
headerRef.current.offsetHeight +
offset +
actionButtonsHeight +
actionButtonsBottomMargin +
(getFlagshipMetadata().navbarHeight || 0) +
Expand Down
12 changes: 12 additions & 0 deletions react/BottomSheet/helpers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,18 @@ describe('computeMinHeight', () => {

expect(res).toBe(85)
})

it('should return correct value with offset', () => {
const res = computeMinHeight({
isClosable: false,
headerRef: { current: { offsetHeight: 10 } },
offset: 10,
actionButtonsHeight: 20,
actionButtonsBottomMargin: 30
})

expect(res).toBe(95)
})
})

describe('setTopPosition', () => {
Expand Down
9 changes: 8 additions & 1 deletion react/CozyDialogs/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import Button from 'cozy-ui/transpiled/react/Buttons'

* **componentsProps** : `<object>` – overriden properties of specific components
* **dialogTitle** : `<object>` – DialogTitle component properties
* **size** : `<string>` – Can be "s", "m" (default) or "l"
* **size** : `<string>` – Can be "small", "medium" (default), "large" or "full"
* **open** : `<boolean>` (required) – To open/close the modal
* **disableTitleAutoPadding** : `<boolean>` (optional) – Disable title padding calculation that would prevent overlapping with close and back buttons
* if set to `true` then you should handle those CSS properties by yourself or title will take 100% of width
Expand Down Expand Up @@ -205,6 +205,7 @@ const initialVariants = [{
showActions: true,
disableGutters: false,
hideTitle: false,
fullScreen: false,
withBackground: false
}]

Expand Down Expand Up @@ -264,6 +265,11 @@ const initialVariants = [{
label="Large"
control={<Radio />}
/>
<FormControlLabel
value="full"
label="Full"
control={<Radio />}
/>
</RadioGroup>
</FormControl>
<div className="u-mt-1">
Expand All @@ -283,6 +289,7 @@ const initialVariants = [{
{state.modalOpened && (
<DialogComponent
open
{...(variant.fullScreen && { fullScreen: true })}
size={DialogComponent !== ConfirmDialog ? state.size : undefined}
onClose={variant.withCloseButton ? handleClose : undefined}
onBack={variant.withBackButton ? handleBack : undefined}
Expand Down
2 changes: 1 addition & 1 deletion react/CozyDialogs/dialogPropTypes.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import PropTypes from 'prop-types'

export default {
size: PropTypes.oneOf(['small', 'medium', 'large']),
size: PropTypes.oneOf(['small', 'medium', 'large', 'full']),
open: PropTypes.bool.isRequired,
disableTitleAutoPadding: PropTypes.bool,
background: PropTypes.string,
Expand Down
2 changes: 1 addition & 1 deletion react/CozyDialogs/useCozyDialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import useBreakpoints from '../providers/Breakpoints'
import { makeStyles } from '../styles'

let globalId = 0
const modalSizes = ['small', 'medium', 'large']
const modalSizes = ['small', 'medium', 'large', 'full']

const useStyles = makeStyles({
paper: {
Expand Down
4 changes: 4 additions & 0 deletions react/MuiCozyTheme/overrides/makeLightNormalOverrides.js
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,10 @@ export const makeLightNormalOverrides = theme => ({
maxWidth: '800px'
}
},
'&.full': {
width: '100%',
maxWidth: '100%'
},
'&.overflow': {
overflowY: 'visible !important', // Allow the icon to overflow the dialog, otherwise it will be cut off,
'& .cozyDialogContent': {
Expand Down
20 changes: 17 additions & 3 deletions react/SearchBar/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import DemoProvider from 'cozy-ui/docs/components/DemoProvider'
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'

const initialVariants = [{ elevation: true }]
const initialVariants = [{ elevation: true, button: false, customIcon: false, disabledClear: false }]

;

Expand All @@ -13,9 +14,22 @@ const initialVariants = [{ elevation: true }]
{variant => (
<>
<Typography className="u-mb-half">Normal</Typography>
<SearchBar className="u-mb-2" elevation={variant.elevation} />
<SearchBar className="u-mb-2"
elevation={variant.elevation}
disabledClear={variant.disabledClear}
type={variant.button ? "button" : "search"}
icon={variant.customIcon ? CloudIcon : undefined}
label={variant.button ? <Typography color="primary">This is a label</Typography> : undefined}
/>
<Typography className="u-mb-half">Disabled</Typography>
<SearchBar elevation={variant.elevation} disabled />
<SearchBar
disabled
elevation={variant.elevation}
disabledClear={variant.disabledClear}
type={variant.button ? "button" : "search"}
icon={variant.customIcon ? CloudIcon : undefined}
label={variant.button ? <Typography color="primary">This is a label</Typography> : undefined}
/>
</>
)}
</Variants>
Expand Down
89 changes: 74 additions & 15 deletions react/SearchBar/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,31 @@ import PropTypes from 'prop-types'
import React, { forwardRef, useState, useMemo } from 'react'

import withOnlyLocales from './locales/withOnlyLocales'
import ButtonBase from '../ButtonBase'
import Icon from '../Icon'
import { iconPropType } from '../Icon'
import IconButton from '../IconButton'
import CrossCircleIcon from '../Icons/CrossCircle'
import MagnifierIcon from '../Icons/Magnifier'
import InputBase from '../InputBase'
import Paper from '../Paper'
import Typography from '../Typography'
import { useI18n } from '../providers/I18n'
import { makeStyles } from '../styles'

const sizeToPixel = {
small: 40,
medium: 48,
large: 56
}

const useStyles = makeStyles(theme => ({
root: {
display: 'flex',
boxSizing: 'border-box',
position: 'relative',
alignItems: 'center',
height: 40,
height: ({ size }) => sizeToPixel[size],
flex: 1,
borderRadius: 99,
borderStyle: 'solid',
Expand All @@ -37,7 +46,19 @@ const useStyles = makeStyles(theme => ({
backgroundColor: theme.palette.background.contrast
},
inputBase: {
flex: 1
flex: 1,
paddingLeft: ({ icon }) => !icon && '1rem'
},
buttonBase: {
flex: 1,
justifyContent: 'start',
height: '100%',
borderRadius: 99
},
typography: {
color: 'currentColor',
opacity: 0.42,
paddingLeft: ({ icon }) => !icon && '1rem'
},
icon: {
color: theme.palette.text.secondary,
Expand Down Expand Up @@ -81,6 +102,12 @@ const SearchBar = forwardRef(
(
{
placeholder: placeholderProp,
icon,
size,
type,
label: labelProp,
componentsProps,
disabledClear,
className,
defaultValue,
elevation,
Expand All @@ -93,11 +120,12 @@ const SearchBar = forwardRef(
ref
) => {
const { t } = useI18n()
const classes = useStyles()
const classes = useStyles({ size, type, icon })
const [currentValue, setCurrentValue] = useState(defaultValue)
const [isFocused, setIsFocused] = useState(false)

const placeholder = placeholderProp || t('search.placeholder')
const label = labelProp || t('search.placeholder')

const delayedOnChange = useMemo(
() => debounce(event => onChange(event), 375),
Expand Down Expand Up @@ -143,18 +171,32 @@ const SearchBar = forwardRef(
ref={ref}
{...props}
>
<Icon className={classes.icon} icon={MagnifierIcon} />
<InputBase
className={classes.inputBase}
placeholder={disabled ? null : placeholder}
value={disabled ? placeholder : currentValue}
disabled={disabled}
aria-label={placeholder}
onChange={handleChange}
onFocus={handleFocus}
onBlur={handleBlur}
/>
{currentValue && (
{type === 'button' ? (
<ButtonBase className={classes.buttonBase}>
{icon && <Icon className={classes.icon} icon={icon} />}
{typeof label === 'string' ? (
<Typography className={classes.typography}>{label}</Typography>
) : (
label
)}
</ButtonBase>
) : (
<>
{icon && <Icon className={classes.icon} icon={icon} />}
<InputBase
{...componentsProps?.inputBase}
className={classes.inputBase}
placeholder={disabled ? null : placeholder}
value={disabled ? placeholder : currentValue}
disabled={disabled}
aria-label={placeholder}
onChange={handleChange}
onFocus={handleFocus}
onBlur={handleBlur}
/>
</>
)}
{currentValue && !disabledClear && (
<IconButton size="medium" onClick={handleClear}>
<Icon icon={CrossCircleIcon} />
</IconButton>
Expand All @@ -174,6 +216,10 @@ SearchBar.displayName = 'SearchBar'

SearchBar.defaultProps = {
elevation: true,
icon: MagnifierIcon,
size: 'small',
type: 'search',
disabledClear: false,
defaultValue: '',
onChange: () => {},
onFocus: () => {},
Expand All @@ -182,9 +228,22 @@ SearchBar.defaultProps = {

SearchBar.propTypes = {
className: PropTypes.string,
type: PropTypes.oneOf(['button', 'search']),
icon: iconPropType,
size: PropTypes.oneOf(['small', 'medium', 'large']),
componentsProps: PropTypes.shape({
/** Props spread to InputBase component */
inputBase: PropTypes.object
}),
defaultValue: PropTypes.string,
disabledClear: PropTypes.bool,
elevation: PropTypes.bool,
placeholder: PropTypes.string,
label: PropTypes.oneOfType([
PropTypes.string,
PropTypes.func,
PropTypes.object
]),
disabled: PropTypes.bool,
onChange: PropTypes.func,
onFocus: PropTypes.func,
Expand Down
Loading