简体   繁体   English

ReactJS Bootstrap 折叠个人卡片

[英]ReactJS Bootstrap Collapse Individual Card

ReacJS Has an example for a single collapse: ReacJS 有一个单折叠的例子:

function Example() {
  const [open, setOpen] = useState(false);

  return (
    <>
      <Button
        onClick={() => setOpen(!open)}
        aria-controls="example-collapse-text"
        aria-expanded={open}
      >
        click
      </Button>
      <Collapse in={open}>
        <div id="example-collapse-text">
          Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus
          terry richardson ad squid. Nihil anim keffiyeh helvetica, craft beer
          labore wes anderson cred nesciunt sapiente ea proident.
        </div>
      </Collapse>
    </>
  );
}

render(<Example />);

But say I was iterating over an array of objects where each object is a Card and I want the button click on an individual card to only open that card and not all cards.但是假设我正在迭代一个对象数组,其中每个对象都是一张卡片,我希望单击单个卡片的按钮仅打开该卡片而不是所有卡片。

Currently I have this:目前我有这个:

return (
        <div>
          <div className='card-detail'>All Cards ({this.props.cardDetails.details.length})</div>
          {this.props.cardDetails.details.map((detail, index) => {
            return (
              <CardStyle key={index}>
                <Container>
                  <CardRow>
                    <FlexCol>
                      <Button onClick={() => this.setState({ open: !this.state.open })} 
                      aria-controls="example-collapse-text" 
                      aria-expanded={this.state.open} 
                      variant="light">Expand</Button>
                    </FlexCol>
                  </CardRow>
                </Container>
                <CardDetailInfo save={this.saveCard} confirm={this.confirmCard} open={this.state.open} detail={detail}></CardDetailInfo>
              </CardStyle>)
          })}
        </div>
      )


The collapse code is an other class; 

class CardDetailInfo extends Component {
  constructor(props) {
    super(props);
  }
  render() {
      return (
<Collapse in={this.props.open}>
</Collapse>
)

...

ok so I was able to get it to work:好的,所以我能够让它工作:

...
  constructor(props) {
    super(props);
    this.state = {
      open: []    
    }
    this.handleClick = this.handleClick.bind(this);
  }
    componentDidUpdate(oldProps) {
      if(oldProps.open !== this.props.open) {
        this.setState({
          open: this.props.open
        });
      }
    }

  handleClick(event) {
    const id = event.target.id;
    let local_open = this.state.open;
    local_open[id] = !local_open[id]
    this.setState({open: local_open });
  }
...

<Button id={index} onClick={this.handleClick}
variant="light">Expand</Button>

In another class I use if statements

if (this.props.open.length == 0 || this.props.open[this.props.index]==false)
default view

if(this.props.open[this.props.index]==true)
expand view

To have a toggle in each <Collapse> OR keep an array of open status for your list.要在每个<Collapse>进行切换,或者为您的列表保留一组打开状态。 Here are some options you have:以下是您的一些选择:

1. Single Collapse: 1. 单次折叠:

import React, { useState } from 'react';

function Collapse (props) {
  const [open, setOpen] = useState(props.in);
  
  useEffect(() => {
    setOpen(props.in);
  }, [props.in]);

  return (
     ...
  );
}

export default Collapse;

2. Add a new field to cardDetails called "open" and change that property when needed. 2. 向cardDetails添加一个名为“open”的新字段,并在需要时更改该属性。

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

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