Skip to content

Commit

Permalink
Make required work
Browse files Browse the repository at this point in the history
  • Loading branch information
tujoworker committed Nov 13, 2023
1 parent f11b1d5 commit cc1c0f3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ function PhoneNumber(props: Props) {
className,
countryCodeFieldClassName,
numberFieldClassName,
layout = 'vertical',
countryCodePlaceholder,
placeholder,
countryCodeLabel,
Expand All @@ -61,6 +60,10 @@ function PhoneNumber(props: Props) {
disabled,
width = 'large',
help,
required,
validateInitially,
continuousValidation,
validateUnchanged,
handleFocus,
handleBlur,
handleChange,
Expand Down Expand Up @@ -136,7 +139,7 @@ function PhoneNumber(props: Props) {
countryCodeFieldClassName
)}
placeholder={countryCodePlaceholder ?? ' '}
label_direction={layout}
label_direction="vertical"
label={
countryCodeLabel ??
sharedContext?.translation.Forms.countryCodeLabel
Expand Down Expand Up @@ -189,6 +192,10 @@ function PhoneNumber(props: Props) {
disabled={disabled}
width="stretch"
help={help}
required={required}
validateInitially={validateInitially}
continuousValidation={continuousValidation}
validateUnchanged={validateUnchanged}
/>
</Flex.Horizontal>
</FieldBlock>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,24 @@ describe('Field.PhoneNumber', () => {
'dnb-forms-field-block--width-large',
])
})

it('should require one number', async () => {
render(<PhoneNumber required />)

const inputElement = document.querySelector(
'.dnb-forms-field-phone-number__number input'
) as HTMLInputElement

await userEvent.type(inputElement, '1{Backspace}')
fireEvent.blur(inputElement)

expect(document.querySelector('[role="alert"]')).toBeInTheDocument()

await userEvent.type(inputElement, '1')
fireEvent.blur(inputElement)

expect(
document.querySelector('[role="alert"]')
).not.toBeInTheDocument()
})
})
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { Field } from '../../..'
import { Field, Form } from '../../..'

export default {
title: 'Eufemia/Forms/PhoneNumber',
}

export function PhoneNumber() {
return <Field.PhoneNumber />
return (
<Form.Handler onSubmit={console.log}>
<Field.PhoneNumber required validateInitially path="/phoneNumber" />
<Form.SubmitButton top />
</Form.Handler>
)
}

0 comments on commit cc1c0f3

Please sign in to comment.