diff --git a/packages/connect-form/src/components/advanced-options-tabs/advanced-tab/advanced-tab.tsx b/packages/connect-form/src/components/advanced-options-tabs/advanced-tab/advanced-tab.tsx index c27a3735d5c..4fdafd8d501 100644 --- a/packages/connect-form/src/components/advanced-options-tabs/advanced-tab/advanced-tab.tsx +++ b/packages/connect-form/src/components/advanced-options-tabs/advanced-tab/advanced-tab.tsx @@ -128,6 +128,7 @@ function AdvancedTab({ /> {/* Default Database */} + diff --git a/packages/connect-form/src/components/advanced-options-tabs/advanced-tab/url-options-table.spec.tsx b/packages/connect-form/src/components/advanced-options-tabs/advanced-tab/url-options-table.spec.tsx index c2b98bbe4b0..7242058a4bd 100644 --- a/packages/connect-form/src/components/advanced-options-tabs/advanced-tab/url-options-table.spec.tsx +++ b/packages/connect-form/src/components/advanced-options-tabs/advanced-tab/url-options-table.spec.tsx @@ -42,7 +42,6 @@ describe('UrlOptionsTable', function () { }); it('renders view correctly', function () { - expect(screen.getByTestId('add-url-options-button')).to.exist; expect(screen.getByTestId('url-options-table')).to.exist; }); @@ -66,26 +65,7 @@ describe('UrlOptionsTable', function () { }); }); - it('renders new option entry with no value when user clicks on add new button', function () { - const button = screen.getByTestId('add-url-options-button'); - fireEvent.click(button); - - expect(screen.getByTestId('new-option-table-row')).to.exist; - expect( - screen.getByTestId('new-option-input-field').getAttribute('value') - ).to.equal(''); - }); - - it('renders error message when user clicks twice add new button', function () { - const button = screen.getByTestId('add-url-options-button'); - fireEvent.click(button); - fireEvent.click(button); - expect(screen.findByText('Please complete the existing option.')).to - .exist; - }); - it('renders selected key when user select a key', function () { - fireEvent.click(screen.getByTestId('add-url-options-button')); // Add new entry fireEvent.click(screen.getByText(/select key/i)); // Click select button fireEvent.click(screen.getByText(/appname/i)); // Select the option @@ -109,7 +89,6 @@ describe('UrlOptionsTable', function () { }); it('renders input value when user changes value', function () { - fireEvent.click(screen.getByTestId('add-url-options-button')); fireEvent.change(screen.getByTestId('new-option-input-field'), { target: { value: 'hello' }, }); @@ -123,7 +102,6 @@ describe('UrlOptionsTable', function () { }); it('should update an option - when name changes', function () { - fireEvent.click(screen.getByTestId('add-url-options-button')); // Add new entry fireEvent.click(screen.getByText(/select key/i)); // Click select button fireEvent.click(screen.getByText(/appname/i)); // Select the option @@ -140,6 +118,8 @@ describe('UrlOptionsTable', function () { newKey: 'compressors', value: '', }); + + expect(screen.getByText(/select key/i)).to.exist; // renders an empty new option }); it('should update an option - when value changes', function () { @@ -156,13 +136,9 @@ describe('UrlOptionsTable', function () { }); }); - it('should delete a new option', function () { - fireEvent.click(screen.getByTestId('add-url-options-button')); - fireEvent.click(screen.getByTestId('new-option-delete-button')); - expect(() => { - screen.getByTestId('new-option-table-row'); - }).to.throw; - expect(updateConnectionFormFieldSpy.callCount).to.equal(0); + it('should not render a delete button for a new option', function () { + expect(screen.getByTestId('new-option-table-row')).to.exist; + expect(screen.queryByTestId('new-option-delete-button')).to.not.exist; }); // eslint-disable-next-line mocha/no-setup-in-describe @@ -197,7 +173,6 @@ describe('UrlOptionsTable', function () { it('renders view correctly', function () { expect(screen.getByTestId('url-options-table')).to.exist; - expect(screen.getByTestId('add-url-options-button')).to.exist; }); it('renders new option row and fields', function () { diff --git a/packages/connect-form/src/components/advanced-options-tabs/advanced-tab/url-options-table.tsx b/packages/connect-form/src/components/advanced-options-tabs/advanced-tab/url-options-table.tsx index 2c23df0b7b4..bedfc631021 100644 --- a/packages/connect-form/src/components/advanced-options-tabs/advanced-tab/url-options-table.tsx +++ b/packages/connect-form/src/components/advanced-options-tabs/advanced-tab/url-options-table.tsx @@ -1,19 +1,17 @@ -import React, { ChangeEvent, useEffect, useCallback } from 'react'; +import React, { ChangeEvent, useEffect } from 'react'; import { - spacing, Table, TableHeader, Row, Cell, - Button, IconButton, Icon, TextInput, Select, Option, OptionGroup, - Banner, css, + spacing, } from '@mongodb-js/compass-components'; import ConnectionStringUrl from 'mongodb-connection-string-url'; import type { MongoClientOptions } from 'mongodb'; @@ -22,23 +20,42 @@ import { editableUrlOptions, UrlOption } from '../../../utils/url-options'; import { UpdateConnectionFormField } from '../../../hooks/use-connect-form'; const optionSelectStyles = css({ - width: 300, + width: spacing[5] * 9, +}); + +const optionNameCellStyles = css({ + width: spacing[5] * 9, }); const optionValueCellStyles = css({ + // width: '30%', +}); + +const optionValueCellContentStyles = css({ display: 'flex', flexDirection: 'row', - justifyContent: 'space-between', + justifyContent: 'flex-start', alignItems: 'center', - width: '100%', }); -const addUrlOptionsButtonStyles = css({ - textAlign: 'center', - marginTop: spacing[3], - marginBottom: spacing[2], +const valueInputStyles = css({ + maxWidth: spacing[7], +}); + +const deleteOptionButtonStyle = css({ + marginLeft: spacing[2], }); +function appendEmptyOption( + urlOptions: Partial[] +): Partial[] { + if (!urlOptions.length || urlOptions[urlOptions.length - 1].name) { + return [...urlOptions, { name: undefined, value: undefined }]; + } + + return urlOptions; +} + function getUrlOptions(connectionStringUrl: ConnectionStringUrl): UrlOption[] { let opts: string[] = []; const urlOptions: UrlOption[] = []; @@ -65,39 +82,16 @@ function UrlOptionsTable({ connectionStringUrl: ConnectionStringUrl; }): React.ReactElement { const [options, setOptions] = React.useState[]>([]); - const [errorMessage, setErrorMessage] = React.useState(''); - - // To fix UI issue that removes empty option(name -> undefined) when user - // removes an existing option (name -> defined) [because of the state update] - const [containsEmptyOption, setContainsEmptyOption] = React.useState(false); - useEffect(() => { - const options: Partial[] = getUrlOptions(connectionStringUrl); - if (!options.length || containsEmptyOption) { - options.push({ name: undefined, value: undefined }); - } + const options = appendEmptyOption(getUrlOptions(connectionStringUrl)); setOptions(options); - }, [connectionStringUrl, containsEmptyOption]); - - const addUrlOption = useCallback(() => { - setErrorMessage(''); - // Use case: User clicks on `Add url option` button and then clicked again without completing existing entry. - // Don't add another option in such case - if (options.find(({ name }) => !name)) { - setErrorMessage('Please complete the existing option.'); - return; - } - const newOptions = [...options, { name: undefined, value: undefined }]; - setOptions(newOptions); - setContainsEmptyOption(true); - }, [options]); + }, [connectionStringUrl]); const updateUrlOption = ( currentName?: UrlOption['name'], name?: UrlOption['name'], value?: string ) => { - setErrorMessage(''); const indexOfUpdatedOption = options.findIndex( (option) => option.name === currentName ); @@ -107,7 +101,8 @@ function UrlOptionsTable({ }; const newOptions = [...options]; newOptions[indexOfUpdatedOption] = option; - setOptions(newOptions); + + setOptions(appendEmptyOption(newOptions)); if (name) { updateConnectionFormField({ @@ -122,24 +117,17 @@ function UrlOptionsTable({ }; const deleteUrlOption = (name?: UrlOption['name']) => { - setErrorMessage(''); + if (!name) { + return; + } + const indexOfDeletedOption = options.findIndex( (option) => option.name === name ); const newOptions = [...options]; newOptions.splice(indexOfDeletedOption, 1); - if (newOptions.length === 0) { - newOptions.push({ name: undefined, value: undefined }); - } - setOptions(newOptions); - if (!name) { - return; - } - - if (newOptions.filter((x) => !x.name).length > 0) { - setContainsEmptyOption(true); - } + setOptions(appendEmptyOption(newOptions)); updateConnectionFormField({ type: 'delete-search-param', @@ -164,7 +152,7 @@ function UrlOptionsTable({ datum.name ? `${datum.name}-table-row` : 'new-option-table-row' } > - +