File size: 618 Bytes
9a42933
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
export function useCharacterLimit({
  value,
  nbCharsLimits,
  warnBelow,
}: {
  value: string,
  nbCharsLimits: number,
  warnBelow: number
}) {
  const nbCharsUsed = value.length

  const nbCharsRemaining = Math.max(0, nbCharsLimits - nbCharsUsed)

  const colorClass = 
  nbCharsUsed > nbCharsLimits
      ? "text-red-500" :
      nbCharsRemaining < 4
      ? "text-orange-600" :
      nbCharsRemaining < 8
     ? "text-yellow-600"
     : "text-green-600"

  const shouldWarn = nbCharsRemaining < warnBelow

  return {
    nbCharsUsed,
    nbCharsRemaining,
    nbCharsLimits,
    colorClass,
    shouldWarn,
  }
}