reactjs - react 大日历风格的日期有事件

标签 reactjs customization react-big-calendar

我想更改我的月历 View 中包含事件的日期样式。

这是我正在开发的设计:

enter image description here

因此我需要将每个日期的 .rbc-date-cell 样式更改为蓝色。我到处寻找解决方案,最常见的是我找到了为 dateCellWrapper 创建 CustomDateCell 的示例,它将有条件地设置单元格的样式,这是我尝试过的一个示例:

React Big Calendar how to style a single day in the month view

这改变了 .rbc-day-bg 的样式,而不是 .rbc-date-cell。 RBC 上的标记很复杂,因为开发人员为可能持续多天的事件制作了它。这意味着背景 div 与日期单元格位于不同的父级中。我不想遍历/操纵 DOM 来设置这些单元格的样式。

肯定有一种方法可以设置日期单元格的样式吗?我在 SO 和 github 上找到的文档和所有答案到目前为止还没有产生任何结果。可以用 eventPropGetter 来完成吗?

编辑

HTML 如下。我也尝试过自定义插槽 Prop 和 eventPropgetter 但无法直接操作 .rbc-date-cell 的渲染

<div class="rbc-month-row">
  <div class="rbc-row-bg">
    <div class="rbc-day-bg" style="background-color: lightgreen;"></div>
    <div class="rbc-day-bg" style="background-color: lightgreen;"></div>
    <div class="rbc-day-bg rbc-today" style="background-color: lightgreen;"></div>
    <div class="rbc-day-bg" style="background-color: lightblue;"></div>
    <div class="rbc-day-bg" style="background-color: lightblue;"></div>
    <div class="rbc-day-bg" style="background-color: lightblue;"></div>
    <div class="rbc-day-bg" style="background-color: lightblue;"></div>
  </div>
  <div class="rbc-row-content">
    <div class="rbc-row">
      <div class="rbc-date-cell"><a href="#">13</a></div>
      <div class="rbc-date-cell"><a href="#">14</a></div>
      <div class="rbc-date-cell rbc-now rbc-current"><a href="#">15</a></div>
      <div class="rbc-date-cell"><a href="#">16</a></div>
      <div class="rbc-date-cell"><a href="#">17</a></div>
      <div class="rbc-date-cell"><a href="#">18</a></div>
      <div class="rbc-date-cell"><a href="#">19</a></div>
    </div>
  </div>
</div>

rbc-event 与 rbc-date-cell 存在不同的行中,在此标记中有 23 日和 24 日的事件。我不想遍历 DOM 以有条件地设置 div 样式,因为这不是一种非常 React 的做事方式,如果可能的话应该在渲染之前完成

<div class="rbc-row-content">
  <div class="rbc-row ">
    <div class="rbc-date-cell"><a href="#">21</a></div>
    <div class="rbc-date-cell"><a href="#">22</a></div>
    <div class="rbc-date-cell"><a href="#">23</a></div>
    <div class="rbc-date-cell"><a href="#">24</a></div>
    <div class="rbc-date-cell"><a href="#">25</a></div>
    <div class="rbc-date-cell"><a href="#">26</a></div>
    <div class="rbc-date-cell"><a href="#">27</a></div>
  </div>
  <div class="rbc-row">
    <div class="rbc-row-segment" style="flex-basis: 28.5714%; max-width: 28.5714%;"> </div>
    <div class="rbc-row-segment" style="flex-basis: 14.2857%; max-width: 14.2857%;"><button class="rbc-event"><div class="rbc-event-content" title="2/2"><p class="event-wrapper">2/2</p></div></button></div>
    <div class="rbc-row-segment" style="flex-basis: 14.2857%; max-width: 14.2857%;"><button class="rbc-event"><div class="rbc-event-content" title="1/1"><p class="event-wrapper">1/1</p></div></button></div>
  </div>
</div>
row

最佳答案

检查此沙盒代码 https://codesandbox.io/s/73ky8rj2qj 如果那是你想要实现的,那么主要的想法是覆盖月份 dateHeader 然后用每个 dateHeader 渲染检查它的 date prop 是否在任何事件 startDate 之间endDate 并以所需的样式呈现它

关于reactjs - react 大日历风格的日期有事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54105211/

相关文章:

javascript - 编译好的脚本应该放在 React 中的什么位置?

javascript - ReactJS - 父组件中 `setState` 的性能影响

vim - 重新映射:Wq to :wq in vim

javascript - 单击react-big-calendar中的事件后显示弹出窗口

reactjs - react 大日历从星期一而不是星期日开始一周?

javascript - React useState/useEffect 钩子(Hook)未正确返回信息

javascript - Material ui 中选项卡的内联样式不会改变颜色

WPF 自定义 TabControl

ios - Swift自定义表格 View 单元格未解析的标识符

react-big-calendar - react-big-calendar 事件的基本设置未显示