Skip to content

Commit

Permalink
fix(Field.PhoneNumber): ensure data value is used when given
Browse files Browse the repository at this point in the history
  • Loading branch information
tujoworker committed Jan 12, 2024
1 parent a5ae88b commit 6c92502
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ function PhoneNumber(props: Props) {
}

const {
value,
className,
countryCodeFieldClassName,
numberFieldClassName,
Expand Down Expand Up @@ -208,7 +209,7 @@ function PhoneNumber(props: Props) {
* We then update countryCode and phoneNumber when value changes.
*/
useMemo(() => {
const [countryCode, phoneNumber] = splitValue(props.value)
const [countryCode, phoneNumber] = splitValue(props.value || value)
numberRef.current = phoneNumber

if (lang !== langRef.current || !wasFilled.current) {
Expand All @@ -219,7 +220,7 @@ function PhoneNumber(props: Props) {

updateCurrentDataSet()
}
}, [props.value, lang, updateCurrentDataSet])
}, [value, props.value, lang, updateCurrentDataSet])

const prevCountryCodeRef = React.useRef(countryCodeRef.current)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,20 @@ describe('Field.PhoneNumber', () => {
expect(phoneNumberInput).toHaveAttribute('inputmode', 'tel')
})

it('should render value from context', () => {
render(
<Form.Handler data={{ phoneNumber: '9999' }}>
<PhoneNumber path="/phoneNumber" />
</Form.Handler>
)

const phoneNumberInput = document.querySelector(
'.dnb-forms-field-phone-number__number .dnb-input__input'
)

expect(phoneNumberInput).toHaveValue('99 99 ​​ ​​')
})

describe('locale', () => {
it('should change locale', () => {
const { rerender } = render(
Expand Down

0 comments on commit 6c92502

Please sign in to comment.