简体   繁体   English

React 中的周历 - 选择多个日期的问题

[英]Week calendar in React - problem with selecting multiple dates

Herę is my little problem, I'd be very glad for help.这是我的小问题,我很乐意提供帮助。 I'm making simple week calendar, where students can book lessons.我正在制作简单的周历,学生可以在其中预订课程。 It'll have admin view (where admin can select available hours from whole week) and student view (where user will see calendar based on hours chosen by admin).它将具有管理员视图(管理员可以从整个星期中选择可用时间)和学生视图(用户将根据管理员选择的时间查看日历)。

I have problem with selecting multiple dates by admin.我在管理员选择多个日期时遇到问题。

  • Calendar should display hours from 7am-10pm (mon-sun).日历应显示从早上 7 点到晚上 10 点(周一至周日)的时间。 Here's my base code for mapping days and hours (that works fine, but maybe there is another way to do it better).这是我映射日期和时间的基本代码(工作正常,但也许还有另一种方法可以做得更好)。 Here is store:这里是商店:
export const hours = [
    {hour: "7:00"},
    {hour: "7:45"},
    {hour: "8:30"},
    {hour: "9:15"},
    {hour: "10:00"},
    {hour: "10:45"},
    Etc
export const dayNames = [
    {
        longName: 'Monday',
        shortName: 'Md'
    },
    {
        longName: 'Tuesday',
        shortName: 'Tue'
    },

Here is code that map this data:这是映射此数据的代码:

const [active, setActive] = useState([])

...

<div className="calendar-days"> 
    {dayNames.map((item, dayIndex) => 
         <div className="day-column" >
             <h2 key={dayIndex}>{item.longName}</h2>
             {hours.map((hour, hourIndex) => {
                 const isActive = active.includes(hour.hour)
                 return (
                    <button
                       key={hourIndex}
                       onClick={() => setActive(isActive
                       ? active.filter(current => current !== hour.hour)
                       : [...active, {hour: hour.hour, hourId: hourIndex, dayId: dayIndex}])}
                       className={`
                          hour 
                          ${ isActive ? "active" : "" }
                       `}>
                       {hour.hour} 
                    </button>
                  )
              })}
          </div>
       )}  
    </div>

What this code do correctly:此代码正确执行的操作:

  • generates calendar with correct day & hours names生成具有正确日期和时间名称的日历

  • onClick on hour button generates object with selected day id, hour id and hour (and store them in active state)小时按钮上的 onClick 生成具有选定日期 ID、小时 ID 和小时的对象(并将它们存储在活动状态)

What it should do:它应该做什么:

  • give active class to object that is selected (and stored in active state)将活动类赋予选定的对象(并存储在活动状态)

  • when active hour is clicked second time active class should be removed and object should be removed from active state当第二次点击活动时间时,活动类应该被移除,对象应该从活动状态中移除

Thanks for any advices.感谢您的任何建议。 If more code is needed please let me know.如果需要更多代码,请告诉我。

The problem is in this line of code问题出在这行代码

const isActive = active.includes(hour.hour)

Here,这里,

  • hour.hour holds a string value ex: "7:00" hour.hour 包含一个字符串值 ex: "7:00"
  • active is a array of objects ex: [{hour: "7:00", hourId: 1, dayId: 0}] active 是一个对象数组,例如:[{hour: "7:00", hourId: 1, dayId: 0}]

So to find isActive you have to do like this,所以要找到 isActive 你必须这样做,

const isActive = active.find(act => act.hour === hour.hour);

Then, while setting setActive also filter function can be modified as,然后,在设置 setActive 的同时,过滤器功能也可以修改为,

             setActive(
                isActive
                  ? active.filter((current) => current.hour !== hour.hour)
                  : [
                      ...active,
                      {
                        hour: hour.hour,
                        hourId: hourIndex,
                        dayId: dayIndex,
                      },
                    ]
              )

Hope it helps.希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM