Use selected topic when clearing publish topic, also focus input element

This commit is contained in:
Thomas Nordquist
2020-04-15 22:23:18 +02:00
parent 28743ba646
commit e24e505cc0
3 changed files with 13 additions and 8 deletions

View File

@@ -34,7 +34,7 @@ export const setEditorMode = (editorMode: string): Action => {
export const publish = (connectionId: string) => (dispatch: Dispatch<Action>, getState: () => AppState) => { export const publish = (connectionId: string) => (dispatch: Dispatch<Action>, getState: () => AppState) => {
const state = getState() const state = getState()
const topic = state.publish.topic const topic = state.publish.manualTopic ?? state.tree.get('selectedTopic')?.path()
if (!topic) { if (!topic) {
return return

View File

@@ -1,18 +1,21 @@
import ClearAdornment from '../../helper/ClearAdornment' import ClearAdornment from '../../helper/ClearAdornment'
import React, { useCallback, useMemo } from 'react' import React, { useCallback, useMemo, useRef } from 'react'
import { FormControl, Input, InputLabel } from '@material-ui/core' import { FormControl, Input, InputLabel } from '@material-ui/core'
import { publishActions } from '../../../actions' import { publishActions } from '../../../actions'
import { bindActionCreators } from 'redux' import { bindActionCreators } from 'redux'
import { AppState } from '../../../reducers' import { AppState } from '../../../reducers'
import { connect } from 'react-redux' import { connect } from 'react-redux'
function TopicInput(props: { actions: typeof publishActions; topic?: string }) { function TopicInput(props: { actions: typeof publishActions; manualTopic?: string; selectedTopic?: string }) {
const inputElement = useRef<HTMLInputElement>(null)
const updateTopic = useCallback((e: React.ChangeEvent<HTMLInputElement>) => { const updateTopic = useCallback((e: React.ChangeEvent<HTMLInputElement>) => {
props.actions.setTopic(e.target.value) props.actions.setTopic(e.target.value)
}, []) }, [])
const clearTopic = useCallback(() => { const clearTopic = useCallback(() => {
props.actions.setTopic('') props.actions.setTopic('')
inputElement.current?.focus()
}, []) }, [])
const onTopicBlur = useCallback((e: React.FocusEvent<HTMLInputElement>) => { const onTopicBlur = useCallback((e: React.FocusEvent<HTMLInputElement>) => {
@@ -21,7 +24,7 @@ function TopicInput(props: { actions: typeof publishActions; topic?: string }) {
} }
}, []) }, [])
const topicStr = props.topic || '' const topicStr = props.manualTopic || ''
return useMemo( return useMemo(
() => ( () => (
@@ -29,6 +32,7 @@ function TopicInput(props: { actions: typeof publishActions; topic?: string }) {
<FormControl style={{ width: '100%' }}> <FormControl style={{ width: '100%' }}>
<InputLabel htmlFor="publish-topic">Topic</InputLabel> <InputLabel htmlFor="publish-topic">Topic</InputLabel>
<Input <Input
inputRef={inputElement}
id="publish-topic" id="publish-topic"
value={topicStr} value={topicStr}
startAdornment={<span />} startAdornment={<span />}
@@ -36,7 +40,7 @@ function TopicInput(props: { actions: typeof publishActions; topic?: string }) {
onBlur={onTopicBlur} onBlur={onTopicBlur}
onChange={updateTopic} onChange={updateTopic}
multiline={false} multiline={false}
placeholder="example/topic" placeholder={props.selectedTopic ?? 'example/topic'}
/> />
</FormControl> </FormControl>
</div> </div>
@@ -53,7 +57,8 @@ const mapDispatchToProps = (dispatch: any) => {
const mapStateToProps = (state: AppState) => { const mapStateToProps = (state: AppState) => {
return { return {
topic: state.publish.topic, manualTopic: state.publish.manualTopic,
selectedTopic: state.tree.get('selectedTopic')?.path(),
} }
} }

View File

@@ -1,7 +1,7 @@
import { createReducer } from './lib' import { createReducer } from './lib'
export interface PublishState { export interface PublishState {
topic?: string manualTopic?: string
payload?: string payload?: string
retain: boolean retain: boolean
editorMode: string editorMode: string
@@ -59,7 +59,7 @@ export const publishReducer = createReducer(initialState, {
function setTopic(state: PublishState, action: SetTopic) { function setTopic(state: PublishState, action: SetTopic) {
return { return {
...state, ...state,
topic: action.topic, manualTopic: action.topic,
} }
} }