change tag

This commit is contained in:
karimaldeen 2024-09-15 11:28:51 +03:00
parent 0f54e21546
commit 21cbcc91bb
7 changed files with 130 additions and 30 deletions

View File

@ -9,6 +9,7 @@
margin-block: 10px;
border-radius: 5px;
z-index: 9999999 !important;
cursor: pointer;
.ImageBoxIcon {
cursor: pointer;
}

View File

@ -1,10 +1,8 @@
import { Button } from "antd";
import { BsPlusCircleFill } from "react-icons/bs";
import { useNavigate } from "react-router-dom";
import { useTranslation } from "react-i18next";
import useModalHandler from "../../utils/useModalHandler";
import { MdOutlineArrowForwardIos } from "react-icons/md";
import { deletePathSegments } from "../../utils/deletePathSegments";
import { getPrevPathRoute } from "../../utils/getPrevPathRoute";
import PageTitleComponent from "./PageTitle";
@ -33,10 +31,10 @@ const PageHeader = ({
if (PrevPath === 0) {
return;
}
// navigate(deletePathSegments(location.pathname, PrevPath));
navigate(deletePathSegments(location.pathname, PrevPath));
};
const handleNavigateToPage = (location: string) => {
// navigate(location);
navigate(location);
};
console.log();
@ -46,7 +44,7 @@ const PageHeader = ({
<span className="page_header_links" onClick={handelNavigate}>
<h1 className="page_title">{t(`PageTitle.${pageTitle}`)}</h1>
<span className="page_links">
<MdOutlineArrowForwardIos /> <PageTitleComponent/>
<PageTitleComponent/>
</span>
</span>
{ addModal ? canAdd && (

View File

@ -9,7 +9,7 @@ const Form = () => {
<ValidationField placeholder="name" label="name" name="name" />
</Col>
<div>
<div className="DynamicTags">
<DynamicTags />
</div>
</Row>

View File

@ -0,0 +1,81 @@
import React, { useState, useMemo } from "react";
import { Select, Spin } from "antd";
import { useTranslation } from "react-i18next";
import { useFormikContext } from "formik";
import { useDebounce } from "../../../../utils/useDebounce";
import { useGetAllTag } from "../../../../api/tags";
const SelectTag: React.FC = () => {
const [searchValue, setSearchValue] = useState<string>("");
const [fieldValue, setFieldValue] = useState<string>("");
const formik = useFormikContext<any>();
const handleChange = (value: string[]) => {
console.log(value);
formik.setFieldValue("tags", value);
setSearchValue("");
setFieldValue("");
};
const handleSearch = useDebounce((value: string) => {
setSearchValue(value);
});
const handleFieldChange = (value: string) => {
setFieldValue(value);
};
const handleBlur = () => {
setSearchValue("");
setFieldValue("");
};
const { data, isLoading } = useGetAllTag({
name: searchValue,
});
const [t] = useTranslation();
const options = data?.data ?? [];
const additionalData =
options.length < 1 && searchValue.length > 1 && !isLoading
? [{ id: searchValue, name: searchValue }]
: [];
const value =
formik?.values?.tags?.map((item: any) => item?.id ?? item) ?? [];
const AllOptions = [...options, ...additionalData];
return (
<div className="SelectTag">
<label htmlFor="">{t("models.tag")}</label>
<Select
mode="multiple"
allowClear
style={{ width: "100%", height: "40px" }}
placeholder=""
fieldNames={{ label: "name", value: "id" }}
onChange={handleChange}
options={AllOptions}
filterOption={false}
loading={isLoading}
notFoundContent={isLoading ? <Spin /> : t("practical.not_found")}
onSearch={(value) => {
handleSearch(value);
handleFieldChange(value);
}}
searchValue={fieldValue}
onDropdownVisibleChange={(open) => {
if (!open) {
handleBlur();
}
}}
value={value}
/>
</div>
);
};
export default SelectTag;

View File

@ -1,5 +1,5 @@
import { useFormikContext } from "formik";
import React from "react";
import React, { useEffect } from "react";
import { useTranslation } from "react-i18next";
import { FaCirclePlus } from "react-icons/fa6";
import Tag from "./Tag";
@ -8,20 +8,37 @@ const DynamicTags = () => {
const formik = useFormikContext<any>();
const [t] = useTranslation();
console.log(formik?.values?.synonyms);
const handleAddChoice = () => {
const length = formik?.values?.synonyms.length;
const lastElement = formik?.values?.synonyms[length - 1];
if (lastElement !== "") {
formik.setFieldValue("synonyms", [
...((formik?.values as any)?.synonyms as any[]),
"",
]);
} else {
}
};
const lastElementIndex = formik?.values?.synonyms?.length - 1;
useEffect(() => {
const currentElement = document.getElementById(`synonyms_${lastElementIndex}`)
if (currentElement) {
currentElement.focus(); // Set focus on the element
}
console.log(currentElement,"currentElement");
}, [lastElementIndex])
// console.log(formik?.values);
// console.log(currentTag);

View File

@ -4,57 +4,52 @@ import { useObjectToEdit } from "../../../../zustand/ObjectToEditState";
import { FaTrash } from "react-icons/fa";
const Tag = ({ data, index }: { data: any; index: number }) => {
const inputRef = useRef<HTMLInputElement>(null);
const textareaRef = useRef<HTMLTextAreaElement>(null);
const formik = useFormikContext<any>();
const { setTagsSearch, setCurrentTag } = useObjectToEdit();
useEffect(() => {
if (inputRef.current) {
inputRef.current.style.width = `${(formik?.values?.synonyms[index]?.length + 1) * 8}px`;
if (textareaRef.current) {
// Adjust the height of the textarea based on content
textareaRef.current.style.height = 'auto'; // Reset height
textareaRef.current.style.height = `${textareaRef.current.scrollHeight}px`; // Set to scroll height
}
}, [formik?.values?.synonyms[index]]);
console.log(formik?.values?.synonyms);
console.log(index);
const handleEditInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
// console.log(e.target.value);
const handleEditInputChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
formik.setFieldValue(`synonyms[${index}]`, e.target.value);
setTagsSearch(e.target.value);
setCurrentTag(index);
};
const handleInputBlur = () => {
// setTagsSearch(null)
// Optionally reset search state
// setTagsSearch(null);
};
const handleDeleteChoice = () => {
console.log(data);
// Create a copy of current tags array
const currentTags = [...formik.values.synonyms];
// Remove the item at the specified index from the array
currentTags.splice(index, 1);
console.log(currentTags); // Log the updated tags array
// Update formik field value with the updated tags array
formik.setFieldValue("synonyms", currentTags);
// Reset search state if needed
setTagsSearch(null);
};
return (
<div className="tag">
<input
ref={inputRef}
<textarea
id={`synonyms_${index}`}
ref={textareaRef}
className="tagInput"
type="text"
value={formik?.values?.synonyms[index]}
onChange={handleEditInputChange}
onBlur={handleInputBlur}
rows={1}
style={{ resize: 'none', overflow: 'hidden' }}
/>
<FaTrash onClick={handleDeleteChoice} />
</div>
);
};
export default Tag;
export default Tag;

View File

@ -42,6 +42,9 @@
.exercise_add_main {
background: var(--bg);
padding: 10px 2vw;
max-height: 84vh;
overflow-y: scroll;
@include Scrollbar();
}
.exercise_add_buttons {
display: flex;
@ -90,19 +93,24 @@
background: rgba(49, 130, 206, 0.15);
color: #3182ce;
min-width: 50px;
padding: 3px 10px;
padding: 5px 10px;
border-radius: 5px;
display: flex;
align-items: center;
svg {
}
}
.tagInput {
all: unset;
min-width: 50px;
min-width: 30px;
padding: 3px 10px;
text-align: center;
font-size: 13px;
}
.DynamicTags{
padding: 2px 15px;
}
.suggests {
display: flex;
gap: 20px;