Skip to content
Merged
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
21 changes: 20 additions & 1 deletion packages/neuron-ui/src/widgets/TextField/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useRef, useState } from 'react'
import React, { useCallback, useRef, useState, useEffect } from 'react'
import Alert from 'widgets/Alert'
import { ReactComponent as Edit } from 'widgets/Icons/Edit.svg'
import { EyesClose, EyesOpen } from 'widgets/Icons/icon'
Expand Down Expand Up @@ -55,11 +55,28 @@ const TextField = React.forwardRef(
ref: React.LegacyRef<HTMLDivElement>
) => {
const [isPasswordHidden, setIsPasswordHidden] = useState(true)
const inputRef = useRef<(HTMLTextAreaElement & HTMLInputElement) | null>(null)
const rowsRef = useRef<number>(rows)
const changePasswordHide = useCallback(() => {
setIsPasswordHidden(v => !v)
}, [setIsPasswordHidden])
// FIXME: label should be limited to a string because it has its own semantic meaning
const labelStr = typeof label === 'string' ? label : field

useEffect(() => {
if (rowsRef.current === rows) {
return
}

rowsRef.current = rows
if (!inputRef.current) {
return
}
inputRef.current.focus()
Comment thread
Keith-CY marked this conversation as resolved.
const position = value?.length || 0
inputRef.current.setSelectionRange(position, position)
}, [rows])

return (
<div
className={`${styles.textField} ${stack ? styles.stack : ''} ${className}`}
Expand All @@ -84,6 +101,7 @@ const TextField = React.forwardRef(
<textarea
id={field}
data-field={field}
ref={inputRef}
rows={rows}
value={value}
placeholder={placeholder}
Expand All @@ -100,6 +118,7 @@ const TextField = React.forwardRef(
<input
id={field}
data-field={field}
ref={inputRef}
type={!isPasswordHidden && type === 'password' ? 'text' : type}
value={value}
placeholder={placeholder}
Expand Down