1- import { css , cx } from '@emotion/css' ;
1+ import { css } from '@emotion/css' ;
22import React , { ChangeEvent , Fragment , useRef , useReducer } from 'react' ;
33import {
44 Icon ,
55 IconButton ,
66 Label ,
77 TextArea ,
8+ Toggle ,
89 spacing ,
910} from '@mongodb-js/compass-components' ;
1011import ConfirmEditConnectionString from './confirm-edit-connection-string' ;
1112import ConnectionStringUrl from 'mongodb-connection-string-url' ;
1213
13- const labelStyles = css ( {
14+ const uriLabelStyles = css ( {
1415 padding : 0 ,
1516 margin : 0 ,
16- marginTop : spacing [ 3 ] ,
17+ flexGrow : 1 ,
1718} ) ;
1819
1920const infoButtonStyles = css ( {
2021 verticalAlign : 'middle' ,
2122 marginTop : - spacing [ 1 ] ,
2223} ) ;
2324
24- const connectionStringEditDisabled = css ( {
25- textarea : {
26- paddingRight : spacing [ 5 ] ,
27- } ,
28- } ) ;
29-
3025const textAreaContainerStyle = css ( {
3126 position : 'relative' ,
3227} ) ;
3328
34- const editConnectionStringStyles = css ( {
35- position : 'absolute' ,
36- right : spacing [ 1 ] ,
37- top : spacing [ 1 ] ,
38- } ) ;
39-
4029const connectionStringStyles = css ( {
4130 textarea : {
4231 minHeight : spacing [ 7 ] ,
4332 resize : 'vertical' ,
4433 } ,
4534} ) ;
4635
36+ const editToggleStyles = css ( {
37+ height : 14 ,
38+ width : 26 ,
39+ margin : spacing [ 1 ] ,
40+ marginRight : 0 ,
41+ } ) ;
42+
43+ const editToggleLabelStyles = css ( {
44+ '&:hover' : {
45+ cursor : 'pointer' ,
46+ } ,
47+ } ) ;
48+
49+ const textAreaLabelContainerStyles = css ( {
50+ marginTop : spacing [ 3 ] ,
51+ display : 'flex' ,
52+ flexDirection : 'row' ,
53+ } ) ;
54+
4755const connectionStringInputId = 'connectionString' ;
4856
4957type State = {
@@ -54,7 +62,8 @@ type State = {
5462type Action =
5563 | { type : 'enable-editing-connection-string' }
5664 | { type : 'show-edit-connection-string-confirmation' }
57- | { type : 'hide-edit-connection-string-confirmation' } ;
65+ | { type : 'hide-edit-connection-string-confirmation' }
66+ | { type : 'hide-connection-string' } ;
5867
5968function reducer ( state : State , action : Action ) : State {
6069 switch ( action . type ) {
@@ -69,6 +78,12 @@ function reducer(state: State, action: Action): State {
6978 ...state ,
7079 showConfirmEditConnectionStringPrompt : true ,
7180 } ;
81+ case 'hide-connection-string' :
82+ return {
83+ ...state ,
84+ showConfirmEditConnectionStringPrompt : false ,
85+ enableEditingConnectionString : false ,
86+ } ;
7287 case 'hide-edit-connection-string-confirmation' :
7388 return {
7489 ...state ,
@@ -129,48 +144,58 @@ function ConnectStringInput({
129144
130145 return (
131146 < Fragment >
132- < Label className = { labelStyles } htmlFor = { connectionStringInputId } >
133- Connection String
134- < IconButton
135- className = { infoButtonStyles }
136- aria-label = "Connection String Documentation"
137- data-testid = "connectionStringDocsButton"
138- href = "https://docs.mongodb.com/manual/reference/connection-string/"
139- target = "_blank"
147+ < div className = { textAreaLabelContainerStyles } >
148+ < Label className = { uriLabelStyles } htmlFor = { connectionStringInputId } >
149+ Connection String
150+ < IconButton
151+ className = { infoButtonStyles }
152+ aria-label = "Connection String Documentation"
153+ data-testid = "connectionStringDocsButton"
154+ href = "https://docs.mongodb.com/manual/reference/connection-string/"
155+ target = "_blank"
156+ >
157+ < Icon glyph = "InfoWithCircle" size = "small" />
158+ </ IconButton >
159+ </ Label >
160+ < label
161+ className = { editToggleLabelStyles }
162+ id = "edit-connection-string-label"
163+ htmlFor = "toggle-edit-connection-string"
140164 >
141- < Icon glyph = "InfoWithCircle" size = "small" />
142- </ IconButton >
143- </ Label >
165+ Edit Connection String
166+ </ label >
167+ < Toggle
168+ className = { editToggleStyles }
169+ id = "toggle-edit-connection-string"
170+ aria-labelledby = "edit-connection-string-label"
171+ size = "xsmall"
172+ checked = { enableEditingConnectionString }
173+ onClick = { ( ) => {
174+ if ( enableEditingConnectionString ) {
175+ dispatch ( {
176+ type : 'hide-connection-string' ,
177+ } ) ;
178+ return ;
179+ }
180+ dispatch ( {
181+ type : 'show-edit-connection-string-confirmation' ,
182+ } ) ;
183+ } }
184+ />
185+ </ div >
144186 < div className = { textAreaContainerStyle } >
145187 < TextArea
146188 onChange = { ( event : ChangeEvent < HTMLTextAreaElement > ) => {
147189 setConnectionString ( event . target . value ) ;
148190 } }
149191 value = { displayedConnectionString }
150- className = { cx (
151- connectionStringStyles ,
152- enableEditingConnectionString ? null : connectionStringEditDisabled
153- ) }
192+ className = { connectionStringStyles }
154193 disabled = { ! enableEditingConnectionString }
155194 id = { connectionStringInputId }
156195 ref = { textAreaEl }
157196 aria-labelledby = "Connection String"
158197 placeholder = "e.g mongodb+srv://username:[email protected] /admin" 159198 />
160- { ! enableEditingConnectionString && (
161- < IconButton
162- className = { editConnectionStringStyles }
163- aria-label = "Edit Connection String"
164- data-testid = "enableEditConnectionStringButton"
165- onClick = { ( ) =>
166- dispatch ( {
167- type : 'show-edit-connection-string-confirmation' ,
168- } )
169- }
170- >
171- < Icon glyph = "Edit" size = "small" />
172- </ IconButton >
173- ) }
174199 < ConfirmEditConnectionString
175200 open = { showConfirmEditConnectionStringPrompt }
176201 onCancel = { ( ) =>
0 commit comments