Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

Commit

Permalink
Merge pull request #438 from vicdurai/vic_translation_for_calendar_co…
Browse files Browse the repository at this point in the history
…mponent_issue-324

feat(datetimepicker): add support for multiple languages
  • Loading branch information
matteovivona authored Jun 5, 2020
2 parents 47ec12c + a33e397 commit a667e10
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
12 changes: 9 additions & 3 deletions src/components/DateTimePicker/DateTimePicker.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import { enUS, ptBR } from 'date-fns/locale'
import * as locales from 'date-fns/locale'
import React, { ReactNode, CSSProperties } from 'react'
import InputGroup from 'react-bootstrap/InputGroup'
import DatePicker, { registerLocale, setDefaultLocale } from 'react-datepicker'

import { Icon } from '../Icon'
import 'react-datepicker/dist/react-datepicker.css'

registerLocale('enUS', enUS)
registerLocale('ptBR', ptBR)
// Generic way to register all available locales
Object.entries(locales).forEach(([key, locale]) => {
registerLocale(key, locale)
})

// Additional locales required for the frontend
registerLocale('ar', locales.arDZ)
registerLocale('zr', locales.zhCN)

setDefaultLocale('enUS')

Expand Down
1 change: 1 addition & 0 deletions src/components/Dropdown/Dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const Dropdown = (props: Props) => {
style={item.style}
key={item.key || `dropdown-item-${i.toString()}`}
eventKey={item.eventKey || `dropdown-event-${i.toString()}`}
onClick={item.onClick}
>
{item.text}
</DropdownRB.Item>
Expand Down
38 changes: 37 additions & 1 deletion stories/datetimepicker.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { storiesOf } from '@storybook/react'
import * as locales from 'date-fns/locale'
import React, { useState } from 'react'

import { DateTimePicker } from '../src'
import { DateTimePicker, Dropdown } from '../src'

storiesOf('DateTimePickers', module)
.addParameters({
Expand Down Expand Up @@ -289,3 +290,38 @@ storiesOf('DateTimePickers', module)
</div>
)
})
.add('Supported languages', () => {
const [startDate, setStartDate] = useState(new Date())
const [language, setLanguage] = useState('enUS')
const items = Object.keys(locales).map((locale) => ({
text: locale,
onClick: () => {
setLanguage(locale)
},
}))
const ar = {
text: 'ar',
onClick: () => {
setLanguage('ar')
},
}
const zr = {
text: 'zr',
onClick: () => {
setLanguage('zr')
},
}
items.push(ar)
items.push(zr)

return (
<div>
<Dropdown text={language} items={items} />
<DateTimePicker
locale={language}
selected={startDate}
onChange={(date) => setStartDate(date)}
/>
</div>
)
})

0 comments on commit a667e10

Please sign in to comment.