将类组件转换为函数组件 – React 日历时间线

huangapple go评论73阅读模式
英文:

Convert Class component to functional component - react calendar timeline

问题

我有一个React日历时间轴npm包的示例。这是使用类组件开发的,有人可以帮助我将其转换为在React中使用函数组件吗?我对React还很陌生,所以无法在函数组件中使其工作。

示例链接:
https://codesandbox.io/s/r1mxzj581m

英文:

I have an example of React Calendar Timeline npm. This is developed with Class component can someone help me to convert this to use functional component in React. I am new to react so couldn't make it work with functional component.

Example link:
https://codesandbox.io/s/r1mxzj581m

答案1

得分: 1

以下是功能组件的工作沙盒链接

import React, { useState } from "react";
import moment from "moment";
import Timeline from "react-calendar-timeline";
import generateFakeData from "./generate-fake-data";

const keys = {
  groupIdKey: "id",
  groupTitleKey: "title",
  groupRightTitleKey: "rightTitle",
  itemIdKey: "id",
  itemTitleKey: "title",
  itemDivTitleKey: "title",
  itemGroupKey: "group",
  itemTimeStartKey: "start",
  itemTimeEndKey: "end",
  groupLabelKey: "title"
};

const App = () => {
  const { groups: initialGroups, items: initialItems } = generateFakeData();
  const defaultTimeStart = moment().startOf("day").toDate();
  const defaultTimeEnd = moment().startOf("day").add(1, "day").toDate();

  // 将每3个组中的2个转换为节点,将第一个保留为根节点
  const newGroups = initialGroups.map((group) => {
    const isRoot = (parseInt(group.id) - 1) % 3 === 0;
    const parent = isRoot
      ? null
      : Math.floor((parseInt(group.id) - 1) / 3) * 3 + 1;

    return { ...group, root: isRoot, parent };
  });

  const [groups, setGroups] = useState(newGroups);
  const [items] = useState(initialItems);
  const [openGroups, setOpenGroups] = useState({});

  const toggleGroup = (id) => {
    setOpenGroups((prevOpenGroups) => ({
      ...prevOpenGroups,
      [id]: !prevOpenGroups[id]
    }));
  };

  // 隐藏(过滤)已关闭的组,对于其余的组,修补它们的“title”并添加一些回调或填充
  const filteredGroups = groups.filter((g) => g.root || openGroups[g.parent]);
  const newGroupsToShow = filteredGroups.map((group) => {
    return {
      ...group,
      title: group.root ? (
        <div
          onClick={() => toggleGroup(parseInt(group.id))}
          style={{ cursor: "pointer" }}
        >
          {openGroups[parseInt(group.id)] ? "[-]" : "[+]"} {group.title}
        </div>
      ) : (
        <div style={{ paddingLeft: 20 }}>{group.title}</div>
      )
    };
  });

  return (
    <Timeline
      groups={newGroupsToShow}
      items={items}
      keys={keys}
      sidebarWidth={150}
      canMove
      canResize="right"
      canSelect
      itemsSorted
      itemTouchSendsClick={false}
      stackItems
      itemHeightRatio={0.75}
      showCursorLine
      defaultTimeStart={defaultTimeStart}
      defaultTimeEnd={defaultTimeEnd}
    />
  );
};

export default App;
英文:

Here is the working sandbox link for functional component.

import React, { useState } from &quot;react&quot;;
import moment from &quot;moment&quot;;
import Timeline from &quot;react-calendar-timeline&quot;;
import generateFakeData from &quot;./generate-fake-data&quot;;
const keys = {
groupIdKey: &quot;id&quot;,
groupTitleKey: &quot;title&quot;,
groupRightTitleKey: &quot;rightTitle&quot;,
itemIdKey: &quot;id&quot;,
itemTitleKey: &quot;title&quot;,
itemDivTitleKey: &quot;title&quot;,
itemGroupKey: &quot;group&quot;,
itemTimeStartKey: &quot;start&quot;,
itemTimeEndKey: &quot;end&quot;,
groupLabelKey: &quot;title&quot;
};
const App = () =&gt; {
const { groups: initialGroups, items: initialItems } = generateFakeData();
const defaultTimeStart = moment().startOf(&quot;day&quot;).toDate();
const defaultTimeEnd = moment().startOf(&quot;day&quot;).add(1, &quot;day&quot;).toDate();
// convert every 2 groups out of 3 to nodes, leaving the first as the root
const newGroups = initialGroups.map((group) =&gt; {
const isRoot = (parseInt(group.id) - 1) % 3 === 0;
const parent = isRoot
? null
: Math.floor((parseInt(group.id) - 1) / 3) * 3 + 1;
return { ...group, root: isRoot, parent };
});
const [groups, setGroups] = useState(newGroups);
const [items] = useState(initialItems);
const [openGroups, setOpenGroups] = useState({});
const toggleGroup = (id) =&gt; {
setOpenGroups((prevOpenGroups) =&gt; ({
...prevOpenGroups,
[id]: !prevOpenGroups[id]
}));
};
// hide (filter) the groups that are closed, for the rest, patch their &quot;title&quot; and add some callbacks or padding
const filteredGroups = groups.filter((g) =&gt; g.root || openGroups[g.parent]);
const newGroupsToShow = filteredGroups.map((group) =&gt; {
return {
...group,
title: group.root ? (
&lt;div
onClick={() =&gt; toggleGroup(parseInt(group.id))}
style={{ cursor: &quot;pointer&quot; }}
&gt;
{openGroups[parseInt(group.id)] ? &quot;[-]&quot; : &quot;[+]&quot;} {group.title}
&lt;/div&gt;
) : (
&lt;div style={{ paddingLeft: 20 }}&gt;{group.title}&lt;/div&gt;
)
};
});
return (
&lt;Timeline
groups={newGroupsToShow}
items={items}
keys={keys}
sidebarWidth={150}
canMove
canResize=&quot;right&quot;
canSelect
itemsSorted
itemTouchSendsClick={false}
stackItems
itemHeightRatio={0.75}
showCursorLine
defaultTimeStart={defaultTimeStart}
defaultTimeEnd={defaultTimeEnd}
/&gt;
);
};
export default App;

huangapple
  • 本文由 发表于 2023年7月27日 18:35:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76778886.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定