reactjsfieldsetprimereact

Toggle FieldSet from an other component


How to make PrimeReact FieldSet controllable from an other (parent) component on React?

I have a boolean object toggled, i would like to pass it into child component.

When toggled = true, the FieldSet should be opened.

When toggled = false, the FieldSet should be closed.

Edit interesting-maxwell-9s1vx1

import React from "react";
import { Fieldset } from "primereact/fieldset";

const FieldsetDemo = () => {
  return (
    <div>
      <div className="card">
        <h5>Toggleable</h5>
        <Fieldset legend="Header" toggleable>
          <p>
            Lorem ipsum dolor sit amet.
          </p>
        </Fieldset>
      </div>
    </div>
  );
};

Solution

  • Its easy. Working code sandbox: https://codesandbox.io/s/quizzical-snow-so91tv?file=/src/demo/FieldsetDemo.js

    const FieldsetDemo = (props) => {
      return (
        <div>
          <div className="card">
            <h5>Toggleable</h5>
            <Fieldset legend="Header" toggleable collapsed={props.collapsed}>
              <p>
                Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
                eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim
                ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
                aliquip ex ea commodo consequat. Duis aute irure dolor in
                reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
                pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
                culpa qui officia deserunt mollit anim id est laborum.
              </p>
            </Fieldset>
          </div>
        </div>
      );
    };
    
    const rootElement = document.getElementById("root");
    ReactDOM.render(<FieldsetDemo collapsed={true} />, rootElement);