Skip to content

Commit 465f1e7

Browse files
authored
fix(Field.Email): add default maxLength of 254 (#7244)
Motivation: https://dnb-it.slack.com/archives/CMXABCHEY/p1774429640322809 RFC 5321 and DNB's email guidelines limits the total length of an email address to 254 characters. This makes maxLength=254 the default for Field.Email, removing the need for consumers to set it manually. The default can be overridden via props, or removed by passing undefined. I think this is fine, and am not really considering this as a breaking change, more like a fix, that we should have a max length by default. Can't hardly see this being breaking for anyone.
1 parent c4af73d commit 465f1e7

4 files changed

Lines changed: 69 additions & 0 deletions

File tree

packages/dnb-design-system-portal/src/docs/uilib/extensions/forms/feature-fields/Email/info.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,4 @@ There is a corresponding [Value.Email](/uilib/extensions/forms/Value/Email) comp
2828
- Only valid characters are allowed.
2929
- `autocomplete` is set to `email`.
3030
- `inputmode` is set to `email`.
31+
- `maxLength` defaults to `254`, based on the RFC 5321 email address length limit. Can be overridden.

packages/dnb-eufemia/src/extensions/forms/Field/Email/Email.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ function Email(props: Props) {
4141
label,
4242
autoComplete: 'email',
4343
inputMode: 'email',
44+
// RFC 5321, which is used in DNB, limits the total length of an email address to 254 characters
45+
maxLength: 254,
4446
pattern,
4547
trim: true,
4648
...props,

packages/dnb-eufemia/src/extensions/forms/Field/Email/EmailDocs.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,9 @@ import { StringProperties } from '../String/StringDocs'
33

44
export const EmailProperties: PropertiesTableProps = {
55
...StringProperties,
6+
maxLength: {
7+
doc: 'Validation for maximum length of the text (number of characters). Defaults to `254` based on the RFC 5321 email address length limit.',
8+
type: 'number',
9+
status: 'optional',
10+
},
611
}

packages/dnb-eufemia/src/extensions/forms/Field/Email/__tests__/Email.test.tsx

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,67 @@ describe('Field.Email', () => {
111111
expect(input).toHaveAttribute('inputmode', 'email')
112112
})
113113

114+
it('should have a default maxLength of 254', async () => {
115+
render(
116+
<Form.Handler>
117+
<Field.Email path="/email" />
118+
</Form.Handler>
119+
)
120+
121+
const input = document.querySelector('input')
122+
123+
// 245 + '@' + 'test.com' = 254 characters – should be valid
124+
const validEmail = `${'a'.repeat(245)}@test.com`
125+
expect(validEmail).toHaveLength(254)
126+
await userEvent.type(input, validEmail)
127+
fireEvent.blur(input)
128+
129+
expect(screen.queryByRole('alert')).not.toBeInTheDocument()
130+
131+
// 246 + '@' + 'test.com' = 255 characters – should be invalid
132+
await userEvent.clear(input)
133+
const tooLongEmail = `${'a'.repeat(246)}@test.com`
134+
expect(tooLongEmail).toHaveLength(255)
135+
await userEvent.type(input, tooLongEmail)
136+
fireEvent.blur(input)
137+
138+
expect(screen.queryByRole('alert')).toBeInTheDocument()
139+
})
140+
141+
it('should allow overriding maxLength via props', async () => {
142+
render(
143+
<Form.Handler>
144+
<Field.Email path="/email" maxLength={10} />
145+
</Form.Handler>
146+
)
147+
148+
const input = document.querySelector('input')
149+
150+
// 11 characters should be invalid with maxLength=10
151+
await userEvent.type(input, 'aa@test.com')
152+
fireEvent.blur(input)
153+
154+
expect(screen.queryByRole('alert')).toBeInTheDocument()
155+
})
156+
157+
it('should not enforce maxLength when set to undefined', async () => {
158+
render(
159+
<Form.Handler>
160+
<Field.Email path="/email" maxLength={undefined} />
161+
</Form.Handler>
162+
)
163+
164+
const input = document.querySelector('input')
165+
166+
// 255 characters would fail default maxLength of 254, but should pass when undefined
167+
const longEmail = `${'a'.repeat(246)}@test.com`
168+
expect(longEmail).toHaveLength(255)
169+
await userEvent.type(input, longEmail)
170+
fireEvent.blur(input)
171+
172+
expect(screen.queryByRole('alert')).not.toBeInTheDocument()
173+
})
174+
114175
it('should have type="text" to avoid browser input manipulation', () => {
115176
const { rerender } = render(<Field.Email />)
116177

0 commit comments

Comments
 (0)