Preserve composer focus when adding thread posts (#11605)

This commit is contained in:
DS Boyce
2026-08-31 09:37:55 -07:00
committed by GitHub
parent f32ebdec79
commit 0f121427fb
3 changed files with 63 additions and 1 deletions
+1 -1
View File
@@ -1700,7 +1700,7 @@ let ComposerPost = memo(function ComposerPost({
style={[a.pt_xs]}
richtext={richtext}
placeholder={selectTextInputPlaceholder}
autoFocus={isLastPost}
autoFocus={isActive}
webForceMinHeight={forceMinHeight}
// To avoid overlap with the close button:
hasRightPadding={isPartOfThread}
@@ -0,0 +1,60 @@
import {composerReducer, createComposerState} from './composer'
jest.mock('#/state/gallery', () => ({
createInitialImages: jest.fn(),
}))
jest.mock('#/logger', () => ({
logger: {
warn: jest.fn(),
},
}))
jest.mock('#/state/queries/postgate/util', () => ({
createPostgateRecord: jest.fn(() => ({})),
}))
jest.mock('#/state/queries/threadgate', () => ({
threadgateRecordToAllowUISetting: jest.fn(() => []),
}))
function createState() {
return createComposerState({
initText: undefined,
initMention: undefined,
initImageUris: undefined,
initQuoteUri: undefined,
initInteractionSettings: undefined,
})
}
describe('composerReducer', () => {
describe('add_post', () => {
it('selects the appended post and requests focus', () => {
const state = createState()
const nextState = composerReducer(state, {type: 'add_post'})
expect(nextState.thread.posts).toHaveLength(2)
expect(nextState.activePostIndex).toBe(1)
expect(nextState.mutableNeedsFocusActive).toBe(true)
})
it('selects a post inserted in the middle of a thread', () => {
let state = createState()
state = composerReducer(state, {type: 'add_post'})
state = composerReducer(state, {type: 'add_post'})
const lastPostId = state.thread.posts[2].id
state = composerReducer(state, {
type: 'focus_post',
postId: state.thread.posts[0].id,
})
const nextState = composerReducer(state, {type: 'add_post'})
expect(nextState.thread.posts).toHaveLength(4)
expect(nextState.activePostIndex).toBe(1)
expect(nextState.thread.posts[2].id).not.toBe(lastPostId)
expect(nextState.thread.posts[3].id).toBe(lastPostId)
expect(nextState.mutableNeedsFocusActive).toBe(true)
})
})
})
+2
View File
@@ -253,6 +253,8 @@ export function composerReducer(
return {
...state,
isDirty: true,
activePostIndex: activePostIndex + 1,
mutableNeedsFocusActive: true,
thread: {
...state.thread,
posts: nextPosts,