【问题标题】:Generate Material Timeline from JSON Object从 JSON 对象生成材质时间线
【发布时间】:2021-09-06 21:23:37
【问题描述】:

我有这个由 BE 生成的 Json 对象:

{
  "step1": {
    "approved": true,
    "approvalTime": "10-11-2021",
    "title": "title 1",
    "description": "description 1"
  },
  "step2": {
    "approved": true,
    "approvalTime": "10-11-2021",
    "title": "title 2",
    "description": "description 2"
  },
  "step3": {
    "approved": true,
    "approvalTime": "10-11-2021",
    "title": "title 3",
    "description": "description 3"
  }
}

材料时间线:

                     <Timeline align="alternate">
                        <TimelineItem>
                            <TimelineOppositeContent>
                                <Typography variant="body2" color="textSecondary"> // get color form Json
                                    9:30 am  // get this value from JSON
                                </Typography>
                            </TimelineOppositeContent>
                            <TimelineSeparator>
                                <TimelineDot>
                                    <LibraryBooks />
                                </TimelineDot>
                                <TimelineConnector />
                            </TimelineSeparator>
                            <TimelineContent>
                                <Paper elevation={3} className={classesTimeline.paper}>
                                    <Typography variant="h6" component="h1">
                                        title 1 // get title from JSON
                                    </Typography>
                                    <Typography> // get description from JSON</Typography>
                                </Paper>
                            </TimelineContent>
                        </TimelineItem>

                        <TimelineItem>
                            <TimelineOppositeContent>
                                <Typography variant="body2" color="textSecondary">
                                    11:00 am // get this value from JSON
                                </Typography>
                            </TimelineOppositeContent>
                            <TimelineSeparator>
                                <TimelineDot color="primary">
                                    <PeopleAlt />
                                </TimelineDot>
                                <TimelineConnector />
                            </TimelineSeparator>
                            <TimelineContent>
                                <Paper elevation={3} className={classesTimeline.paper}>
                                    <Typography variant="h6" component="h1">
                                        title 2 // get title from JSON
                                    </Typography>
                                    <Typography>description 2 // get value from JSON</Typography>
                                </Paper>
                            </TimelineContent>
                        </TimelineItem>

                        <TimelineItem>
                            <TimelineOppositeContent>
                                <Typography variant="body2" color="textSecondary">
                                    10:00 am // get value from JSON
                                </Typography>
                            </TimelineOppositeContent>
                            <TimelineSeparator>
                                <TimelineDot color="primary">
                                    <BusinessCenter />
                                </TimelineDot>
                                <TimelineConnector />
                            </TimelineSeparator>
                            <TimelineContent>
                                <Paper elevation={3} className={classesTimeline.paper}>
                                    <Typography variant="h6" component="h1">
                                        title 3 // get values from JSON
                                    </Typography>
                                    <Typography>description 3 // get value from JSON</Typography>
                                </Paper>
                            </TimelineContent>
                        </TimelineItem>

                    </Timeline>

是否可以使用 Typescript 设置来自 JSON 对象的所有值并根据 JSON 值设置 Paper 组件颜色?当我有"approved": true 为Paper 背景设置绿色时。

【问题讨论】:

  • 不确定我是否完全关闭。但是这个材质时间轴代码是在 .tsx 文件中吗?当您在变量中拥有结构时(比如说 myjson),您可以简单地在内部引用它。例如。类似${myjson.approved ? 'green' : 'textSecondary'}
  • 嗨,Peter,我在您的 Material Timeline 中看到几行引用了 JSON 中未出现的值(例如 color、9:30 am)。我应该从哪里获取这些值?
  • 你可以暂时忽略它们。
  • 我不确定“由 BE 生成”是什么意思。后端? JSON 是来自项目目录中的 .json 文件,还是您从 API 获取的内容?你得到的答案很好,但我们都错过了如何让 JSON 作为道具传递。
  • 您所说的“来自可靠来源的答案”是什么意思?

标签: javascript reactjs typescript material-ui react-typescript


【解决方案1】:

是的,你可以,而且它比你想象的要简单。实现这一目标有两个主要选择:

  1. 传播道具 - 意味着将您需要的值作为道具传递给将时间线作为其子元素之一的组件:
<ComponentWithTimeline steps={<stepsJsonVariableName>}/>

时间线组件的样式部分

classesTimeline:{
    ...classesTimeline
    greenPaper:
    {
        ...classesTimeline.paper,
        backgroundColor: "green",
    }
}

组件的时间轴部分


                     <Timeline align="alternate">
                        <TimelineItem>
                            <TimelineOppositeContent>
                                <Typography variant="body2" color="textSecondary"> // get color form Json
                                    9:30 am  // get this value from JSON
                                </Typography>
                            </TimelineOppositeContent>
                            <TimelineSeparator>
                                <TimelineDot>
                                    <LibraryBooks />
                                </TimelineDot>
                                <TimelineConnector />
                            </TimelineSeparator>
                            <TimelineContent>
                                <Paper elevation={3} className={(props.steps.step1.approved) ? classesTimeline.greenPaper : classesTimeline.paper}>
                                    <Typography variant="h6" component="h1">
                                        {props.steps.step1.title}
                                    </Typography>
                                    <Typography> {props.steps.step1.description}</Typography>
                                </Paper>
                            </TimelineContent>
                        </TimelineItem>

                        <TimelineItem>
                            <TimelineOppositeContent>
                                <Typography variant="body2" color="textSecondary">
                                    11:00 am // get this value from JSON
                                </Typography>
                            </TimelineOppositeContent>
                            <TimelineSeparator>
                                <TimelineDot color="primary">
                                    <PeopleAlt />
                                </TimelineDot>
                                <TimelineConnector />
                            </TimelineSeparator>
                            <TimelineContent>
                                <Paper elevation={3} className={(props.steps.step.approved) ? classesTimeline.greenPaper : classesTimeline.paper}>
                                    <Typography variant="h6" component="h1">
                                        {props.steps.step2.title}
                                    </Typography>
                                    <Typography>{props.steps.step2.description}</Typography>
                                </Paper>
                            </TimelineContent>
                        </TimelineItem>

                        <TimelineItem>
                            <TimelineOppositeContent>
                                <Typography variant="body2" color="textSecondary">
                                    10:00 am // get value from JSON
                                </Typography>
                            </TimelineOppositeContent>
                            <TimelineSeparator>
                                <TimelineDot color="primary">
                                    <BusinessCenter />
                                </TimelineDot>
                                <TimelineConnector />
                            </TimelineSeparator>
                            <TimelineContent>
                                <Paper elevation={3} className={(props.steps.step3.approved) ? classesTimeline.greenPaper : classesTimeline.paper}>
                                    <Typography variant="h6" component="h1">
                                        {props.steps}
                                    </Typography>
                                    <Typography>{props.steps.step3.description}
</Typography>
                                </Paper>
                            </TimelineContent>
                        </TimelineItem>

                    </Timeline>
  1. 使用反应上下文使 json 对象的值可用于所有子对象 (docs)

基本示例:

// Context lets us pass a value deep into the component tree
// without explicitly threading it through every component.
// Create a context for the current theme (with "light" as the default).
const ThemeContext = React.createContext('light');

class App extends React.Component {
  render() {
    // Use a Provider to pass the current theme to the tree below.
    // Any component can read it, no matter how deep it is.
    // In this example, we're passing "dark" as the current value.
    return (
      <ThemeContext.Provider value="dark">
        <Toolbar />
      </ThemeContext.Provider>
    );
  }
}

// A component in the middle doesn't have to
// pass the theme down explicitly anymore.
function Toolbar() {
  return (
    <div>
      <ThemedButton />
    </div>
  );
}

class ThemedButton extends React.Component {
  // Assign a contextType to read the current theme context.
  // React will find the closest theme Provider above and use its value.
  // In this example, the current theme is "dark".
  static contextType = ThemeContext;
  render() {
    return <Button theme={this.context} />;
  }
}

【讨论】:

  • @PeterPenzov 这个答案对你有帮助吗?
【解决方案2】:

您可以根据approved 字段有条件地注入一个类名,
以下是提取的组件的示例:

const YourCustomComp = (x: {approved: boolean; approvalTime: string; title: string; description: string}) => (
  <>
     ....
     <Paper elevation={3} className={`${classesTimeline.paper} ${x.approved ? 'greenSelector' : ''} `>
        <Typography variant="h6" component="h1">
           {x.title}
        </Typography>
        <Typography> {x.description} </Typography>
     </Paper>
     ....
  </>
)

还有一个名为clsx 的实用程序是为这种情况创建的,我建议你看看它。

【讨论】:

    猜你喜欢
    • 2021-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-09
    • 2020-08-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多