import classNames from 'classnames'; │ │ 5 import { format } from 'date-fns'; │ │ 4…
Private research annotation
Sign in to add a private research label or note.
- User
import classNames from 'classnames'; │
│ 5 import { format } from 'date-fns'; │
│ 4 import { ru } from 'date-fns/locale'; │
│ 3 import React, { useEffect, useRef, useState } from 'react'; │
│ 2 │
│ 1 import styles from '@/styles/components/Datepicker/index.module.scss'; │
│7 │
│ 1 function formatRu(date: Date, formatStr: string): string { │
│ 2 │ return format(date, formatStr, { locale: ru }); │
│ 3 } │
│ 4 │
│ 5 function getWeek(): Date[] { │
│ 6 │ const dates: Date[] = []; │
│ 7 │ for (let i = 6; i >= 0; i--) { │
│ 8 │ │ const today = new Date(); │
│ 9 │ │ dates.push(new Date(today.setDate(today.getDate() - i))); │
│ 10 │ } │
│ 11 │ │ │ 12 │ return dates; │ │ 13 } │ │ 14 │ │ 15 type PropsType = { │ │ 16 │ onChange: (dateStr: string) => void; │ │ 17 }; │ │ 18 │ │ 19 const Datepicker: React.FC = ({ onChange }) => { │ │ 20 │ const rootEl = useRef(null); │ │ 21 │ const [selectedDate, setSelectedDate] = useState<Date | null>(null); │ │ 22 │ const [week] = useState(getWeek()); │ │ 23 │ │ │ 24 │ function processSelectedDateChange(date: Date | null) { │ │ 25 │ │ setSelectedDate(date); │ │ 26 │ │ onChange(date ? format(date, 'yyyy-MM-dd') : ''); │ │ 27 │ } │ │ 28 │ │ │ 29 │ useEffect(() => { │ │ 30 │ │ if (!rootEl.current) return; │ │ 31 │ │ rootEl.current.scrollBy(0, 0); │ │ 32 │ }, []); │ │ 33 │ │ │ 34 │ return ( │ │ 35 │ │ │ │ 36 │ │ │ │ │ 37 │ │ │ │ │ │ 38 │ │ │ │ │ │ │ 39 │ │ │ │ │ │ {selectedDate ? formatRu(selectedDate, 'do, MMMM') : 'Выбрать дату'} │ │ 40 │ │ │ │ │ │ │ 41 │ │ │ │ │ <input │ │ 42 │ │ │ │ │ │ onClick={(e) => { │ │ 43 │ │ │ │ │ │ │ const input = e.currentTarget; │ │ 44 │ │ │ │ │ │ │ if ('showPicker' in input) { │ │ 45 │ │ │ │ │ │ │ │ input.showPicker(); │ │ 46 │ │ │ │ │ │ │ } │ │ 47 │ │ │ │ │ │ }} │ │ 48 │ │ │ │ │ │ type="date" │ │ 49 │ │ │ │ │ │ onChange={(e) => { │ │ 50 │ │ │ │ │ │ │ const dateStr = e.target.value; │ │ 51 │ │ │ │ │ │ │ processSelectedDateChange(dateStr ? new Date(dateStr) : null); │ │ 52 │ │ │ │ │ │ }} │ │ 53 │ │ │ │ │ /> │ │ 54 │ │ │ │ │ {selectedDate && ( │ │ 55 │ │ │ │ │ │ <button │ │ 56 │ │ │ │ │ │ │ onClick={() => processSelectedDateChange(null)} │ │ 57 │ │ │ │ │ │ │ className={styles.reset} │ │ 58 │ │ │ │ │ │ │ type="button" │ │ 59 │ │ │ │ │ │ /> │ │ 60 │ │ │ │ │ )} │ │ 61 │ │ │ │ │ │ 62 │ │ │ │ {week.map((el, i) => ( │ │ 63 │ │ │ │ │ <button │ │ 64 │ │ │ │ │ │ key={i} │ │ 65 │ │ │ │ │ │ type="button" │ │ 66 │ │ │ │ │ │ className={classNames(styles.date, { │ │ 67 │ │ │ │ │ │ │ [styles.__active]: │ │ 68 │ │ │ │ │ │ │ │ selectedDate && │ │ 69 │ │ │ │ │ │ │ │ formatRu(selectedDate, 'd MM') === formatRu(el, 'd MM'), │ │ 70 │ │ │ │ │ │ })} │ │ 71 │ │ │ │ │ │ onClick={() => processSelectedDateChange(el)} - Assistant
The calendar you are observing when clicking the button in your Next.js datepicker component does not appear to be implemented within the provided code snippet. Instead, it seems to be leveraging the native date input element's built-in calendar functionality.
Explanation of the Code:
-
Input Type: In your code, you are using an
<input type="date">element:javascript
-
- User
but this site is in russian language, and i want calender to be also russian, and monday as week start. but i got english and s as start. how to change locale of this input?
- Assistant