Skip to content

Commit c89be91

Browse files
committed
add tags input
1 parent 3590a13 commit c89be91

File tree

5 files changed

+100
-22
lines changed

5 files changed

+100
-22
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"dependencies": {
1212
"@material-ui/core": "latest",
1313
"@material-ui/icons": "^4.9.1",
14+
"@material-ui/lab": "^4.0.0-alpha.55",
1415
"clsx": "latest",
1516
"moment": "^2.26.0",
1617
"react": "latest",

src/App.js

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ import React, { useState } from "react";
22
import { makeStyles } from "@material-ui/core/styles";
33
import Container from "@material-ui/core/Container";
44
import Typography from "@material-ui/core/Typography";
5-
import TextField from "@material-ui/core/TextField";
65
import Button from "@material-ui/core/Button";
76
import List from "@material-ui/core/List";
87
import ListItem from "@material-ui/core/ListItem";
98
import ListItemText from "@material-ui/core/ListItemText";
109
import Chip from "@material-ui/core/Chip";
10+
import NoteInput from "./NoteInput";
11+
import TagsInput from "./TagsInput";
1112
import moment from "moment";
1213
import { v4 as uuidv4 } from "uuid";
1314

@@ -21,33 +22,29 @@ const useStyles = makeStyles((theme) => ({
2122
form: {
2223
margin: theme.spacing(5, 0, 3, 0),
2324
},
24-
input: {
25-
marginBottom: theme.spacing(2),
26-
},
2725
tag: {
2826
margin: theme.spacing(0, 0, 0, 0.5),
2927
},
3028
}));
3129

3230
export default function App() {
3331
const classes = useStyles();
34-
35-
const dummyData = [
32+
const dummyNotes = [
3633
{
3734
id: uuidv4(),
38-
timestamp: moment().add(25, "minutes"),
35+
timestamp: moment().subtract(25, "minutes"),
3936
text: "Get groceries",
4037
tags: ["todo"],
4138
},
4239
{
4340
id: uuidv4(),
44-
timestamp: moment().add(10, "minutes"),
41+
timestamp: moment().subtract(10, "minutes"),
4542
text: "Go to the gym later",
4643
tags: ["todo", "fitness", "2020 goals"],
4744
},
4845
{
4946
id: uuidv4(),
50-
timestamp: moment().add(4, "minutes"),
47+
timestamp: moment().subtract(4, "minutes"),
5148
text: "Mitch is the trainer from Colorado",
5249
tags: ["people"],
5350
},
@@ -58,9 +55,20 @@ export default function App() {
5855
tags: ["work", "reminder"],
5956
},
6057
];
61-
const [notes, setNotes] = useState(dummyData);
58+
const dummyTags = [
59+
"todo",
60+
"fitness",
61+
"2020 goals",
62+
"people",
63+
"work",
64+
"reminder",
65+
];
66+
const [notes, setNotes] = useState(dummyNotes);
67+
const [tags, setTags] = useState(dummyTags);
6268
// const [notes, setNotes] = useState([]);
63-
const [input, setInput] = useState("");
69+
// const [tags, setTags] = useState([]);
70+
const [noteInputValue, setNoteInputValue] = useState("");
71+
const [tagsInputValue, setTagsInputValue] = useState([]);
6472

6573
const addNote = (text, tags) => {
6674
setNotes([{ id: uuidv4(), timestamp: moment(), text, tags }, ...notes]);
@@ -75,13 +83,17 @@ export default function App() {
7583

7684
const handleSaveNote = (e) => {
7785
e.preventDefault();
78-
setInput("");
79-
// TODO: account for tags
80-
addNote(input, []);
86+
setNoteInputValue("");
87+
setTagsInputValue([]);
88+
addNote(noteInputValue, tagsInputValue);
8189
};
8290

8391
const handleChangeNote = (e) => {
84-
setInput(e.target.value);
92+
setNoteInputValue(e.target.value);
93+
};
94+
95+
const handleChangeTags = (e, newValue) => {
96+
setTagsInputValue(newValue);
8597
};
8698

8799
return (
@@ -95,18 +107,17 @@ export default function App() {
95107
SimpleNote
96108
</Typography>
97109
<form className={classes.form} onSubmit={handleSaveNote}>
98-
<TextField
99-
className={classes.input}
100-
value={input}
101-
onChange={handleChangeNote}
102-
fullWidth
103-
placeholder="Write something here..."
110+
<NoteInput value={noteInputValue} onChange={handleChangeNote} />
111+
<TagsInput
112+
value={tagsInputValue}
113+
onChange={handleChangeTags}
114+
options={tags}
104115
/>
105116
<Button
106117
type="submit"
107118
variant="contained"
108119
color="primary"
109-
disabled={!input}
120+
disabled={!noteInputValue}
110121
>
111122
Save
112123
</Button>

src/NoteInput.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import React from "react";
2+
import { makeStyles } from "@material-ui/core/styles";
3+
import TextField from "@material-ui/core/TextField";
4+
5+
const useStyles = makeStyles((theme) => ({
6+
input: {
7+
marginBottom: theme.spacing(2),
8+
},
9+
}));
10+
11+
export default function NoteInput(props) {
12+
const classes = useStyles();
13+
14+
return (
15+
<TextField
16+
className={classes.input}
17+
value={props.value}
18+
onChange={props.onChange}
19+
fullWidth
20+
placeholder="Write your note..."
21+
/>
22+
);
23+
}

src/TagsInput.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import React from "react";
2+
import { makeStyles } from "@material-ui/core/styles";
3+
import Autocomplete from "@material-ui/lab/Autocomplete";
4+
import TextField from "@material-ui/core/TextField";
5+
6+
const useStyles = makeStyles((theme) => ({
7+
input: {
8+
marginBottom: theme.spacing(3),
9+
},
10+
}));
11+
12+
export default function TagsInput(props) {
13+
const classes = useStyles();
14+
const [inputValue, setInputValue] = React.useState(""); // what's currently typed
15+
16+
return (
17+
<Autocomplete
18+
className={classes.input}
19+
value={props.value}
20+
onChange={props.onChange}
21+
inputValue={inputValue}
22+
onInputChange={(e, newInputValue) => {
23+
setInputValue(newInputValue);
24+
}}
25+
multiple
26+
options={props.options}
27+
renderInput={(params) => (
28+
<TextField {...params} placeholder="Add some tags..." />
29+
)}
30+
/>
31+
);
32+
}

yarn.lock

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,6 +1337,17 @@
13371337
dependencies:
13381338
"@babel/runtime" "^7.4.4"
13391339

1340+
"@material-ui/lab@^4.0.0-alpha.55":
1341+
version "4.0.0-alpha.55"
1342+
resolved "https://registry.yarnpkg.com/@material-ui/lab/-/lab-4.0.0-alpha.55.tgz#82a850dc59654e04ee3a31be1b34eb3bf64d5585"
1343+
integrity sha512-mPHiS52Z1AT6NlWNp07xxTkoKGU3DZhoGdVtLKGy2+uMH5t4/UPtiZLRab7hR3hvuHvguxgV4tkBC9ww3xqUzA==
1344+
dependencies:
1345+
"@babel/runtime" "^7.4.4"
1346+
"@material-ui/utils" "^4.9.6"
1347+
clsx "^1.0.4"
1348+
prop-types "^15.7.2"
1349+
react-is "^16.8.0"
1350+
13401351
"@material-ui/styles@^4.10.0":
13411352
version "4.10.0"
13421353
resolved "https://registry.yarnpkg.com/@material-ui/styles/-/styles-4.10.0.tgz#2406dc23aa358217aa8cc772e6237bd7f0544071"

0 commit comments

Comments
 (0)