Skip to content

Commit c124e16

Browse files
ToMESSKaHerrTopi
authored andcommitted
fix(ui-text-input): make TextInput maintain focus when renderAfterInput is conditionally rendered
INSTUI-4466
1 parent 308bb2f commit c124e16

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed

cypress/component/TextInput.cy.tsx

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2222
* SOFTWARE.
2323
*/
24-
import React from 'react'
25-
import { TextInput } from '../../packages/ui'
24+
import React, { useState } from 'react'
25+
import { TextInput } from '@instructure/ui'
2626

2727
import '../support/component'
2828
import 'cypress-real-events'
@@ -110,4 +110,43 @@ describe('<TextInput/>', () => {
110110
'12px'
111111
)
112112
})
113+
114+
it('should maintain focus while typing when after-content is conditionally rendered', () => {
115+
const TextTextInput = () => {
116+
const [value, setValue] = useState('')
117+
118+
const renderAfterInput = () => {
119+
if (!value) return
120+
return <div>Hello!</div>
121+
}
122+
123+
return (
124+
<TextInput
125+
value={value}
126+
onChange={(event, newValue) => {
127+
setValue(newValue)
128+
}}
129+
renderAfterInput={renderAfterInput}
130+
/>
131+
)
132+
}
133+
cy.mount(<TextTextInput />)
134+
135+
cy.get('input').as('textInput').click().should('be.focused')
136+
cy.get('[class*="textInput__afterElement"]').should('not.exist')
137+
138+
cy.get('@textInput')
139+
.type('a')
140+
.should(($input) => {
141+
expect($input).to.have.value('a')
142+
})
143+
.should('have.focus')
144+
145+
cy.get('[class*="textInput__afterElement"]').should('contain', 'Hello!')
146+
147+
cy.get('@textInput')
148+
.type('bc')
149+
.should('have.value', 'abc')
150+
.and('be.focused')
151+
})
113152
})

packages/ui-text-input/src/TextInput/index.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,15 @@ class TextInput extends Component<TextInputProps, TextInputState> {
145145
makeStyleProps = (): TextInputStyleProps => {
146146
const { interaction } = this
147147
const { hasFocus, afterElementHasWidth } = this.state
148+
const beforeElement = this.props.renderBeforeInput
149+
? callRenderProp(this.props.renderBeforeInput)
150+
: null
148151
return {
149152
disabled: interaction === 'disabled',
150153
invalid: this.invalid,
151154
focused: hasFocus,
152155
afterElementHasWidth: afterElementHasWidth,
153-
beforeElementExists: this.props.renderBeforeInput != undefined
156+
beforeElementExists: !!beforeElement
154157
}
155158
}
156159

@@ -309,7 +312,11 @@ class TextInput extends Component<TextInputProps, TextInputState> {
309312
? callRenderProp(renderAfterInput)
310313
: null
311314

312-
const renderBeforeOrAfter = !!beforeElement || !!afterElement
315+
const renderBeforeOrAfter =
316+
!!beforeElement ||
317+
!!afterElement ||
318+
renderBeforeInput !== undefined ||
319+
renderAfterInput !== undefined
313320

314321
const rawLabel = callRenderProp(renderLabel)
315322
const label = hasVisibleChildren(rawLabel) ? (

0 commit comments

Comments
 (0)