Connected addTask component to store, fixed cursor position on autofocus of text fields

This commit is contained in:
Dmitriy Shishkov 2021-10-17 18:20:49 +03:00
parent 3edf5edaec
commit 0b730b8b2d
No known key found for this signature in database
GPG Key ID: 14358F96FCDD8060
4 changed files with 32 additions and 7 deletions

View File

@ -6,16 +6,27 @@ import TrapFocus from "@mui/material/Unstable_TrapFocus";
import Box from "@mui/material/Box"; import Box from "@mui/material/Box";
import Button from "@mui/material/Button"; import Button from "@mui/material/Button";
import { useAppDispatch, useAppSelector } from "../hooks"; import { useAppDispatch, useAppSelector, useInputValue } from "../hooks";
import { import {
open as openAction, open as openAction,
close as closeAction, close as closeAction,
} from "../store/slices/uiState"; } from "../store/slices/uiState";
import { add } from "../store/slices/todo";
export const AddTask: React.FC = () => { export const AddTask: React.FC = () => {
const open = useAppSelector((state) => state.uiState.addBarOpen); const open = useAppSelector((state) => state.uiState.addBarOpen);
const dispatch = useAppDispatch(); const dispatch = useAppDispatch();
const { value, onChange, submit } = useInputValue("", (submitValue) =>
dispatch(add(submitValue))
);
const save = () => {
submit();
onChange("");
dispatch(closeAction());
};
return ( return (
<TrapFocus open={open}> <TrapFocus open={open}>
<SwipeableDrawer <SwipeableDrawer
@ -46,10 +57,15 @@ export const AddTask: React.FC = () => {
fullWidth fullWidth
placeholder="New task" placeholder="New task"
autoFocus={open} autoFocus={open}
value={value}
onFocus={(e) => {
e.currentTarget.setSelectionRange(value.length, value.length);
}}
onChange={(e) => onChange(e.currentTarget.value)}
multiline multiline
/> />
<Box sx={{ paddingTop: (theme) => theme.spacing(1) }}> <Box sx={{ paddingTop: (theme) => theme.spacing(1) }}>
<Button variant="text" sx={{ float: "right" }}> <Button onClick={save} variant="text" sx={{ float: "right" }}>
Save Save
</Button> </Button>
</Box> </Box>

View File

@ -36,7 +36,10 @@ export const TodoItem: React.FC<TodoItemProps> = ({ task, index }) => {
variant="standard" variant="standard"
value={value} value={value}
autoFocus={true} autoFocus={true}
onChange={onChange} onFocus={(e) => {
e.currentTarget.setSelectionRange(value.length, value.length);
}}
onChange={(e) => onChange(e.currentTarget.value)}
onBlur={() => { onBlur={() => {
setEditing(false); setEditing(false);
submit(); submit();

View File

@ -4,7 +4,7 @@ import { TypedUseSelectorHook, useDispatch, useSelector } from "react-redux";
import { AppDispatch, RootState } from "./store"; import { AppDispatch, RootState } from "./store";
export type UseInputValueReturnT = { export type UseInputValueReturnT = {
onChange: ChangeEventHandler<HTMLInputElement>; onChange: (value: string) => void;
submit: () => void; submit: () => void;
value: string; value: string;
}; };
@ -16,7 +16,7 @@ export const useInputValue = (
const [value, setValue] = useState(initialValue); const [value, setValue] = useState(initialValue);
return { return {
onChange: (e) => setValue(e.currentTarget.value), onChange: (value) => setValue(value),
submit: () => onSubmit(value), submit: () => onSubmit(value),
value, value,
}; };

View File

@ -13,8 +13,14 @@ export const todoSlice = createSlice({
name: "todo", name: "todo",
initialState: initialState, initialState: initialState,
reducers: { reducers: {
add: (state, { payload }: PayloadAction<TaskItemT>) => { add: (state, { payload }: PayloadAction<string>) => {
state.tasks.push(payload); const newId = state.tasks[state.tasks.length - 1]?.id ?? 0;
state.tasks.push({
done: false,
text: payload,
id: newId,
});
}, },
updateText: ( updateText: (
state, state,