@@ -3,7 +3,7 @@ import { Identity } from 'common/types/responses'
33import { useUpdateIdentityMutation } from 'common/services/useIdentity'
44import Button from './base/forms/Button'
55import ErrorMessage from './ErrorMessage'
6- import classNames from 'classnames '
6+ import GhostInput from './base/forms/GhostInput '
77
88type EditIdentityType = {
99 data : Identity
@@ -13,7 +13,7 @@ type EditIdentityType = {
1313
1414const EditIdentity : FC < EditIdentityType > = ( { data, environmentId } ) => {
1515 const [ alias , setAlias ] = useState ( data . dashboard_alias )
16- const aliasRef = useRef < HTMLSpanElement > ( null )
16+ const aliasRef = useRef < HTMLInputElement > ( null )
1717
1818 useEffect ( ( ) => {
1919 setAlias ( data ?. dashboard_alias )
@@ -22,20 +22,11 @@ const EditIdentity: FC<EditIdentityType> = ({ data, environmentId }) => {
2222 const [ updateIdentity , { error, isLoading } ] = useUpdateIdentityMutation ( { } )
2323
2424 const handleBlur = ( ) => {
25- if ( aliasRef . current ) {
26- const updatedAlias = ( aliasRef . current . textContent || '' )
27- . replace ( / \n / g, ' ' )
28- . trim ( )
29- . toLowerCase ( )
30-
31- aliasRef . current . textContent = alias
32- setAlias ( updatedAlias )
33- onSubmit ( updatedAlias )
34- }
25+ onSubmit ( alias )
3526 }
3627
37- const onSubmit = ( updatedAlias : string ) => {
38- if ( ! isLoading && updatedAlias ) {
28+ const onSubmit = ( updatedAlias ? : string ) => {
29+ if ( ! isLoading ) {
3930 updateIdentity ( {
4031 data : { ...data , dashboard_alias : updatedAlias } ,
4132 environmentId,
@@ -44,26 +35,7 @@ const EditIdentity: FC<EditIdentityType> = ({ data, environmentId }) => {
4435 }
4536
4637 const handleFocus = ( ) => {
47- if ( ! alias ) {
48- aliasRef . current . textContent = '' // Clear the content
49- }
50-
51- // Ensure that aliasRef.current has at least one child node (a text node)
52- if ( aliasRef . current && aliasRef . current . childNodes . length === 0 ) {
53- aliasRef . current . appendChild ( document . createTextNode ( '' ) )
54- }
55-
56- if ( aliasRef . current ) {
57- const selection = window . getSelection ( )
58- const range = document . createRange ( )
59-
60- const textLength = aliasRef . current . textContent ?. length || 0
61- range . setStart ( aliasRef . current . childNodes [ 0 ] , textLength )
62- range . collapse ( true )
63-
64- selection ?. removeAllRanges ( )
65- selection ?. addRange ( range )
66- }
38+ aliasRef . current ?. focus ( )
6739 }
6840
6941 const handleKeyDown = ( e : React . KeyboardEvent < HTMLSpanElement > ) => {
@@ -73,48 +45,27 @@ const EditIdentity: FC<EditIdentityType> = ({ data, environmentId }) => {
7345 }
7446 }
7547
76- const handleInput = ( ) => {
77- if ( aliasRef . current ) {
78- const selection = window . getSelection ( )
79- const range = selection ?. getRangeAt ( 0 )
80- const cursorPosition = range ?. startOffset || 0
81-
82- const lowerCaseText = aliasRef . current . textContent ?. toLowerCase ( ) || ''
83- aliasRef . current . textContent = lowerCaseText
84-
85- // Restore cursor position
86- const newRange = document . createRange ( )
87- newRange . setStart (
88- aliasRef . current . childNodes [ 0 ] ,
89- Math . min ( cursorPosition , lowerCaseText . length ) ,
90- )
91- newRange . collapse ( true )
92-
93- selection ?. removeAllRanges ( )
94- selection ?. addRange ( newRange )
95- }
48+ const handleInput = ( e : React . ChangeEvent < HTMLInputElement > ) => {
49+ const newValue = e . target . value . toLowerCase ( )
50+ setAlias ( newValue . replace ( / \n / g, ' ' ) . trim ( ) )
9651 }
9752
9853 return (
9954 < >
100- < span
55+ < GhostInput
10156 ref = { aliasRef }
102- className = { classNames ( 'fw-normal' , { 'text-muted' : ! alias } ) }
103- contentEditable = { true }
104- suppressContentEditableWarning = { true }
57+ value = { alias }
58+ onChange = { handleInput }
10559 onBlur = { handleBlur }
10660 onKeyDown = { handleKeyDown }
107- onInput = { handleInput }
108- role = 'textbox'
109- aria-label = 'Alias'
110- >
111- { alias || 'None' }
112- </ span >
61+ placeholder = 'None'
62+ />
11363 < Button
11464 disabled = { ! data }
11565 iconSize = { 18 }
11666 theme = 'text'
117- className = 'ms-2 text-primary'
67+ style = { { lineHeight : 'inherit' } }
68+ className = 'text-primary'
11869 iconRightColour = 'primary'
11970 iconRight = { 'edit' }
12071 onClick = { handleFocus }
0 commit comments