feat(agent): support dynamic newOption in select and selectMulti metadata fields (#306)

* feat(webui): redesign select and multi-select metadata fields with search box and dynamic option creation

* fix(webui): stabilize select popover placement and preserve field visibility in store

* refactor(webui): enforce 2-row collapsed height and whitespace-pre-wrap on LongTextField

* feat(agent): support dynamic newOption in select and selectMulti metadata fields

* fix(metadata): address code review feedback on selectMulti item validation and WebUI option deduplication
This commit is contained in:
Yiling-J
2026-08-14 10:56:18 +08:00
committed by GitHub
parent 7acf736300
commit fc878775c2
11 changed files with 851 additions and 28 deletions
@@ -1,5 +1,5 @@
// @vitest-environment happy-dom
import { cleanup, fireEvent, render, screen } from '@testing-library/react'
import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'
import React from 'react'
import { afterEach, describe, expect, it, vi } from 'vitest'
import SelectField from './select-field'
@@ -83,5 +83,39 @@ describe('SelectField Component', () => {
expect(screen.getByText('abc')).toBeTruthy()
// "Add options: abc" button should be hidden
expect(screen.queryByText('Add options: abc')).toBeNull()
// Type "foo" -> exact match with option id "foo" / displayName "Foo"
fireEvent.change(searchInput, { target: { value: 'foo' } })
expect(screen.queryByText('Add options: foo')).toBeNull()
})
it('calls API and onSave when adding a new option', async () => {
const onSave = vi.fn()
const { client } = await import('@/ui/api/client')
vi.mocked(client.api.fields[':fieldId'].$put).mockResolvedValue({
ok: true,
json: async () => ({}),
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any)
render(<SelectField value="" config={sampleConfig} fieldId="field_123" onSave={onSave} />)
const trigger = screen.getByText('Select an option')
fireEvent.click(trigger)
const searchInput = screen.getByPlaceholderText('Search options...')
fireEvent.change(searchInput, { target: { value: 'New Custom Option' } })
const addBtn = screen.getByText('Add options: New Custom Option')
expect(addBtn).toBeTruthy()
fireEvent.click(addBtn)
await waitFor(() => {
expect(client.api.fields[':fieldId'].$put).toHaveBeenCalledWith(
expect.objectContaining({
param: { fieldId: 'field_123' },
}),
)
expect(onSave).toHaveBeenCalledWith('new-custom-option')
})
})
})
@@ -28,7 +28,9 @@ const SelectField: React.FC<FieldProps<string>> = ({
opt.displayName.toLowerCase().includes(trimmedSearch.toLowerCase()),
)
const hasExactMatch = options.some(
(opt: SelectOption) => opt.displayName.trim().toLowerCase() === trimmedSearch.toLowerCase(),
(opt: SelectOption) =>
opt.displayName.trim().toLowerCase() === trimmedSearch.toLowerCase() ||
opt.id.toLowerCase() === trimmedSearch.toLowerCase(),
)
const handleStartEdit = () => {
@@ -1,5 +1,5 @@
// @vitest-environment happy-dom
import { cleanup, fireEvent, render, screen } from '@testing-library/react'
import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'
import React from 'react'
import { afterEach, describe, expect, it, vi } from 'vitest'
import SelectMultiField from './select-multi-field'
@@ -79,5 +79,46 @@ describe('SelectMultiField Component', () => {
fireEvent.change(searchInput, { target: { value: 'abc' } })
expect(screen.getByText('abc')).toBeTruthy()
expect(screen.queryByText('Add options: abc')).toBeNull()
// Type "foo" -> exact match with option id "foo" / displayName "Foo"
fireEvent.change(searchInput, { target: { value: 'foo' } })
expect(screen.queryByText('Add options: foo')).toBeNull()
})
it('calls API and onSave when adding a new option in MultiSelect', async () => {
const onSave = vi.fn()
const { client } = await import('@/ui/api/client')
vi.mocked(client.api.fields[':fieldId'].$put).mockResolvedValue({
ok: true,
json: async () => ({}),
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any)
render(
<SelectMultiField
value={['foo']}
config={sampleConfig}
fieldId="field_multi_123"
onSave={onSave}
/>,
)
const trigger = screen.getByText('Foo')
fireEvent.click(trigger)
const searchInput = screen.getByPlaceholderText('Search options...')
fireEvent.change(searchInput, { target: { value: 'New Multi Option' } })
const addBtn = screen.getByText('Add options: New Multi Option')
expect(addBtn).toBeTruthy()
fireEvent.click(addBtn)
await waitFor(() => {
expect(client.api.fields[':fieldId'].$put).toHaveBeenCalledWith(
expect.objectContaining({
param: { fieldId: 'field_multi_123' },
}),
)
expect(onSave).toHaveBeenCalledWith(['foo', 'new-multi-option'])
})
})
})
@@ -32,7 +32,9 @@ const SelectMultiField: React.FC<FieldProps<string[]>> = ({
opt.displayName.toLowerCase().includes(trimmedSearch.toLowerCase()),
)
const hasExactMatch = options.some(
(opt: SelectOption) => opt.displayName.trim().toLowerCase() === trimmedSearch.toLowerCase(),
(opt: SelectOption) =>
opt.displayName.trim().toLowerCase() === trimmedSearch.toLowerCase() ||
opt.id.toLowerCase() === trimmedSearch.toLowerCase(),
)
// Dynamic calculation for how many items fit in one line