Skip to content

Commit

Permalink
fix(DatePicker): renders mask_placeholder based on locale
Browse files Browse the repository at this point in the history
  • Loading branch information
langz committed Nov 30, 2023
1 parent 81dab4d commit 1041d9b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
10 changes: 9 additions & 1 deletion packages/dnb-eufemia/src/components/date-picker/DatePicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -466,12 +466,20 @@ export default class DatePicker extends React.PureComponent {
}

render() {
const {
lang,
locale: { code },
} = this.props

// use only the props from context, who are available here anyway
const props = extendPropsWithContextInClassComponent(
this.props,
DatePicker.defaultProps,
{ skeleton: this.context?.skeleton },
this.context.getTranslation(this.props).DatePicker,
this.context.getTranslation({
lang,
locale: code,
}).DatePicker,
// Deprecated – can be removed in v11
pickFormElementProps(this.context?.FormRow),
pickFormElementProps(this.context?.formElement),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import addDays from 'date-fns/addDays'
import addMonths from 'date-fns/addMonths'
import getDaysInMonth from 'date-fns/getDaysInMonth'
import isWeekend from 'date-fns/isWeekend'
import enLocale from 'date-fns/locale/en-GB'
import {
toRange,
dayOffset,
Expand Down Expand Up @@ -1290,6 +1291,35 @@ describe('DatePicker component', () => {
expect(element.classList).toContain('dnb-date-picker--opened')
})

it('renders correct placeholder when setting locale', () => {
const props: DatePickerProps = {}

render(<DatePicker {...props} show_input={true} locale={enLocale} />)

const dayElem = document.querySelectorAll(
'input.dnb-date-picker__input--day'
)[0] as HTMLInputElement
const monthElem = document.querySelectorAll(
'input.dnb-date-picker__input--month'
)[0] as HTMLInputElement
const yearElem = document.querySelectorAll(
'input.dnb-date-picker__input--year'
)[0] as HTMLInputElement

const seperator1 = document.querySelectorAll(
'.dnb-date-picker--separator'
)[0]
const seperator2 = document.querySelectorAll(
'.dnb-date-picker--separator'
)[0]

expect(dayElem.value).toBe('dd')
expect(monthElem.value).toBe('mm')
expect(yearElem.value).toBe('yyyy')
expect(seperator1.textContent).toBe('/')
expect(seperator2.textContent).toBe('/')
})

it('has to react on keydown events', async () => {
render(
<DatePicker
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -544,3 +544,14 @@ export const GlobalStatusExample = () => {
</>
)
}

export const LocalePropExample = () => {
return (
<>
<DatePicker show_input={true} locale={enLocale} />
{/* <Provider locale="en-GB">
<DatePicker show_input={true} />
</Provider> */}
</>
)
}

0 comments on commit 1041d9b

Please sign in to comment.