Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: react-related fixes #1050

Merged
merged 2 commits into from
May 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions app/assets/javascripts/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { StartApplication } from './Device/StartApplication'
import { ApplicationGroup } from './UIModels/ApplicationGroup'
import { WebOrDesktopDevice } from './Device/WebOrDesktopDevice'
import { WebApplication } from './UIModels/Application'
import { createRoot } from 'react-dom/client'
import { createRoot, Root } from 'react-dom/client'

let keyCount = 0
const getKey = () => {
Expand All @@ -37,11 +37,6 @@ const getKey = () => {

const RootId = 'app-group-root'

const rootElement = document.createElement('div')
rootElement.id = RootId
const appendedRootNode = document.body.appendChild(rootElement)
const root = createRoot(appendedRootNode)

const startApplication: StartApplication = async function startApplication(
defaultSyncServerHost: string,
device: WebOrDesktopDevice,
Expand All @@ -50,6 +45,7 @@ const startApplication: StartApplication = async function startApplication(
) {
SNLog.onLog = console.log
SNLog.onError = console.error
let root: Root

const onDestroy = () => {
const rootElement = document.getElementById(RootId) as HTMLElement
Expand All @@ -59,6 +55,11 @@ const startApplication: StartApplication = async function startApplication(
}

const renderApp = () => {
const rootElement = document.createElement('div')
rootElement.id = RootId
const appendedRootNode = document.body.appendChild(rootElement)
root = createRoot(appendedRootNode)

root.render(
<ApplicationGroupView
key={getKey()}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { MenuItemType } from '@/Components/Menu/MenuItemType'
import { KeyboardKey } from '@/Services/IOService'
import { ApplicationDescriptor } from '@standardnotes/snjs'
import {
ChangeEventHandler,
FocusEventHandler,
FunctionComponent,
KeyboardEventHandler,
Expand All @@ -29,6 +30,7 @@ const WorkspaceMenuItem: FunctionComponent<Props> = ({
hideOptions,
}) => {
const [isRenaming, setIsRenaming] = useState(false)
const [inputValue, setInputValue] = useState(descriptor.label)
const inputRef = useRef<HTMLInputElement>(null)

useEffect(() => {
Expand All @@ -37,20 +39,21 @@ const WorkspaceMenuItem: FunctionComponent<Props> = ({
}
}, [isRenaming])

const handleChange: ChangeEventHandler<HTMLInputElement> = useCallback((event) => {
setInputValue(event.target.value)
}, [])

const handleInputKeyDown: KeyboardEventHandler = useCallback((event) => {
if (event.key === KeyboardKey.Enter) {
inputRef.current?.blur()
}
}, [])

const handleInputBlur: FocusEventHandler<HTMLInputElement> = useCallback(
(event) => {
const name = event.target.value
renameDescriptor(name)
setIsRenaming(false)
},
[renameDescriptor],
)
const handleInputBlur: FocusEventHandler<HTMLInputElement> = useCallback(() => {
renameDescriptor(inputValue)
setIsRenaming(false)
setInputValue('')
}, [inputValue, renameDescriptor])

return (
<MenuItem
Expand All @@ -64,7 +67,8 @@ const WorkspaceMenuItem: FunctionComponent<Props> = ({
<input
ref={inputRef}
type="text"
value={descriptor.label}
value={inputValue}
onChange={handleChange}
onKeyDown={handleInputKeyDown}
onBlur={handleInputBlur}
/>
Expand Down