-
-
Notifications
You must be signed in to change notification settings - Fork 302
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from premieroctet/feature/focus
remove children autofocus & add Text Input focus
- Loading branch information
Showing
4 changed files
with
60 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,48 @@ | ||
import React from "react"; | ||
import { Input } from "@chakra-ui/core"; | ||
import FormControl from "./FormControl"; | ||
import { useForm } from "../../../hooks/useForm"; | ||
import usePropsSelector from "../../../hooks/usePropsSelector"; | ||
import React, { useRef, useEffect, KeyboardEvent } from 'react' | ||
import { Input } from '@chakra-ui/core' | ||
import FormControl from './FormControl' | ||
import useDispatch from '../../../hooks/useDispatch' | ||
import { useForm } from '../../../hooks/useForm' | ||
import usePropsSelector from '../../../hooks/usePropsSelector' | ||
import { useSelector } from 'react-redux' | ||
import { getInputTextFocused } from '../../../core/selectors/app' | ||
|
||
const ChildrenControl: React.FC = () => { | ||
const { setValueFromEvent } = useForm(); | ||
const children = usePropsSelector("children"); | ||
const dispatch = useDispatch() | ||
const textInput = useRef<HTMLInputElement>(null) | ||
const focusInput = useSelector(getInputTextFocused) | ||
const { setValueFromEvent } = useForm() | ||
const children = usePropsSelector('children') | ||
const onKeyUp = (event: KeyboardEvent) => { | ||
if (event.keyCode === 13 && textInput.current) { | ||
textInput.current.blur() | ||
} | ||
} | ||
useEffect(() => { | ||
if (focusInput && textInput.current) { | ||
textInput.current.focus() | ||
} else if (focusInput === false && textInput.current) { | ||
textInput.current.blur() | ||
} | ||
}, [focusInput]) | ||
|
||
return ( | ||
<FormControl htmlFor="children" label="Text"> | ||
<Input | ||
id="children" | ||
name="children" | ||
autoFocus | ||
size="sm" | ||
value={children || ""} | ||
value={children} | ||
type="text" | ||
onChange={setValueFromEvent} | ||
ref={textInput} | ||
onKeyUp={onKeyUp} | ||
onBlur={() => { | ||
dispatch.app.toggleInputText(false) | ||
}} | ||
/> | ||
</FormControl> | ||
); | ||
}; | ||
) | ||
} | ||
|
||
export default ChildrenControl; | ||
export default ChildrenControl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters