React Hooks clickhandler to children - reactjs

Hello I have a clickhandler that I send to a child component and use it on onclick, but for some reason, my click handler event on my parent component is not running
parent jsx:
type ClickHandler = (tag: ITag) => (e: MouseEvent) => void
const MenuTags: React.FC<{hover: boolean}> = observer(({hover}) => {
const {layoutStore} = useRootStore()
const [tags, setTags] = useState<ITag[]>(Tags)
const showHideDropItem: ShowHideDropItem = (tag) => {
console.log(tag)
setTags((items) =>
items.map((item) => ({
...item,
Active: item.Name === tag.Name ? tag.Active !== true : false,
})),
)
}
const clickHandler: ClickHandler = (tag) => (e) => {
console.log('a')
e.preventDefault()
showHideDropItem(tag)
}
return (
<MenuList
open={layoutStore.sideBar || layoutStore.onHoverSideState}
hover={hover}
>
{tags.map((item) => (
<div key={JSON.stringify(item.Name)}>
{item.Title ? <div className="title_tagList">{item.Title}</div> : ''}
<TagList
open={layoutStore.sideBar || layoutStore.onHoverSideState}
tag={item}
clickHandler={clickHandler}
/>
</div>
))}
</MenuList>
)
})
my children jsx:
const TagList: React.FC<ITagList> = observer(({tag, clickHandler, open}) => {
const tagHandleClick = (e: any) => {
e.preventDefault()
if (tag.Active !== undefined) clickHandler(tag)
}
return (
<ListItem open={open} isDropDown={!!tag.DropdownItems} active={tag.Active}>
<div className="tag-container">
<NavLink
className="tag-wrapper"
to={tag.Link}
onClick={tagHandleClick}
>
<tag.Icon className="svg-main" size={22} />
<span className="tag-name">{tag.Name}</span>
</NavLink>
</div>
</ListItem>
)
})
when clicking on my event it enters my handler of the child component, but the handler does not call my parent component's handler

Your clickHandler is a function that returns a function. It might be easier to see if you temporarily rewrite it like this:
const clickHandler: ClickHandler = (tag) => {
return (e) => {
console.log("a")
e.preventDefault()
showHideDropItem(tag)
}
}
Instead of returning a function you could just do the logic of the inner function directly instead.
const clickHandler: ClickHandler = (tag) => {
console.log('a')
showHideDropItem(tag)
}

Related

React select box function

I am trying to make a custom select box component with parent and shild components, with autocomplete and also fetching from api. The problem is that i am trying to fire onchange function from parent to child to select an item from the select box but it is not working, can someone tell me where is the problem?
export function SelectComponent() {
const [results, setResults] = useState([]);
const [selectedValue, setSelectedValue] = useState<ComboBoxOption>();
const handleOnChange = (e: any) => {
if (!e.target.value.trim()) return setResults([]);
const filteredValue = results.filter((item: any) =>
item.value.toString().toLowerCase().startsWith(item.toLowerCase())
);
setResults(filteredValue);
};
useEffect(() => {
const fetchData = async () => {
const response = await axios.get(...);
setResults(response.data);
};
fetchData();
}, []);
return (
<div>
<SelectField
options={results}
value={selectedValue?.value}
onChange={handleOnChange}
onSelect={item => setSelectedValue(item)}
/>
</div>
);
}
export function SelectField({
...
}: SelectFieldProps) {
const [isOpen, setIsOpen] = useState(false);
const [isActive, setIsActive] = useState(false);
const [defaultValue, setDefaultValue] = useState("");
const handleOnChange: React.ChangeEventHandler<HTMLInputElement> = event => {
setIsOpen(true);
setDefaultValue(event.target.value);
onChange && onChange(event);
};
return (
<div>
<input
placeholder={placeholder}
value={defaultValue}
onChange={handleOnChange}
/>
<button onClick={() => {setIsOpen(!isOpen);}}></button>
<ul>
{options.map((option: any, index: any) => {
return (
<li
key={index}
onClick={() => {setIsOpen(false);}
>
<span>{option.value}</span>
</li>
);
})}
</ul>
)
</div>
);
}
It looks like the problem may be in the handleOnChange function in the SelectComponent. The function is trying to filter the results state based on the value of the input element, but it should be filtering based on the e.target.value instead. Also, it's using item.toLowerCase() which doesn't make sense, instead it should use e.target.value.toLowerCase():
const handleOnChange = (e: any) => {
if (!e.target.value.trim()) return setResults([]);
const filteredValue = results.filter((item: any) =>
item.value.toString().toLowerCase().startsWith(e.target.value.toLowerCase())
);
setResults(filteredValue);
};
Also, in the SelectField component, it seems that you are not calling the onSelect prop when an option is selected. You should call the onSelect prop and pass the selected option as a parameter when an option is clicked, like so:
<li
key={index}
onClick={() => {
setIsOpen(false);
onSelect(option);
}}
>
<span>{option.value}</span>
</li>
I would also recommend using onBlur instead of onClick for the input field, this way it can be closed when the user clicks outside of the component.

Changes don't render in React

I have array of items and searching items function, that returns another array. When I delete or edit item finded items changes don't render, but when search string has another value React render changes.
I know that useEffect can resolve this problem, but dont what to put in callback.
How can resolve this problem?
export const ToDoList = (props: PropsType) => {
const [searchQuery, setSearchQuery] = useState('')
const searchedItems = useMemo(() => {
return props.ToDoData.filter(item => item.text.includes(searchQuery))
},
[searchQuery])
return (
{props.ToDoData.length ?
<>
<input
...
onChange={e => setSearchQuery(e.target.value)}
/>
<ItemsList
...
items={
searchQuery ?
searchedItems :
props.ToDoData
}
/>
</> :
...
}
)
}
export const ItemsList = (props: PropsType) => {
const [editedText, setEditedText] = useState('')
const onDeleteItem = (id: number) => {
props.dispatch(deleteItem(id))
},
onEditItemMode = (id: number, text: string) => {
props.dispatch(setEditMode(true, id))
setEditedText(text)
},
onEditText = (id: number) => {
props.dispatch(setEditedTextInItem(id, editedText))
props.dispatch(setEditMode(false, id))
setEditedText('')
},
onToggleCompletedStatus = (id: number, status: string) => {
...
}
return (
{props.items.length ?
props.items.map((object) => (
<div
className="Item"
key={object.id}
>
{props.inEditMode.some((id: number) => id === object.id) ?
<>
<input
value={editedText}
onChange={e => { setEditedText(e.currentTarget.value) }}
/>
<button onClick={() => onEditText(object.id)}>
Change text
</button>
</> :
<>
<div className="Item__textBlock">
<input
type='checkbox'
onClick={() => { onToggleCompletedStatus(object.id, object.status)}}
/>
<span className={
object.status === 'completed' ?
'completed' :
'in process'
}>
{object.text}
</span>
</div>
<div className="Item__buttonBlock">
<button
className="Item__button"
disabled={props.inEditMode.length !== 0}
onClick={() => onEditItemMode(object.id, object.text)}
>
<img src={editImg} />
</button>
<button
className="Item__button"
onClick={() => { onDeleteItem(object.id) }}
>
<img src={removeImg} />
</button>
</div>
</>
}
</div>
)) :
...
}
)
}
// This code creates a list that is ONLY updated when searchQuery is updated
const searchedItems = useMemo(() => {
return props.ToDoData.filter(item => item.text.includes(searchQuery))
}, [searchQuery]);
// This code creates the list every time the component renders,
// so it will always be correct
const searchedItems = props.ToDoData.filter(item => item.text.includes(searchQuery))
// If you absolutely need to optimize the render of this component
// This code will update the list whenever the reference for ToDoData is updated as well
const searchedItems = useMemo(() => {
return props.ToDoData.filter(item => item.text.includes(searchQuery))
}, [searchQuery, props.ToDoData]);

Focus Trap React and a few component's in 2 popup's

I have 2 popup's(I reuse CloseButton(component) and Modal(component) in 2 popup's) and need to do focus trap at all. I lf answer 4 better way.
1 popup Screen, components: ModalLogin-Modal-CloseButton.
I read about some hooks: useRef() and forwardRef(props, ref)
but i don't undestand why it's not work in my case. I am trying to find a solution. I need help :)
In ModalLogin, I try to do a focus trap. To do this, I mark what should happen with focus when moving to 1 and the last element. I need to pass my ref hook obtained via Modal-CloseButton. I read that you can't just transfer refs to functional components. I try to use the forwardref hook in the necessary components where I transfer it, here's what I do:
All links without focus-trap and hook's!.
https://github.com/j3n4r3v/ligabank-credit/blob/master/src/components/form-login/modal-login.jsx [Modal-login full]
const ModalLogin = () => {
const topTabTrap* = useRef();
const bottomTabTrap* = useRef();
const firstFocusableElement = useRef();
const lastFocusableElement = useRef();
useEffect(() => {
const trapFocus = (event) => {
if (event.target === topTabTrap.current) {
lastFocusableElement.current.focus()
}
if (event.target === bottomTabTrap.current) {
firstFocusableElement.current.focus()
}
}
document.addEventListener('focusin', trapFocus)
return () => document.removeEventListener('focusin', trapFocus)
}, [firstFocusableElement, lastFocusableElement])
return (
<Modal onCloseModal={() => onCloseForm()} ref={lastFocusableElement}>
<form >
<span ref={topTabTrap} tabIndex="0" />
<Logo />
<Input id="email" ref={firstFocusableElement} />
<Input id="password" />
<Button type="submit" />
<span ref={bottomTabTrap} tabIndex="0"/>
</form>
</Modal>
);
};
https://github.com/j3n4r3v/ligabank-credit/blob/master/src/components/modal/modal.jsx [Modal full]
const Modal = forwardRef(({ props, ref }) => {
const { children, onCloseModal, ...props } = props;
const overlayRef = useRef();
useEffect(() => {
const preventWheelScroll = (evt) => evt.preventDefault();
document.addEventListener('keydown', onEscClick);
window.addEventListener('wheel', preventWheelScroll, { passive: false });
return () => {
document.removeEventListener('keydown', onEscClick);
window.removeEventListener('wheel', preventWheelScroll);
};
});
const onCloseModalButtonClick = () => {
onCloseModal();
};
return (
<div className="overlay" ref={overlayRef}
onClick={(evt) => onOverlayClick(evt)}>
<div className="modal">
<CloseButton
ref={ref}
onClick={() => onCloseModalButtonClick()}
{...props}
/>
{children}
</div>
</div>
);
});
https://github.com/j3n4r3v/ligabank-credit/blob/master/src/components/close-button/close-button.jsx [CloseButton full]
const CloseButton = forwardRef(({ props, ref }) => {
const {className, onClick, ...props} = props;
return (
<button className={`${className} close-button`}
onClick={(evt) => onClick(evt)}
tabIndex="0"
ref={ref}
{...props}
>Close</button>
);
});
And now i have a lot of errors just like: 1 - Cannot read properties of undefined (reading 'children') - Modal, 2 - ... className undefined in CloseButton etc.
2 popup Screen, components: Modal(reuse in 1 popup) - InfoSuccess- CloseButton(reuse in 1 popup)
I have only 1 interactive element - button (tabindex) and no more. Now i don't have any idea about 2 popup with focus-trap ((
https://github.com/j3n4r3v/ligabank-credit/blob/master/src/components/success-modal/success-modal.jsx [SuccessModal full]
const SuccessModal = ({ className, onChangeVisibleSuccess }) => {
return (
<Modal onCloseModal={() => onChangeVisibleSuccess(false)}>
<InfoSuccess className={className} />
</Modal>
);
};
https://github.com/j3n4r3v/ligabank-credit/blob/master/src/components/info-block/info-block.jsx [Infoblock full]
const InfoBlock = ({ className, title, desc, type }) => {
return (
<section className={`info-block ${className} info-block--${type}`}>
<h3 className="info-block__title">{title}</h3>
<p className="info-block__desc">{desc}</p>
</section>
);
};
const InfoSuccess = ({ className }) => (
<InfoBlock
title="Спасибо за обращение в наш банк."
desc="Наш менеджер скоро свяжется с вами по указанному номеру телефона."
type="center"
className={className}
/>
);
I know about 3 in 1 = 1 component and no problem in popup with Focus-Trap. But i want understand about my case, it's real to life or not and what best practice.

How do I write test for hooks?

I am making unit test for the component and also trying to make a test for hook but I can't seem to get it working. This is my hook. What do I need to change or do to fix this test?
import { useState } from 'react';
function UseToggleState (initialValue = false) {
const [state, setState] = useState(initialValue);
const toggle = () => setState(!state);
return [state, toggle];
};
export default UseToggleState
And this is the component I am using it.
export function Todo({ id, task, completed }) {
const classes = useStyles();
const dispatch = useContext(DispatchContext);
const [isEditing, toggle] = useToggleState(false);
if (isEditing) {
return (
<li
className={classes.Todo}
style={{ overflowY: "hidden" }}
onClick={() => toggle()}
>
<EditForm id={id} task={task} toggleEditForm={toggle} />
</li>
);
}
return (
<li
className={classes.Todo}
onClick={() => dispatch({ type: TOGGLE_TODO, id })}
>
<span
style={{
textDecoration: completed ? "line-through" : "",
color: completed ? "#A9ABAE" : "#34495e",
}}
>
{task}
</span>
<div className={classes.icons}>
<FontAwesomeIcon
icon={faPen}
size="1x"
onClick={(e) => {
e.stopPropagation();
toggle();
}}
/>{" "}
<FontAwesomeIcon
icon={faTrash}
size="1x"
color={"#c0392b"}
onClick={(e) => {
e.stopPropagation();
dispatch({ type: REMOVE_TODO, id });
}}
/>
</div>
</li>
);
}
And the test file is as follows. It keeps saying that toggle is not a function and I am not quite sure why it is doing that. Is there something I need to change differently to make it work?
describe("useToggleState", () => {
it("Initial toggle is true", () => {
const { result } = renderHook(() => UseToggleState(true))
act(() => {
result.current.toggle
})
expect(result.current.state).toBeTruthy()
})
it("Toggle is false", () => {
const { result } = renderHook(() => UseToggleState(false))
act(() => {
result.current.toggle
})
expect(result.current.state).toBeFalsy()
})
})
You test is supposed to be:
describe("useToggleState", () => {
it("Initial toggle is false", () => {
const { result } = renderHook(() => UseToggleState(true))
act(() => {
result.current[1]()
})
expect(result.current[0]).toBe(false)
})
it("Toggle is true", () => {
const { result } = renderHook(() => UseToggleState())
act(() => {
result.current[1]()
})
expect(result.current[0]).toBe(true)
})
})

How can I edit a todo in my react app using hooks?

I'm trying to figure out how to edit a todo item in my react app using hooks, but I can't seem to figure out how to write the code.
Most of the solutions I've seen online are using class components and it's not written with the same logic as my app.
Here is my current code
function TodoList() {
const [todos, setTodos] = useState([]);
const addTodo = todo => {
if (!todo.text || /^\s*$/.test(todo.text)) {
return;
}
const newTodos = [todo, ...todos];
setTodos(newTodos);
console.log(newTodos);
};
const removeTodo = id => {
const removedArr = [...todos].filter(todoId => todoId.id !== id);
setTodos(removedArr);
};
const completeTodo = id => {
let updatedTodos = todos.map(todo => {
if (todo.id === id) {
todo.isComplete = !todo.isComplete;
}
return todo;
});
setTodos(updatedTodos);
};
const editTodo = e => {
setTodos(e.target.value);
};
return (
<>
<TodoForm onSubmit={addTodo} />
{todos.map(todo => (
<div>
<div
key={todo.id}
className={todo.isComplete ? 'complete' : ''}
key={todo.id}
onClick={() => completeTodo(todo.id)}
>
{todo.text}
</div>
<FaWindowClose onClick={() => removeTodo(todo.id)} />
</div>
))}
</>
);
}
Here is the code from the other component
function TodoForm(props) {
const [input, setInput] = useState('');
const handleChange = e => {
setInput(e.target.value);
};
const handleSubmit = e => {
e.preventDefault();
props.onSubmit({
id: Math.floor(Math.random() * 10000),
text: input,
complete: false
});
setInput('');
};
return (
<form onSubmit={handleSubmit}>
<input
placeholder='todo...'
value={input}
onChange={handleChange}
name='text'
/>
<button onClick={handleSubmit}>add todo</button>
</form>
);
}
So right now everything works where I can add todos and delete todos + cross out todos. Only thing missing is being able to edit them.
I saw some suggestions about updating the text value with an input form, but I'm not too sure how I'd implement that in my editTodo function.
Similar to your removeTodo handler, you want to pass the todo.id to completeTodo.
<div className={todo.isComplete ? "complete" : ""} key={todo.id} onClick={() => completeTodo(todo.id)}>
Then you would update a bool value in the todo object.
const completeTodo = (id) => {
let updatedTodos = todos.map(todo => {
if(todo.id === id){
todo.isComplete = true
}
return todo
})
setTodos(updatedTodos)
};
Edit: add styling strikethrough
You'll then conditionally add a css style based on isComplete boolean
CSS
.complete {
text-decoration: line-through;
}
To be able to click on the Remove button, place it outside the todo div in your map function.
{todos.map((todo, isComplete) => (
<>
<div
key={todo.id}
onClick={completeTodo}
className={isComplete ? 'complete' : ''}
>
{todo.text}
</div>
<FaWindowClose onClick={() => removeTodo(todo.id)} />
</>
))}
As discussion with you in another question here it is:
TodoList.js
import React, { useState } from "react";
import TodoForm from "./TodoForm";
import Todo from "./Todo";
function TodoList({ onClick }) {
const [todos, setTodos] = useState([]);
//Track is edit clicked or not
const [editId, setEdit] = useState(false);
//Save input value in input box
const [inputValue, setInputValue] = useState("");
const handleEditChange = (id, text) => {
setEdit(id);
setInputValue(text);
};
const addTodo = (todo) => {
if (!todo.text || /^\s*$/.test(todo.text)) {
return;
}
const newTodos = [todo, ...todos];
setTodos(newTodos);
console.log(newTodos);
};
const removeTodo = (id) => {
const removedArr = [...todos].filter((todoId) => todoId.id !== id);
setTodos(removedArr);
};
const completeTodo = (id) => {
let updatedTodos = todos.map((todo) => {
if (todo.id === id) {
todo.isComplete = !todo.isComplete;
}
return todo;
});
setTodos(updatedTodos);
};
const editTodo = (id, text) => {
let editTodos = todos.map((todo) => {
if (todo.id === id) {
todo.text = text;
}
return todo;
});
setTodos(editTodos);
setEdit(false);
};
return (
<>
<TodoForm onSubmit={addTodo} />
{/* I want to move this code below into a new component called Todo.js */}
<Todo
todos={todos}
completeTodo={completeTodo}
removeTodo={removeTodo}
editTodo={editTodo}
handleEditChange={handleEditChange}
editId={editId}
inputValue={inputValue}
setInputValue={setInputValue}
/>
</>
);
}
export default TodoList;
Todo.js
// I want to move this code into this component
import React, { useState } from "react";
import { FaWindowClose, FaRegEdit } from "react-icons/fa";
const Todo = ({
todos,
completeTodo,
removeTodo,
editTodo,
editId,
handleEditChange,
inputValue,
setInputValue
}) => {
return todos.map((todo) => (
<div className="todo-row">
{editId === todo.id ? (
<input
type="text"
value={inputValue}
onChange={(e) => setInputValue(e.target.value)}
/>
) : (
<div
key={todo.id}
className={todo.isComplete ? "complete" : ""}
onClick={() => completeTodo(todo.id)}
>
{todo.text}
</div>
)}
{editId === todo.id ? (
<button onClick={() => editTodo(todo.id, inputValue)}>Edit todo</button>
) : (
<>
<FaWindowClose onClick={() => removeTodo(todo.id)} />
<FaRegEdit onClick={() => handleEditChange(todo.id, todo.text)} />
</>
)}
</div>
));
};
export default Todo;
Make sure you read and understand code first. Logic is pretty simple what you do in completeTodo. You just need to update text part. Tricky part is to open in input. So logic is like track if user click on id set that id. And check if id is there open input with that id value other wise normal one.
Here is demo of this POC: https://codesandbox.io/s/nostalgic-silence-idm21?file=/src/Todo.js:0-1059

Resources