OpenMetadata icon indicating copy to clipboard operation
OpenMetadata copied to clipboard

Add support for creating custom options in Autocomplete

Open Rohit0301 opened this issue 5 months ago โ€ข 3 comments

Describe your changes:

Fixes 29443

I worked on ... because ...

Type of change:

  • [ ] Bug fix
  • [ ] Improvement
  • [ ] New feature
  • [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • [ ] Documentation

High-level design:

N/A โ€” small change.

Tests:

Use cases covered

Unit tests

Backend integration tests

Ingestion integration tests

Playwright (UI) tests

Manual testing performed

UI screen recording / screenshots:

Not applicable.

Checklist:

  • [x] I have read the CONTRIBUTING document.
  • [ ] My PR title is Fixes <issue-number>: <short explanation>
  • [ ] My PR is linked to a GitHub issue via Fixes #<issue-number> above.
  • [ ] I have commented on my code, particularly in hard-to-understand areas.
  • [ ] For JSON Schema changes: I updated the migration scripts or explained why it is not needed.
  • [ ] For UI changes: I attached a screen recording and/or screenshots above.
  • [ ] I have added tests (unit / integration / Playwright as applicable) and listed them above.

Greptile Summary

This PR adds an allowsCreation prop to the Autocomplete component that lets users type a custom value and commit it (via Enter or blur) as a new selectable item, wiring the feature through FieldPropsMap and renderFieldElement. It also adds the missing onFocus forwarding to several field types (Input, TextArea, Toggle, Checkbox, Select).

  • Core creation logic (autocomplete.tsx): Introduces onCreateItem, an Enter-key guard, and an onBlur handler; has multiple interaction bugs where creation fires incorrectly when the dropdown is open or when the user clicks an existing item (see inline comments).
  • Form field wiring (render-field-element.tsx): Correctly falls back to a synthetic item in handleInsert when allowsCreation is set and the key is not in the item list; onFocus forwarding additions are clean.
  • Stories (Autocomplete.stories.tsx): Three representative stories cover multi-select, single-select, and pre-selected scenarios.

Confidence Score: 3/5

The new allowsCreation feature has interaction bugs that will cause wrong items to be selected or duplicates to be created in normal keyboard and mouse workflows; needs fixes before merging.

Three distinct defects exist in the core creation flow: the Enter handler intercepts keyboard selection of highlighted dropdown items, the blur handler fires before click-based selection commits (creating a spurious item and blocking the intended one in single-select mode), and onCreateItem does not respect the multiple={false} cap. Each bug is reachable through the primary user interactions the feature is designed to support.

autocomplete.tsx โ€” all three issues are in the InnerAutocomplete keydown/blur handlers and the AutocompleteBase.onCreateItem callback.

Important Files Changed

Filename Overview
openmetadata-ui-core-components/src/main/resources/ui/src/components/base/autocomplete/autocomplete.tsx Adds allowsCreation prop with Enter-key and blur-triggered item creation; has three behavioral bugs: Enter intercepts existing-item selection when the dropdown is open, blur fires before listbox click selection causing wrong item creation, and onCreateItem does not enforce the multiple={false} limit.
openmetadata-ui-core-components/src/main/resources/ui/src/components/application/form-field/render-field-element.tsx Wires allowsCreation into handleInsert fallback and correctly passes onFocus to previously-missing field types (Input, TextArea, Toggle, Checkbox, Select); logic is straightforward and correct.
openmetadata-ui-core-components/src/main/resources/ui/src/components/application/form-field/form-field.types.ts Adds allowsCreation?: boolean to FieldPropsMap; trivial type-only change, no issues.
openmetadata-ui-core-components/src/main/resources/ui/src/stories/Autocomplete.stories.tsx Adds three Storybook stories for the new allowsCreation feature; stories are well-structured and cover multi-select, single-select, and pre-selected scenarios.

Sequence Diagram

%%{init: {'theme': 'neutral'}}%%
sequenceDiagram
    participant User
    participant AriaInput
    participant InnerAutocomplete
    participant AutocompleteBase
    participant Dropdown

    Note over User,Dropdown: Happy path โ€” user types & presses Enter (dropdown closed)
    User->>AriaInput: type "foo"
    AriaInput->>AutocompleteBase: onInputChange("foo")
    User->>AriaInput: keydown Enter
    AriaInput->>InnerAutocomplete: handleInputKeyDown(Enter)
    InnerAutocomplete->>AutocompleteBase: onCreateItem("foo")
    AutocompleteBase->>AutocompleteBase: add to allItems + internalSelected

    Note over User,Dropdown: Bug path โ€” user types & presses Enter (dropdown open + item highlighted)
    User->>AriaInput: type "rea"
    AriaInput->>Dropdown: opens, highlights "React"
    User->>AriaInput: keydown Enter
    AriaInput->>InnerAutocomplete: handleInputKeyDown(Enter)
    InnerAutocomplete-->>AutocompleteBase: onCreateItem("rea") โ€” wrong: creates custom item
    Dropdown-->>AutocompleteBase: onSelectionChange("React") โ€” also fires (react-aria)

    Note over User,Dropdown: Bug path โ€” user types & clicks existing dropdown item
    User->>AriaInput: type "foo"
    User->>Dropdown: click "Foo"
    Dropdown->>AriaInput: pointerdown triggers blur on input
    AriaInput->>InnerAutocomplete: "onBlur(value="foo")"
    InnerAutocomplete-->>AutocompleteBase: onCreateItem("foo") โ€” spurious creation
    Dropdown->>AutocompleteBase: onSelectionChange("Foo") โ€” blocked in single-select
%%{init: {'theme': 'base', 'themeVariables': {"darkMode": true, "background": "#0d1117", "primaryColor": "#21262d", "primaryTextColor": "#e6edf3", "primaryBorderColor": "#8b949e", "lineColor": "#8b949e", "textColor": "#e6edf3", "edgeLabelBackground": "#161b22", "actorBkg": "#21262d", "actorBorder": "#8b949e", "actorTextColor": "#e6edf3", "actorLineColor": "#8b949e", "signalColor": "#8b949e", "signalTextColor": "#e6edf3", "noteBkgColor": "#373320", "noteBorderColor": "#d4a72c", "noteTextColor": "#f0e6c0", "labelBoxBkgColor": "#21262d", "labelBoxBorderColor": "#8b949e", "labelTextColor": "#e6edf3", "loopTextColor": "#e6edf3", "activationBkgColor": "#30363d", "activationBorderColor": "#8b949e"}}}%%
sequenceDiagram
    participant User
    participant AriaInput
    participant InnerAutocomplete
    participant AutocompleteBase
    participant Dropdown

    Note over User,Dropdown: Happy path โ€” user types & presses Enter (dropdown closed)
    User->>AriaInput: type "foo"
    AriaInput->>AutocompleteBase: onInputChange("foo")
    User->>AriaInput: keydown Enter
    AriaInput->>InnerAutocomplete: handleInputKeyDown(Enter)
    InnerAutocomplete->>AutocompleteBase: onCreateItem("foo")
    AutocompleteBase->>AutocompleteBase: add to allItems + internalSelected

    Note over User,Dropdown: Bug path โ€” user types & presses Enter (dropdown open + item highlighted)
    User->>AriaInput: type "rea"
    AriaInput->>Dropdown: opens, highlights "React"
    User->>AriaInput: keydown Enter
    AriaInput->>InnerAutocomplete: handleInputKeyDown(Enter)
    InnerAutocomplete-->>AutocompleteBase: onCreateItem("rea") โ€” wrong: creates custom item
    Dropdown-->>AutocompleteBase: onSelectionChange("React") โ€” also fires (react-aria)

    Note over User,Dropdown: Bug path โ€” user types & clicks existing dropdown item
    User->>AriaInput: type "foo"
    User->>Dropdown: click "Foo"
    Dropdown->>AriaInput: pointerdown triggers blur on input
    AriaInput->>InnerAutocomplete: "onBlur(value="foo")"
    InnerAutocomplete-->>AutocompleteBase: onCreateItem("foo") โ€” spurious creation
    Dropdown->>AutocompleteBase: onSelectionChange("Foo") โ€” blocked in single-select

Reviews (1): Last reviewed commit: "Add support for creating custom options ..." | Re-trigger Greptile

Greptile also left 3 inline comments on this PR.

Rohit0301 avatar Jun 24 '26 19:06 Rohit0301

๐ŸŸก Playwright Results โ€” all passed (22 flaky)

โœ… 4448 passed ยท โŒ 0 failed ยท ๐ŸŸก 22 flaky ยท โญ๏ธ 88 skipped

Shard Passed Failed Flaky Skipped
๐ŸŸก Shard 1 323 0 2 4
๐ŸŸก Shard 2 830 0 7 9
๐ŸŸก Shard 3 836 0 3 7
๐ŸŸก Shard 4 833 0 3 10
๐ŸŸก Shard 5 833 0 1 50
๐ŸŸก Shard 6 793 0 6 8
๐ŸŸก 22 flaky test(s) (passed on retry)
  • Pages/SearchSettings.spec.ts โ€บ Preview config reflects reverted n-gram weight after save (shard 1, 1 retry)
  • Flow/SearchRBAC.spec.ts โ€บ a fully denied user sees neither asset type when browsing (shard 1, 2 retries)
  • Features/BulkEditEntity.spec.ts โ€บ Glossary (shard 2, 1 retry)
  • Features/BulkEditOperationBadges.spec.ts โ€บ Glossary bulk edit search filters rows and clear restores them (shard 2, 1 retry)
  • Features/BulkEditOperationBadges.spec.ts โ€บ Database service bulk edit search filters rows and clear restores them (shard 2, 1 retry)
  • Features/BulkImport.spec.ts โ€บ Database Schema (shard 2, 1 retry)
  • Features/BulkImportWithDotInName.spec.ts โ€บ CSV with quoted FQN loads correctly in import grid (shard 2, 1 retry)
  • Features/DataQuality/DataQuality.spec.ts โ€บ Table test case (shard 2, 1 retry)
  • Features/DataQuality/TestLibrary.spec.ts โ€บ should maintain page on edit and reset to first page on delete (shard 2, 1 retry)
  • Features/SearchExport.spec.ts โ€บ Export queues a background job and downloads from the jobs tray (shard 3, 1 retry)
  • Features/Table.spec.ts โ€บ Table pagination with sorting should works (shard 3, 1 retry)
  • Features/Tasks/TaskNavigation.spec.ts โ€บ navigating to /table/TASK-XXXXX should show 404 (invalid URL pattern) (shard 3, 1 retry)
  • Flow/PersonaFlow.spec.ts โ€บ Set default persona for team should work properly (shard 4, 1 retry)
  • Pages/CustomProperties.spec.ts โ€บ Date (shard 4, 1 retry)
  • Pages/CustomProperties.spec.ts โ€บ Should clear search and show all properties for apiCollection in right panel (shard 4, 1 retry)
  • Pages/ExplorePageRightPanel_KnowledgeCenter.spec.ts โ€บ Should remove user owner for knowledgeCenter (shard 5, 1 retry)
  • Pages/GlossaryImportExport.spec.ts โ€บ Check for Circular Reference in Glossary Import (shard 6, 1 retry)
  • Pages/GlossaryImportExport.spec.ts โ€บ Import partial success - some terms pass, some fail (shard 6, 1 retry)
  • Pages/Lineage/LineageFilters.spec.ts โ€บ Verify lineage schema filter selection (shard 6, 1 retry)
  • Pages/Lineage/LineageRightPanel.spec.ts โ€บ Verify custom properties tab IS visible for supported type: searchIndex (shard 6, 1 retry)
  • Pages/Lineage/PlatformLineage.spec.ts โ€บ Verify domain platform view (shard 6, 1 retry)
  • Pages/ServiceEntity.spec.ts โ€บ Announcement create, edit & delete (shard 6, 1 retry)

๐Ÿ“ฆ Download artifacts

How to debug locally
# Download playwright-test-results-<shard> artifact and unzip
npx playwright show-trace path/to/trace.zip    # view trace

github-actions[bot] avatar Jun 24 '26 22:06 github-actions[bot]

Jest test Coverage

UI tests summary

Lines Statements Branches Functions
Coverage: 63%
63.42% (70680/111437) 45.9% (40625/88504) 47.81% (12367/25864)

github-actions[bot] avatar Jun 25 '26 12:06 github-actions[bot]

Code Review โœ… Approved 5 resolved / 5 findings

Adds allowsCreation and hideDropdown functionality to the Autocomplete component to support custom free-form options. All reported issues, including spurious item creation on blur and side effects within state updates, have been resolved.

โœ… 5 resolved
โœ… Bug: Enter always creates new item, blocking selection of highlighted option

๐Ÿ“„ openmetadata-ui-core-components/src/main/resources/ui/src/components/base/autocomplete/autocomplete.tsx:139-153 In handleInputKeyDown, when allowsCreation is true and the input is non-empty, pressing Enter unconditionally calls event.preventDefault() and context.onCreateItem?.(inputValue.trim()), then returns. This runs before/instead of the ComboBox's default Enter behavior that selects the currently highlighted option. As a result, a user who types a filter (e.g. "Ali"), arrows down to an existing option ("Alice Johnson") and presses Enter will create a brand-new item "Ali" instead of selecting the existing option. Keyboard selection of existing items becomes impossible whenever creation is enabled.

Consider only creating when there is no active/focused option in the ComboBox state (e.g. check comboBoxStateContext.selectionManager.focusedKey is null) before short-circuiting to onCreateItem, otherwise fall through to default selection.

โœ… Bug: onCreateItem ignores multiple=false, allowing many items in single-select

๐Ÿ“„ openmetadata-ui-core-components/src/main/resources/ui/src/components/base/autocomplete/autocomplete.tsx:431-445 ๐Ÿ“„ openmetadata-ui-core-components/src/main/resources/ui/src/components/base/autocomplete/autocomplete.tsx:405-411 onSelectionChange guards single-select with if (!multiple && internalSelected.length >= 1) return;, but the new onCreateItem callback has no such guard. It always appends the created item to internalSelected. In single-select mode (multiple={false}, e.g. the AllowsCreationSingleSelect story), repeatedly creating values accumulates multiple selected items, violating the single-selection contract. Mirror the guard from onSelectionChange: in single-select, replace the selection (or bail when one already exists) instead of pushing. Also add multiple to the useCallback dependency array.

โœ… Edge Case: onBlur auto-creates spurious items from leftover filter text

๐Ÿ“„ openmetadata-ui-core-components/src/main/resources/ui/src/components/base/autocomplete/autocomplete.tsx:276-281 The AriaInput onBlur handler creates an item from any non-empty trimmed input value when allowsCreation is set. This fires whenever the input loses focus, including when the user typed a search filter intending to pick an existing option, or simply clicked away without wanting to create anything. In particular, selecting an existing option by mouse can leave residual filter text that then gets turned into an unintended custom item on blur. Consider only creating on an explicit commit action (Enter), or skipping blur-creation when a dropdown option is being selected / the value matches an existing item's label.

โœ… Bug: Single-select create-replace no longer notifies form value

๐Ÿ“„ openmetadata-ui-core-components/src/main/resources/ui/src/components/base/autocomplete/autocomplete.tsx:450-461 ๐Ÿ“„ openmetadata-ui-core-components/src/main/resources/ui/src/components/application/form-field/render-field-element.tsx The delta moved onItemInserted?.(value) from an unconditional call (previously outside the updater) to inside the setInternalSelected updater's !alreadySelected branch. The single-select replacement branch (if (!multiple && prev.length >= 1) { return [newItem]; }) now returns the new item but never calls onItemInserted.

Before this commit, onItemInserted fired unconditionally after the updater, so this path worked. Now, in single-select mode with allowsCreation, if an item is already selected and the user creates a new custom value (Enter/blur), internalSelected is replaced with [newItem] โ€” so the chip UI shows the new value โ€” but onItemInserted is never invoked. Consequently handleInsert in render-field-element.tsx never calls field.onChange, and the react-hook-form value retains the OLD selection. The visible chip and the committed form value diverge silently until submission.

Fix: call onItemInserted?.(value) in the replacement branch as well (or restructure to compute the decision outside the updater).

โœ… Quality: Side effect (onItemInserted) called inside setState updater

๐Ÿ“„ openmetadata-ui-core-components/src/main/resources/ui/src/components/base/autocomplete/autocomplete.tsx:450-464 onItemInserted?.(value) is invoked inside the setInternalSelected updater callback. State updater functions should be pure; React intentionally double-invokes updaters in StrictMode (development) to surface impurity, which would fire onItemInserted โ€” and therefore field.onChange/any parent side effects โ€” twice during development. It also makes the logic harder to reason about and may trip strict lint rules.

Consider computing the dedup decision against the current internalSelected outside the updater (adding it to the useCallback deps) and calling onItemInserted from the effectful body rather than from within the state updater.

Options

Display: compact โ†’ Showing less information.

Comment with these commands to change:

Compact
gitar display:verbose         

Was this helpful? React with ๐Ÿ‘ / ๐Ÿ‘Ž | Gitar

gitar-bot avatar Jun 26 '26 05:06 gitar-bot