Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trim descriptions, so whitespace-only descriptions don't get passed through #2516

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion app/components/form/fields/DescriptionField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,18 @@ export function DescriptionField<
TFieldValues extends FieldValues,
TName extends FieldPath<TFieldValues>,
>(props: Omit<TextFieldProps<TFieldValues, TName>, 'validate'>) {
return <TextField as="textarea" validate={validateDescription} {...props} />
return (
<TextField
as="textarea"
validate={validateDescription}
onBlur={(event) => {
const trimmedDescription = event.target.value.trim()
event.target.value = trimmedDescription
props.control._formValues.description = trimmedDescription
}}
Copy link
Collaborator

@david-crespo david-crespo Oct 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think blur is too specific of a place for this — shouldn't transform work? Or I guess that prevents you from ever typing whitespace. I'm thinking we should just not do this if this is the only way to do it. Ideally it would happen at submit time (which we might already be doing manually in a few paces), but I don't know of a way to wire this up at field level to happen on submit, which is at form level.

{...props}
/>
)
}

// TODO Update JSON schema to match this, add fuzz testing between this and name pattern
Expand Down
19 changes: 16 additions & 3 deletions test/e2e/floating-ip-create.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ test('can create a floating IP', async ({ page }) => {

const floatingIpName = 'my-floating-ip'
await page.fill('input[name=name]', floatingIpName)
await page
.getByRole('textbox', { name: 'Description' })
.fill('A description for this Floating IP')
const description = page.getByRole('textbox', { name: 'Description' })
await description.fill('A description for this Floating IP')

const poolListbox = page.getByRole('button', { name: 'IP pool' })

Expand All @@ -51,6 +50,20 @@ test('can create a floating IP', async ({ page }) => {
description: 'A description for this Floating IP',
'IP pool': 'ip-pool-1',
})

// Make sure that descriptions with only whitespace get trimmed
await page.locator('text="New Floating IP"').click()
await page.fill('input[name=name]', 'no-description')
await description.fill(' ')
await description.blur()
await expect(description).toContainText('')
await page.getByRole('button', { name: 'Create floating IP' }).click()

await expectRowVisible(page.getByRole('table'), {
name: 'no-description',
description: '—',
'IP pool': 'ip-pool-1',
})
})

test('can detach and attach a floating IP', async ({ page }) => {
Expand Down
Loading