Skip to content

Commit

Permalink
feat: Make token editor form submit on onSubmit event (#2531)
Browse files Browse the repository at this point in the history
  • Loading branch information
georgylobko committed Aug 2, 2024
1 parent 6796f15 commit c4a96c6
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,7 @@ Object {
"awsui_token-editor-field-operator_1heb1",
"awsui_token-editor-field-property_1heb1",
"awsui_token-editor-field-value_1heb1",
"awsui_token-editor-form_1wzqe",
"awsui_token-editor-submit_1heb1",
],
"radio-group": Array [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

import React from 'react';
import { act, render } from '@testing-library/react';
import { act, fireEvent, render } from '@testing-library/react';

import PropertyFilter from '../../../lib/components/property-filter';
import {
Expand Down Expand Up @@ -90,6 +90,7 @@ function findEditor(tokenIndex: number, { expandToViewport }: { expandToViewport
valueAutosuggest: editor.findValueField().findControl()!.findAutosuggest()!,
cancelButton: editor.findCancelButton(),
submitButton: editor.findSubmitButton(),
form: editor.findForm(),
}
: null;
}
Expand Down Expand Up @@ -255,5 +256,27 @@ describe.each([false, true])('token editor, expandToViewport=%s', expandToViewpo
})
);
});

test('form submit closes the popover and saves the changes', () => {
const handleChange = jest.fn();
renderComponent({
onChange: handleChange,
query: { tokens: [{ propertyKey: 'string', value: 'first', operator: '!:' }], operation: 'or' },
});
const editor = openEditor(0, { expandToViewport });

act(() => editor.operatorSelect.openDropdown());
act(() => editor.operatorSelect.selectOption(1));
act(() => editor.propertySelect.openDropdown());
act(() => editor.propertySelect.selectOption(1));
act(() => editor.valueAutosuggest.setInputValue('123'));
fireEvent.submit(editor.form!.getElement());
expect(findEditor(0, { expandToViewport })).toBeNull();
expect(handleChange).toHaveBeenCalledWith(
expect.objectContaining({
detail: { operation: 'or', tokens: [{ operator: ':', propertyKey: undefined, value: '123' }] },
})
);
});
});
});
21 changes: 14 additions & 7 deletions src/property-filter/token-editor.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import React from 'react';
import React, { FormEvent } from 'react';
import clsx from 'clsx';

import InternalAutosuggest from '../autosuggest/internal';
Expand Down Expand Up @@ -225,9 +225,19 @@ export function TokenEditor({
onChangeTemporaryToken({ ...temporaryToken, value: newValue });
};

const onApply = () => {
setToken(matchTokenValue(temporaryToken, filteringOptions));
onDismiss();
};

const onFormSubmit = (event: FormEvent) => {
event.preventDefault();
onApply();
};

return (
<div className={styles['token-editor']}>
<div className={styles['token-editor-form']}>
<form className={styles['token-editor-form']} onSubmit={onFormSubmit}>
<InternalFormField
label={i18nStrings.propertyText}
className={clsx(styles['token-editor-field-property'], testUtilStyles['token-editor-field-property'])}
Expand Down Expand Up @@ -272,7 +282,7 @@ export function TokenEditor({
i18nStrings={i18nStrings}
/>
</InternalFormField>
</div>
</form>

<div className={styles['token-editor-actions']}>
<InternalButton
Expand All @@ -286,10 +296,7 @@ export function TokenEditor({
<InternalButton
className={clsx(styles['token-editor-submit'], testUtilStyles['token-editor-submit'])}
formAction="none"
onClick={() => {
setToken(matchTokenValue(temporaryToken, filteringOptions));
onDismiss();
}}
onClick={onApply}
>
{i18nStrings.applyActionText}
</InternalButton>
Expand Down
4 changes: 4 additions & 0 deletions src/test-utils/dom/property-filter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ export class PropertyFilterEditorDropdownWrapper extends ComponentWrapper {
return this.findComponent(`.${popoverStyles['dismiss-control']}`, ButtonWrapper)!;
}

findForm(): ElementWrapper {
return this.findByClassName(styles['token-editor-form'])!;
}

findPropertyField(): FormFieldWrapper {
return this.findComponent(`.${testUtilStyles['token-editor-field-property']}`, FormFieldWrapper)!;
}
Expand Down

0 comments on commit c4a96c6

Please sign in to comment.