-
Notifications
You must be signed in to change notification settings - Fork 20
fix(plasma-web,plasma-b2c): Add useResizeObserver and fix behavior with resize in TextArea
#1186
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { useEffect } from 'react'; | ||
|
||
/** | ||
* Отслеживает изменение размеров переданного элемента | ||
* @param {React.Ref<T>} ref - реф элемента, за которым нужно следить | ||
* @callback callback - функция, которая вызывается при изменении элемента | ||
* @param {Element} element - элемент, размер которого изменился | ||
*/ | ||
export const useResizeObserver = <T extends HTMLElement>( | ||
ref: React.MutableRefObject<T | null>, | ||
callback: (element: T) => void, | ||
) => { | ||
useEffect(() => { | ||
/* istanbul ignore if: убираем проверку на рефы из покрытия */ | ||
if (!ref?.current) { | ||
return; | ||
} | ||
|
||
const { current } = ref; | ||
|
||
const resizeObserver = new window.ResizeObserver(() => callback(current)); | ||
|
||
resizeObserver.observe(ref.current); | ||
|
||
return () => { | ||
/* istanbul ignore if: убираем проверку на рефы из покрытия */ | ||
if (!ref?.current) { | ||
return; | ||
} | ||
|
||
resizeObserver.unobserve(ref.current); | ||
}; | ||
}, [ref]); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,12 @@ | ||
import React, { forwardRef } from 'react'; | ||
import React, { forwardRef, useState, useMemo, createRef } from 'react'; | ||
import styled from 'styled-components'; | ||
import { FieldRoot, FieldContent, FieldHelper, TextArea as BaseArea } from '@sberdevices/plasma-core'; | ||
import { | ||
FieldRoot, | ||
FieldContent, | ||
FieldHelper, | ||
TextArea as BaseArea, | ||
useResizeObserver, | ||
} from '@sberdevices/plasma-core'; | ||
import type { TextAreaProps as BaseProps } from '@sberdevices/plasma-core'; | ||
|
||
import { applyInputStyles } from '../Field'; | ||
|
@@ -16,14 +22,35 @@ const StyledTextArea = styled(BaseArea)` | |
${applyInputStyles} | ||
`; | ||
|
||
const StyledFieldHelperWrapper = styled.div<{ width: number }>` | ||
position: absolute; | ||
top: 0; | ||
|
||
display: flex; | ||
justify-content: flex-end; | ||
|
||
width: ${({ width }) => width}px; | ||
`; | ||
|
||
/** | ||
* Поле ввода многострочного текста. | ||
*/ | ||
// eslint-disable-next-line prefer-arrow-callback | ||
export const TextArea = forwardRef<HTMLTextAreaElement, TextAreaProps>(function TextArea( | ||
{ id, disabled, status, label, placeholder, contentRight, helperText, style, className, ...rest }, | ||
ref, | ||
outerRef, | ||
) { | ||
const [width, setWidth] = useState(0); | ||
const ref = useMemo(() => (outerRef && 'current' in outerRef ? outerRef : createRef<HTMLTextAreaElement>()), [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. не сломается, если сюда функцию передадут в There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. не должно, как раз проверкой на |
||
outerRef, | ||
]); | ||
|
||
useResizeObserver(ref, (currentElement) => { | ||
const { width: elementWidth } = currentElement.getBoundingClientRect(); | ||
|
||
setWidth(elementWidth); | ||
}); | ||
|
||
Comment on lines
+43
to
+53
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. не хочется это в отдельный хуй отнести? один и тот же код два раза встречается There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. кажется, что пока нет в этом необходимости, т.к. конкретно эти случаи очень специфичны и появляются в двух компонентах. |
||
const placeLabel = (label || placeholder) as string | undefined; | ||
|
||
return ( | ||
|
@@ -44,8 +71,10 @@ export const TextArea = forwardRef<HTMLTextAreaElement, TextAreaProps>(function | |
aria-describedby={id ? `${id}-helpertext` : undefined} | ||
{...rest} | ||
/> | ||
{contentRight && <FieldContent pos="right">{contentRight}</FieldContent>} | ||
{helperText && <FieldHelper id={id ? `${id}-helpertext` : undefined}>{helperText}</FieldHelper>} | ||
<StyledFieldHelperWrapper width={width}> | ||
{contentRight && <FieldContent pos="right">{contentRight}</FieldContent>} | ||
</StyledFieldHelperWrapper> | ||
</FieldRoot> | ||
); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
а этот ифак зачем? у тебя же не дойдет сюда если рефа нет
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ага, я тоже так думал, но почему-то ts ругается