Skip to content

Commit e2665eb

Browse files
authored
Merge pull request #47 from premieroctet/feature/focus
remove children autofocus & add Text Input focus
2 parents 0b93a46 + f35cf3d commit e2665eb

File tree

4 files changed

+60
-15
lines changed

4 files changed

+60
-15
lines changed
Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,48 @@
1-
import React from "react";
2-
import { Input } from "@chakra-ui/core";
3-
import FormControl from "./FormControl";
4-
import { useForm } from "../../../hooks/useForm";
5-
import usePropsSelector from "../../../hooks/usePropsSelector";
1+
import React, { useRef, useEffect, KeyboardEvent } from 'react'
2+
import { Input } from '@chakra-ui/core'
3+
import FormControl from './FormControl'
4+
import useDispatch from '../../../hooks/useDispatch'
5+
import { useForm } from '../../../hooks/useForm'
6+
import usePropsSelector from '../../../hooks/usePropsSelector'
7+
import { useSelector } from 'react-redux'
8+
import { getInputTextFocused } from '../../../core/selectors/app'
69

710
const ChildrenControl: React.FC = () => {
8-
const { setValueFromEvent } = useForm();
9-
const children = usePropsSelector("children");
11+
const dispatch = useDispatch()
12+
const textInput = useRef<HTMLInputElement>(null)
13+
const focusInput = useSelector(getInputTextFocused)
14+
const { setValueFromEvent } = useForm()
15+
const children = usePropsSelector('children')
16+
const onKeyUp = (event: KeyboardEvent) => {
17+
if (event.keyCode === 13 && textInput.current) {
18+
textInput.current.blur()
19+
}
20+
}
21+
useEffect(() => {
22+
if (focusInput && textInput.current) {
23+
textInput.current.focus()
24+
} else if (focusInput === false && textInput.current) {
25+
textInput.current.blur()
26+
}
27+
}, [focusInput])
1028

1129
return (
1230
<FormControl htmlFor="children" label="Text">
1331
<Input
1432
id="children"
1533
name="children"
16-
autoFocus
1734
size="sm"
18-
value={children || ""}
35+
value={children}
1936
type="text"
2037
onChange={setValueFromEvent}
38+
ref={textInput}
39+
onKeyUp={onKeyUp}
40+
onBlur={() => {
41+
dispatch.app.toggleInputText(false)
42+
}}
2143
/>
2244
</FormControl>
23-
);
24-
};
45+
)
46+
}
2547

26-
export default ChildrenControl;
48+
export default ChildrenControl

src/core/models/app.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ type Overlay = undefined | { rect: DOMRect; id: string; type: ComponentType }
55
export type AppState = {
66
showLayout: boolean
77
showCode: boolean
8+
inputTextFocused: boolean
89
overlay: undefined | Overlay
910
}
1011

1112
const app = createModel({
1213
state: {
1314
showLayout: true,
1415
showCode: false,
16+
inputTextFocused: false,
1517
overlay: undefined,
1618
} as AppState,
1719
reducers: {
@@ -27,6 +29,12 @@ const app = createModel({
2729
showCode: !state.showCode,
2830
}
2931
},
32+
toggleInputText(state: AppState): AppState {
33+
return {
34+
...state,
35+
inputTextFocused: !state.inputTextFocused,
36+
}
37+
},
3038

3139
setOverlay(state: AppState, overlay: Overlay | undefined): AppState {
3240
return {

src/core/selectors/app.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,10 @@ import { RootState } from '../store'
33
export const getShowLayout = (state: RootState) => state.app.showLayout
44

55
export const getShowCode = (state: RootState) => state.app.showCode
6+
7+
export const getFocusedComponent = (id: IComponent['id']) => (
8+
state: RootState,
9+
) => state.app.inputTextFocused && state.components.present.selectedId === id
10+
11+
export const getInputTextFocused = (state: RootState) =>
12+
state.app.inputTextFocused

src/hooks/useInteractive.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
getIsSelectedComponent,
77
getIsHovered,
88
} from '../core/selectors/components'
9-
import { getShowLayout } from '../core/selectors/app'
9+
import { getShowLayout, getFocusedComponent } from '../core/selectors/app'
1010

1111
export const useInteractive = (
1212
component: IComponent,
@@ -16,12 +16,13 @@ export const useInteractive = (
1616
const showLayout = useSelector(getShowLayout)
1717
const isComponentSelected = useSelector(getIsSelectedComponent(component.id))
1818
const isHovered = useSelector(getIsHovered(component.id))
19+
const focusInput = useSelector(getFocusedComponent(component.id))
20+
1921
const [, drag] = useDrag({
2022
item: { id: component.id, type: component.type, isMoved: true },
2123
})
2224

2325
const ref = useRef<HTMLDivElement>(null)
24-
2526
let props = {
2627
...component.props,
2728
onMouseOver: (event: MouseEvent) => {
@@ -36,6 +37,13 @@ export const useInteractive = (
3637
event.stopPropagation()
3738
dispatch.components.select(component.id)
3839
},
40+
onDoubleClick: (event: MouseEvent) => {
41+
event.preventDefault()
42+
event.stopPropagation()
43+
if (focusInput === false) {
44+
dispatch.app.toggleInputText()
45+
}
46+
},
3947
}
4048

4149
if (showLayout && enableVisualHelper) {
@@ -49,7 +57,7 @@ export const useInteractive = (
4957
if (isHovered || isComponentSelected) {
5058
props = {
5159
...props,
52-
boxShadow: `#4FD1C5 0px 0px 0px 2px inset`,
60+
boxShadow: `${focusInput ? '#ffc4c7' : '#4FD1C5'} 0px 0px 0px 2px inset`,
5361
}
5462
}
5563

0 commit comments

Comments
 (0)