Spaces:
Runtime error
Runtime error
Charles De Dampierre
commited on
Commit
·
62bb6b2
1
Parent(s):
7a9ee24
add Search Bar
Browse files- src/TextContainer.jsx +45 -8
src/TextContainer.jsx
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
import React, { useState } from "react";
|
2 |
import PropTypes from "prop-types";
|
3 |
import Box from "@mui/material/Box";
|
4 |
import Typography from "@mui/material/Typography";
|
@@ -8,6 +8,7 @@ import ListItem from "@mui/material/ListItem";
|
|
8 |
import ListItemText from "@mui/material/ListItemText";
|
9 |
import ListItemIcon from "@mui/material/ListItemIcon";
|
10 |
import DescriptionIcon from '@mui/icons-material/Description';
|
|
|
11 |
|
12 |
export const topicsSizeFraction = (topicsCentroids, topicSize) => {
|
13 |
const totalSize = topicsCentroids.reduce((sum, topic) => sum + topic.size, 0);
|
@@ -16,13 +17,26 @@ export const topicsSizeFraction = (topicsCentroids, topicSize) => {
|
|
16 |
|
17 |
function TextContainer({ topicName, topicSizeFraction, content }) {
|
18 |
const [selectedDocument, setSelectedDocument] = useState(null);
|
|
|
|
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
23 |
} else {
|
24 |
-
|
|
|
|
|
|
|
25 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
};
|
27 |
|
28 |
return (
|
@@ -45,6 +59,8 @@ function TextContainer({ topicName, topicSizeFraction, content }) {
|
|
45 |
<Typography variant="h6" style={{ marginBottom: "8px" }}>
|
46 |
{topicName}
|
47 |
</Typography>
|
|
|
|
|
48 |
</Box>
|
49 |
<Typography
|
50 |
variant="h5"
|
@@ -56,10 +72,22 @@ function TextContainer({ topicName, topicSizeFraction, content }) {
|
|
56 |
>
|
57 |
{topicSizeFraction}% of the Territory
|
58 |
</Typography>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
<Paper elevation={3} style={{ maxHeight: "70vh", overflowY: "auto" }}>
|
60 |
<List>
|
61 |
-
{
|
62 |
-
<ListItem
|
|
|
|
|
|
|
|
|
|
|
63 |
<ListItemIcon>
|
64 |
<DescriptionIcon /> {/* Display a document icon */}
|
65 |
</ListItemIcon>
|
@@ -68,6 +96,15 @@ function TextContainer({ topicName, topicSizeFraction, content }) {
|
|
68 |
))}
|
69 |
</List>
|
70 |
</Paper>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
</Box>
|
72 |
</div>
|
73 |
);
|
@@ -79,4 +116,4 @@ TextContainer.propTypes = {
|
|
79 |
content: PropTypes.array.isRequired,
|
80 |
};
|
81 |
|
82 |
-
export default TextContainer;
|
|
|
1 |
+
import React, { useState, useEffect } from "react";
|
2 |
import PropTypes from "prop-types";
|
3 |
import Box from "@mui/material/Box";
|
4 |
import Typography from "@mui/material/Typography";
|
|
|
8 |
import ListItemText from "@mui/material/ListItemText";
|
9 |
import ListItemIcon from "@mui/material/ListItemIcon";
|
10 |
import DescriptionIcon from '@mui/icons-material/Description';
|
11 |
+
import TextField from "@mui/material/TextField";
|
12 |
|
13 |
export const topicsSizeFraction = (topicsCentroids, topicSize) => {
|
14 |
const totalSize = topicsCentroids.reduce((sum, topic) => sum + topic.size, 0);
|
|
|
17 |
|
18 |
function TextContainer({ topicName, topicSizeFraction, content }) {
|
19 |
const [selectedDocument, setSelectedDocument] = useState(null);
|
20 |
+
const [searchQuery, setSearchQuery] = useState("");
|
21 |
+
const [filteredContent, setFilteredContent] = useState([]);
|
22 |
|
23 |
+
useEffect(() => {
|
24 |
+
// Update the filtered content based on the search query
|
25 |
+
if (searchQuery.trim() === "") {
|
26 |
+
setFilteredContent(content);
|
27 |
} else {
|
28 |
+
const filtered = content.filter((doc) =>
|
29 |
+
doc.toLowerCase().includes(searchQuery.toLowerCase())
|
30 |
+
);
|
31 |
+
setFilteredContent(filtered);
|
32 |
}
|
33 |
+
|
34 |
+
// Automatically select the first document when search results change
|
35 |
+
setSelectedDocument(0);
|
36 |
+
}, [searchQuery, content]);
|
37 |
+
|
38 |
+
const handleDocumentClick = (docIndex) => {
|
39 |
+
setSelectedDocument(docIndex);
|
40 |
};
|
41 |
|
42 |
return (
|
|
|
59 |
<Typography variant="h6" style={{ marginBottom: "8px" }}>
|
60 |
{topicName}
|
61 |
</Typography>
|
62 |
+
|
63 |
+
|
64 |
</Box>
|
65 |
<Typography
|
66 |
variant="h5"
|
|
|
72 |
>
|
73 |
{topicSizeFraction}% of the Territory
|
74 |
</Typography>
|
75 |
+
<TextField
|
76 |
+
label="Search"
|
77 |
+
variant="outlined"
|
78 |
+
fullWidth
|
79 |
+
value={searchQuery}
|
80 |
+
onChange={(e) => setSearchQuery(e.target.value)}
|
81 |
+
/>
|
82 |
<Paper elevation={3} style={{ maxHeight: "70vh", overflowY: "auto" }}>
|
83 |
<List>
|
84 |
+
{filteredContent.map((doc, index) => (
|
85 |
+
<ListItem
|
86 |
+
button
|
87 |
+
key={`textcontainerdoc-${index}`}
|
88 |
+
onClick={() => handleDocumentClick(index)}
|
89 |
+
selected={selectedDocument === index}
|
90 |
+
>
|
91 |
<ListItemIcon>
|
92 |
<DescriptionIcon /> {/* Display a document icon */}
|
93 |
</ListItemIcon>
|
|
|
96 |
))}
|
97 |
</List>
|
98 |
</Paper>
|
99 |
+
<Box
|
100 |
+
style={{
|
101 |
+
marginTop: "20px",
|
102 |
+
padding: "8px",
|
103 |
+
width: "80%", // Adjust the width as needed
|
104 |
+
margin: "0 auto", // Center horizontally
|
105 |
+
}}
|
106 |
+
>
|
107 |
+
</Box>
|
108 |
</Box>
|
109 |
</div>
|
110 |
);
|
|
|
116 |
content: PropTypes.array.isRequired,
|
117 |
};
|
118 |
|
119 |
+
export default TextContainer;
|