简体   繁体   English

自定义react Tab组件不接受“ dissabledTabs”道具

[英]Custom react Tab component is not accepting “dissabledTabs” prop

I have created a tab component as follows: 我创建了一个选项卡组件,如下所示:

tab/index.jsx 标签/ index.jsx

import React from 'react';
import TabHeader from './header';
import TabBody from './body';
import TabHeaderList from './header/list';
import TabBodyList from './body/list';
import style from './tab.css';

/**
 * Tabs are a great way to allow the user to switch between several pages that are full screen.
 */
export default class Tabs extends React.Component {
    constructor (props) {
        super(props);
        this.state = {
            currentTab: props.active || 0,
            disabledTabs: props.disabledTabs || []
        };
        this.ctx = {
            onChange: this.onChange
        };

        this.headerClick = this.headerClick.bind(this);
    }

    onChange(tab) {
        console.log('onChange: ', tab);
    }

    headerClick(e, onActivation, index){
        console.log("e:::::::::::::",e);
        e.stopPropagation();
        e.preventDefault();
        if(this.state.currentTab === index || this.state.disabledTabs.indexOf(index) !== -1) return false;
        let handled = null;
        if (onActivation) {
            handled = onActivation(index, this.state.currentTab);
        }
        if (handled) {
            e.preventDefault();
        } else {
            this.setState({currentTab: parseInt(index, 10)});
        }
    }

    render() {
        const type = this.props.type || 'default';
        let justified = this.props.align || '';
        if(justified === 'justified'){
            justified = 'flex1';
        }
        if(this.props.children.length !== 2) return null;

        const TabHeaderListTag = this.props.children[0];
        const TabBodyListTag = this.props.children[1];
        const {disabledTabs, currentTab } = this.state;

        console.log("---------------------");
        console.log(this.props);
        console.log("---------------------");
        console.log(this.state);
        console.log("---------------------");

        return (TabHeaderListTag.type === TabHeaderList && TabBodyListTag.type === TabBodyList ? (<div className={style.container}>
            <div className={style.headerContainer}>
                {
                    TabHeaderListTag.props.children.map((Header, i) => (Header.type === TabHeader ? (<button key={i} className={ [
                            'ft-sz-14',style[type],justified, style.header, disabledTabs.indexOf(i) !==-1?'disabled':'', currentTab === i ? style.active: ''
                        ].join(' ') } onClick={evnt => { this.headerClick(evnt, Header.props.onActivation, i) }} > {Header} </button>) : null)
                    )
                }
            </div>
            <div className={style.bodyContainer}>
                {
                    TabBodyListTag.props.children.map((Body, i) => (
                            Body.type === TabBody ? (<div key={i} className={`bg-white width100 ${(currentTab === i) ? '': 'display-none'}`}> {Body} </div>) : null
                        )
                    )
                }
            </div>
        </div>):null);
    }

};

tab/body/index.jsx 标签/体/ index.jsx

import React from 'react';

export default (props) => {
    return <div>{props.children}</div>;
};

tab/body/list/index.jsx 标签/体/列表/ index.jsx

import React from 'react';

export default (props) => {
    return props.children;
};

tab/header/index.jsx 选项卡/标题/ index.jsx

import React from 'react';

export default (props) => {
    return props.children;
};

tab/header/list/index.jsx 选项卡/标题/列表/ index.jsx

import React from 'react';

export default (props) => {
    return <div>{props.children}</div>;
};

But when I'm using it as follows: 但是当我如下使用它时:

class Sam extends React.Component{
    constructor(props){
        super(props);
        this.state = {
            value: 'Initial'
        };

        this.onChange = this.onChange.bind(this);
        this.onActivation = this.onActivation.bind(this);
    }

    onChange(e){
        console.log('onChange', e.target.value);
        this.setState({value: e.target.value});
    }

    onActivation(cur, old){
        console.log('onActivation', cur, old);
    }

    render() {
        return (<div>
            <input style={{marginLeft: '4rem'}} onChange={this.onChange}/>
            <Tab align="justified" type="gray" active={1} disabledTabs={[2]}>
                <TabHeaderList>
                    <TabHeader onActivation={this.onActivation}><Icon name="contact" width="25" height="25" color="gray" /></TabHeader>
                    <TabHeader><Icon name="dial" width="25" height="25" color="gray" /></TabHeader>
                    <TabHeader> <div>Disabled</div> </TabHeader>
                </TabHeaderList>
                <TabBodyList>
                    <TabBody className="pad-left-1r">
                        <h4>Testing Button {this.state.value}</h4>
                        <p>
                            Sample tab body
                        </p>
                    </TabBody>
                    <TabBody className="pad-left-1r">
                        <h4>Testing Button 2</h4>
                        <p>
                            This is just to demonstarate the use of tabs.
                            You could also make a `ul` inside like this:
                        </p>
                    </TabBody>
                    <TabBody className="pad-left-1r">
                        <h2>Im disabledTab</h2>
                    </TabBody>
                </TabBodyList>
            </Tab>
        </div>
        );
    }
}

it throws following error: 它引发以下错误:

Component(...): A valid React element (or null) must be returned. You Component(...): A valid React element (or null) must be returned. You may have returned undefined, an array or some other invalid object. Component(...): A valid React element (or null) must be returned. You may have returned undefined, an array or some other invalid object.

I'm unable to find where I'm returning undefined actually so it is throwing error. 我无法找到实际上要返回undefined的位置,因此会引发错误。

But when I'm removing disabledTabs={[2]} in component Sam it works fine ie as follows: 但是,当我在组件Sam删除disabledTabs={[2]} ,它可以正常工作,即如下所示:

<div>
            <input style={{marginLeft: '4rem'}} onChange={this.onChange}/>
            <Tab align="justified" type="gray" active={1} >
                <TabHeaderList>
                    <TabHeader onActivation={this.onActivation}><Icon name="contact" width="25" height="25" color="gray" /></TabHeader>
                    <TabHeader><Icon name="dial" width="25" height="25" color="gray" /></TabHeader>
                </TabHeaderList>
                <TabBodyList>
                    <TabBody className="pad-left-1r">
                        <h4>Testing Button {this.state.value}</h4>
                        <p>
                            Sample tab body
                        </p>
                    </TabBody>
                    <TabBody className="pad-left-1r">
                        <h4>Testing Button 2</h4>
                        <p>
                            This is just to demonstarate the use of tabs.
                            You could also make a `ul` inside like this:
                        </p>
                    </TabBody>
                </TabBodyList>
            </Tab>
        </div>

Changed some component as follows it worked: 更改了某些组件,如下所示:

tab/body/list/index.jsx 标签/体/列表/ index.jsx

import React from 'react';

export default (props) => {
    return <div>props.children</div>;
};

tab/header/index.jsx 选项卡/标题/ index.jsx

import React from 'react';

export default (props) => {
    return <div>props.children</div>;
};

These component was returning undefined incase of single child 如果是单个孩子,这些组件将返回未定义的

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

相关问题 React Router不接受组件作为道具 - React router not accepting component as prop React 组件不接受 JSX Element 作为道具 - React component is not accepting JSX Element as prop React Native中的自定义按钮组件不接受道具 - Custom button Component in React Native is not accepting props 如何处理自定义React事件作为组件道具 - How to handle Custom React Event as component prop React - 提供给 ButtonBase 的 `component` 属性无效。 请确保在此自定义组件中呈现 children 道具 - React - The `component` prop provided to ButtonBase is invalid. Please make sure the children prop is rendered in this custom component React Native:renderItem 组件到 Custom FlatList 作为道具? (如 Vue 插槽) - React Native: renderItem component to Custom FlatList as a prop? (like Vue slots) 将道具传递到自定义组件中 - Passing a prop into a custom component React 组件中的 children 属性 - children prop in React component 在 React Router 中重写自定义路由以使用渲染道具而不是组件道具 - Rewriting custom route to use render prop instead of component prop in React Router 如何将自定义组件作为“属性”传递给React.js渲染函数中的另一个自定义组件? - How to pass a custom component as a 'prop' to another custom component in React.js render function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM