Skip to content

Commit 64e380b

Browse files
committed
add new feature
1 parent 5fcbad0 commit 64e380b

File tree

7 files changed

+209
-77
lines changed

7 files changed

+209
-77
lines changed

src/components/Headerbar/index.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function Headerbar() {
4040
</div>
4141

4242
<div className="control">
43-
<Link to="/">
43+
<Link to="/new-blog">
4444
<Button>Create</Button>
4545
</Link>
4646
{/* <Link to="/">

src/components/Select/index.tsx

+110-50
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
import React from "react";
22
import "./styles.scss";
3-
import {
4-
UseFormRegister,
5-
UseFormRegisterReturn,
6-
UseFormSetValue,
7-
} from "react-hook-form";
3+
import { UseFormSetValue } from "react-hook-form";
84
import useToggle from "../../hooks/useToggle";
9-
import { IBlog, IUser } from "../../interfaces";
105
import Icon from "../Icon";
116
import { X } from "@styled-icons/heroicons-outline";
7+
import { DEFAULT_PROFFILE_IMG_URL } from "../../utils/constants";
8+
import { IUser } from "../../interfaces";
129

1310
interface ISelectProps {
14-
defaultValues?: any[];
15-
datalist: any[];
11+
// defaultValues?: any[];
12+
// datalist: any[];
13+
defaultValues?: string[];
14+
datalist: Array<string | IUser>;
1615
type?: "multiple" | "single";
1716
maxTags?: number;
1817
hideTag?: boolean;
@@ -29,18 +28,29 @@ const Select = ({
2928
name,
3029
setValue,
3130
}: ISelectProps) => {
31+
// const defaultVl = defaultValues.map((item) => {
32+
// if (typeof item === "string") {
33+
// return item;
34+
// } else {
35+
// return item.username;
36+
// }
37+
// });
38+
3239
const [isShow, setShow] = useToggle(false);
3340
const [inputValue, setInputValue] = React.useState("");
34-
const [tags, setTags] = React.useState<any[]>(defaultValues);
41+
const [tags, setTags] = React.useState<string[]>(defaultValues);
3542
const [isKeyReleased, setIsKeyReleased] = React.useState(false);
3643
const inputRef = React.useRef<HTMLInputElement>(null);
3744

38-
const dataLst: any[] =
39-
typeof datalist[0] === "string"
40-
? datalist
41-
: datalist.map((d) => d.username);
45+
const dataLs: any[] = datalist.map((item) => {
46+
if (typeof item === "string") {
47+
return item;
48+
} else {
49+
return item.username;
50+
}
51+
});
4252

43-
const filter = dataLst
53+
const filter = dataLs
4454
.sort()
4555
.filter(
4656
(item) =>
@@ -57,7 +67,9 @@ const Select = ({
5767
const handleCheckoxChange = (
5868
e: React.ChangeEvent<HTMLInputElement>
5969
): void => {
70+
console.log("running");
6071
const { checked, value } = e.target;
72+
console.log(value, checked);
6173
let newCheckedList = [...tags];
6274

6375
if (checked) {
@@ -79,7 +91,7 @@ const Select = ({
7991
) {
8092
e.preventDefault();
8193

82-
if (dataLst.includes(trimmedInput)) {
94+
if (dataLs.includes(trimmedInput)) {
8395
setTags((prev) => [...prev, trimmedInput]);
8496
} else if (filter.length) {
8597
for (let i = 0; i < filter.length; i++) {
@@ -118,7 +130,7 @@ const Select = ({
118130
setIsKeyReleased(true);
119131
};
120132

121-
const search = (data: any[]): any[] => {
133+
const search = (data: Array<string | IUser>): any[] => {
122134
return data.filter(
123135
(item) =>
124136
(typeof item === "string" ? item : item.username)
@@ -128,45 +140,90 @@ const Select = ({
128140
);
129141
};
130142

131-
// React.useEffect(() => {
132-
// console.log(tags);
133-
// setValue(name, tags);
134-
// }, []);
143+
React.useEffect(() => {
144+
console.log(tags);
145+
setValue(name, tags);
146+
}, [tags]);
135147

136148
const deleteTag = (index: number) => {
137149
setTags((prevState) => prevState.filter((tag, i) => i !== index));
138150
};
139151

152+
const handleFocus = () => {
153+
setShow(true);
154+
if (inputRef.current) {
155+
inputRef.current.focus();
156+
}
157+
};
158+
159+
const handleBlur = (e: React.FocusEvent) => {
160+
if (!e.currentTarget.contains(e.relatedTarget)) {
161+
setShow(false);
162+
}
163+
};
164+
165+
const renderOptions = () => {
166+
const isString = typeof datalist[0] === "string";
167+
168+
let options = search(datalist)
169+
.sort(
170+
isString
171+
? undefined
172+
: (a, b) =>
173+
a.username > b.username ? 1 : b.username > a.username ? -1 : 0
174+
)
175+
.map(
176+
isString
177+
? (item) => (
178+
<React.Fragment key={item}>
179+
<input
180+
id={item}
181+
type="checkbox"
182+
value={item}
183+
checked={tags.includes(item)}
184+
onChange={handleCheckoxChange}
185+
/>
186+
<label htmlFor={item}>{item}</label>
187+
</React.Fragment>
188+
)
189+
: (item) => (
190+
<React.Fragment key={item.username}>
191+
<input
192+
id={item.username}
193+
type="checkbox"
194+
value={item.username}
195+
checked={tags.includes(item.username)}
196+
onChange={(e) => handleCheckoxChange(e)}
197+
/>
198+
<label htmlFor={item} className="user-row">
199+
<img src={DEFAULT_PROFFILE_IMG_URL} alt="profile" />
200+
<div>{item.username}</div>
201+
</label>
202+
</React.Fragment>
203+
)
204+
);
205+
206+
return options;
207+
};
208+
140209
return (
141-
<div
142-
className="select"
143-
onFocus={() => {
144-
setShow(true);
145-
if (inputRef.current) {
146-
inputRef.current.focus();
147-
}
148-
}}
149-
onBlur={(e) => {
150-
if (!e.currentTarget.contains(e.relatedTarget)) {
151-
setShow(false);
152-
}
153-
}}
154-
>
155-
{tags.map((tag, index) => (
156-
<div className="select__tag" tabIndex={1}>
157-
{tag}
158-
<button onClick={() => deleteTag(index)}>
159-
<Icon icon={X} size={12} />
160-
</button>
161-
</div>
162-
))}
210+
<div className="select" onFocus={handleFocus} onBlur={handleBlur}>
211+
{!hideTag &&
212+
tags.map((tag, index) => (
213+
<div className="select__tag" tabIndex={1} key={tag + "-tag"}>
214+
{tag}
215+
<button onClick={() => deleteTag(index)}>
216+
<Icon icon={X} size={12} />
217+
</button>
218+
</div>
219+
))}
163220
<input
164221
className="select__input"
165-
onChange={handleInputChange}
166222
value={inputValue}
223+
ref={inputRef}
224+
onChange={handleInputChange}
167225
onKeyDown={handleKeydown}
168226
onKeyUp={handleKeyUp}
169-
ref={inputRef}
170227
/>
171228
<div
172229
className="select__dropdown"
@@ -176,7 +233,7 @@ const Select = ({
176233
}}
177234
style={{ display: isShow ? "block" : "none" }}
178235
>
179-
{typeof datalist[0] === "string" &&
236+
{/* {typeof datalist[0] === "string" &&
180237
datalist &&
181238
search(datalist)
182239
.sort()
@@ -199,21 +256,24 @@ const Select = ({
199256
.sort((a, b) =>
200257
a.username > b.username ? 1 : b.username > a.username ? -1 : 0
201258
)
202-
.map((item, index) => (
259+
.map((item) => (
203260
<div key={item.guid}>
204261
<input
205262
type="checkbox"
206-
value={item}
263+
value={item.guid}
207264
checked={tags.includes(item)}
208265
onChange={handleCheckoxChange}
209-
id={item}
266+
id={item.guid}
267+
name="select-option"
210268
/>
211-
<label htmlFor={item}>
269+
<label htmlFor={item} className="user-row">
270+
<img src={DEFAULT_PROFFILE_IMG_URL} alt="profile" />
212271
<div>{item.guid}</div>
213272
<div>{item.username}</div>
214273
</label>
215274
</div>
216-
))}
275+
))} */}
276+
{renderOptions()}
217277
</div>
218278
</div>
219279
);

src/components/Select/styles.scss

+19
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,29 @@
9999
&:hover {
100100
color: var.$n5;
101101
}
102+
103+
* {
104+
cursor: pointer;
105+
}
102106
}
103107

104108
input:checked + label {
105109
color: var.$p1;
106110
}
111+
112+
.user-row {
113+
display: flex;
114+
align-items: center;
115+
gap: 14px;
116+
padding: 8px 10px;
117+
118+
img {
119+
height: 34px;
120+
width: 34px;
121+
border-radius: 7px;
122+
object-fit: cover;
123+
object-position: center;
124+
}
125+
}
107126
}
108127
}

0 commit comments

Comments
 (0)