mebubo commited on
Commit
8c96555
·
1 Parent(s): 1435a29

Revert "Attempt at using inline select"

Browse files

This reverts commit 1435a292e25e960b84be51d8b0a2b6c0965982ca.

frontend/public/index.css CHANGED
@@ -121,13 +121,13 @@ details > summary {
121
  color: red;
122
  }
123
 
124
- .word-chip {
125
  background-color: #222222;
126
  color: white;
127
  padding: 2px;
128
  }
129
 
130
- .word-chip.flagged {
131
  background-color: #dd1111;
132
  }
133
 
 
121
  color: red;
122
  }
123
 
124
+ span.word-chip {
125
  background-color: #222222;
126
  color: white;
127
  padding: 2px;
128
  }
129
 
130
+ span.word-chip.flagged {
131
  background-color: #dd1111;
132
  }
133
 
frontend/src/components/WordChip.tsx CHANGED
@@ -1,4 +1,4 @@
1
- import React from "react"
2
 
3
  export const WordChip = ({
4
  word,
@@ -13,39 +13,63 @@ export const WordChip = ({
13
  replacements: string[],
14
  onReplace: (newWord: string) => void
15
  }) => {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
  const handleReplacement = (event: React.ChangeEvent<HTMLSelectElement>) => {
18
  console.log("handleReplacement", event.target.value)
19
  const newWord = event.target.value
20
  onReplace(newWord)
21
- }
22
-
23
- const className = `word-chip ${logprob < threshold ? "flagged" : ""}`
24
-
25
- if (replacements.length === 0) {
26
- return <span className={className}>{word}</span>
27
  }
28
 
29
  return (
30
- <select
31
- className={className}
32
- onChange={handleReplacement}
33
- style={{
34
- appearance: 'none',
35
- border: 'none',
36
- fontFamily: 'inherit',
37
- fontSize: 'inherit',
38
- color: 'inherit',
39
- background: 'inherit',
40
- backgroundColor: 'red',
41
- padding: 0,
42
- cursor: 'pointer'
43
- }}
44
  >
45
- <option key="original" hidden>{word}</option>
46
- {replacements.map((r, i) => (
47
- <option key={i} value={r}>{r}</option>
48
- ))}
49
- </select>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  )
51
  }
 
1
+ import React, { useState, useEffect, useRef } from "react"
2
 
3
  export const WordChip = ({
4
  word,
 
13
  replacements: string[],
14
  onReplace: (newWord: string) => void
15
  }) => {
16
+ const [isExpanded, setIsExpanded] = useState(false);
17
+ const dropdownRef = useRef<HTMLSelectElement>(null);
18
+
19
+ useEffect(() => {
20
+ const handleClickOutside = (event: MouseEvent) => {
21
+ if (dropdownRef.current && !dropdownRef.current.contains(event.target as Node)) {
22
+ setIsExpanded(false);
23
+ }
24
+ };
25
+
26
+ document.addEventListener("mousedown", handleClickOutside);
27
+ return () => {
28
+ document.removeEventListener("mousedown", handleClickOutside);
29
+ };
30
+ }, []);
31
+
32
+ const handleClick = () => {
33
+ if (logprob < threshold && replacements.length > 0) {
34
+ setIsExpanded(true);
35
+ }
36
+ }
37
 
38
  const handleReplacement = (event: React.ChangeEvent<HTMLSelectElement>) => {
39
  console.log("handleReplacement", event.target.value)
40
  const newWord = event.target.value
41
  onReplace(newWord)
42
+ setIsExpanded(false);
 
 
 
 
 
43
  }
44
 
45
  return (
46
+ <span
47
+ title={logprob.toFixed(2)}
48
+ className={`word-chip ${logprob < threshold ? "flagged" : ""}`}
49
+ style={{ position: "relative", cursor: logprob < threshold ? "pointer" : "default" }}
50
+ onClick={handleClick}
 
 
 
 
 
 
 
 
 
51
  >
52
+ {word}
53
+ {isExpanded && (
54
+ <select
55
+ ref={dropdownRef}
56
+ onChange={handleReplacement}
57
+ value={word}
58
+ style={{
59
+ position: "absolute",
60
+ top: "100%",
61
+ left: 0,
62
+ zIndex: 1000,
63
+ overflowY: "auto"
64
+ }}
65
+ size={replacements.length + 1}
66
+ >
67
+ <option key="original" hidden>{word}</option>
68
+ {replacements.map((r, i) => (
69
+ <option key={i} value={r}>{r}</option>
70
+ ))}
71
+ </select>
72
+ )}
73
+ </span>
74
  )
75
  }