-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[EN-6218] feat(textarea): add textarea line limit
- Loading branch information
Showing
8 changed files
with
187 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1
src/components/modals/Modal/ModalGeneric/PostOpportunityModal/PostOpportunityModal.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
import _ from 'lodash'; | ||
import moment from 'moment'; | ||
import React, { | ||
/* memo, */ useCallback, | ||
|
1 change: 0 additions & 1 deletion
1
src/components/utils/BackgroundImage/BackgroundImage.styles.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { useCallback, useEffect, useRef, useState } from 'react'; | ||
import UIkit from 'uikit'; | ||
import { usePrevious } from 'src/hooks/utils'; | ||
|
||
export function useLineLimit( | ||
value: string, | ||
name: string, | ||
onChange: (arg: { target: { value: string; name: string } }) => void, | ||
maxLines?: number | ||
) { | ||
const prevValue: string = usePrevious(value); | ||
|
||
const ref = useRef<HTMLTextAreaElement>(); | ||
|
||
const [numberOfLines, setNumberOfLines] = useState(0); | ||
|
||
const calculateContentHeight = useCallback((ta, scanAmount) => { | ||
const origHeight = ta.style.height; | ||
let height = ta.offsetHeight; | ||
const { scrollHeight } = ta; | ||
const { overflow } = ta.style; | ||
/// only bother if the ta is bigger than content | ||
if (height >= scrollHeight) { | ||
/// check that our browser supports changing dimension | ||
/// calculations mid-way through a function call... | ||
ta.style.height = `${height + scanAmount}px`; | ||
/// because the scrollbar can cause calculation problems | ||
ta.style.overflow = 'hidden'; | ||
/// by checking that scrollHeight has updated | ||
if (scrollHeight < ta.scrollHeight) { | ||
/// now try and scan the ta's height downwards | ||
/// until scrollHeight becomes larger than height | ||
while (ta.offsetHeight >= ta.scrollHeight) { | ||
ta.style.height = `${(height -= scanAmount)}px`; | ||
} | ||
/// be more specific to get the exact height | ||
while (ta.offsetHeight < ta.scrollHeight) { | ||
ta.style.height = `${(height += 1)}px`; | ||
} | ||
/// reset the ta back to it's original height | ||
ta.style.height = origHeight; | ||
/// put the overflow back | ||
ta.style.overflow = overflow; | ||
return height; | ||
} | ||
} else { | ||
return scrollHeight; | ||
} | ||
}, []); | ||
|
||
useEffect(() => { | ||
if (maxLines && ref && ref.current && value !== prevValue) { | ||
const ta = ref.current; | ||
|
||
const style = window.getComputedStyle | ||
? window.getComputedStyle(ta) | ||
: ta.style; | ||
|
||
const taLineHeight = parseInt(style.lineHeight, 10); | ||
const taPaddingBottom = parseInt(style.paddingBottom, 10); | ||
const taPaddingTop = parseInt(style.paddingTop, 10); | ||
const taHeight = calculateContentHeight(ta, taLineHeight); | ||
|
||
const nbOfLines = Math.ceil( | ||
(taHeight - taPaddingBottom - taPaddingTop) / taLineHeight | ||
); | ||
|
||
if (nbOfLines > maxLines) { | ||
if (!prevValue || value.length - prevValue.length > 1) { | ||
UIkit.notification( | ||
`Le texte que vous avez essayé de coller dépasse la limite des ${maxLines} lignes.`, | ||
'danger' | ||
); | ||
} | ||
|
||
onChange({ | ||
target: { | ||
value: prevValue || '', | ||
name, | ||
}, | ||
}); | ||
} else { | ||
setNumberOfLines(!value ? 0 : nbOfLines); | ||
} | ||
} | ||
}, [calculateContentHeight, maxLines, name, onChange, prevValue, value]); | ||
|
||
const remainingLines = | ||
maxLines - numberOfLines < 0 ? 0 : maxLines - numberOfLines; | ||
|
||
return { remainingLines, textAreaRef: ref }; | ||
} |