简体   繁体   English

antd时间轴的Reactjs条件属性

[英]Reactjs conditional attributes for an antd timeline

I am looping through a set of JSON data for this ReactJS component I am creating that has a timeline. 我正在为我创建的带有时间轴的这个ReactJS组件遍历一组JSON数据。

The JSON looks like this : JSON看起来像这样:

"chart": [{
    "label": "Registered",
    "date": "2017-07-03" 
}, {
    "label": "1",
    "date": "2017-07-04"
}, {
    "label": "2",
    "date": "2017-08-01"
}, {
    "label": "3",
    "date": "2017-09-01"
}]

And the code looks like this : 代码看起来像这样:

<Timeline>
    {
        this.props.data.contents.chart.map(function(e, index) {
            return (
                <Timeline.Item key={index}>
                    {e.label}
                    <span>{moment(e.date).format("DD MMM YYYY")}</span>
                </Timeline.Item>
            );
        })
    }            
</Timeline>

But I want to add a conditional attribute to take - to modify the Timeline.Item part. 但是我想添加一个带条件的属性-修改Timeline.Item部分。 But I am getting syntax errors. 但是我收到语法错误。

<Timeline>
    {
        this.props.data.contents.chart.map(function(e, index) {
            return (
                <Timeline.Item key={index} (e.label === "Registered" ? "dot={<Icon type="idcard" style={{ fontSize: '12px' }} />} color="red"" : null)>
                    {e.label}
                    <span>{moment(e.date).format("DD MMM YYYY")}</span>
                </Timeline.Item>
            );
        })
    }            
</Timeline>

You cannot build the actual JSX syntax code based on a ternary operator. 您不能基于三元运算符来构建实际的JSX语法代码。 You need to do one of the following : 您需要执行以下操作之一:

Either build each prop based on your condition as following : 根据您的条件构建每个道具,如下所示:

<Timeline>
    {
        this.props.data.contents.chart.map(function(e, index) {
            return (
                <Timeline.Item key={ index }
                               dot={ (e.label === 'Registered' ? <Icon type="idcard" style={{ fontSize: '12px' }} /> : null) }
                               color={ (e.label === 'Registered' ? 'red' : '') }>
                    {e.label}
                    <span>{moment(e.date).format("DD MMM YYYY")}</span>
                </Timeline.Item>
            );
        })
    }            
</Timeline>

Or return completely different JSX based on the condition : 或者根据条件返回完全不同的JSX:

<Timeline>
{
    this.props.data.contents.chart.map(function(e, index) {
        if (e.label === 'Registered') {
            return (
                <Timeline.Item key={ index }
                               dot={ <Icon type="idcard" style={{ fontSize: '12px' }} /> }
                               color="red">
                    {e.label}
                    <span>{moment(e.date).format("DD MMM YYYY")}</span>
                </Timeline.Item>
            );
        }
        return (
            <Timeline.Item key={ index }>
                {e.label}
                <span>{moment(e.date).format("DD MMM YYYY")}</span>
            </Timeline.Item>
        );
    })
}            
</Timeline>

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

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