1

I tried to use multiple tabs in react. But it doesn't seem working (please see the picture). I 'm not able to display the header for my Tabs neither the body (from match.json) that should contained a textbox field as input...

import React from "react";
import { Tab, Tabs as TabsComponent, TabList, TabPanel } from "react-tabs";
import "react-tabs/style/react-tabs.css";

const Tabs = ({ data }) => (
  <TabsComponent>
    <TabList>
      {data.map(({ name }, i) => (
        <Tab key={i}>{name}</Tab>
      ))}
    </TabList>
    {data.map(({ name }, i) => (
      <TabPanel key={i}>{name}</TabPanel>
    ))}
  </TabsComponent>
);

export default Tabs;

MenuItemDisplay:

 export default function MenuItemDisplay() {
  const { match } = JsonData;
  ...
        <Tabs data={match.questions[0]} />
      </div>
    </>
  );
}

match.json :

 {
  "match": [
    {
      ...
      "_ids": [
          "questions": [
            {
              "name": "Tell me about yourself ",
              "typeProof": ""
            },
            ...
          ],
        ]
      }
    ]
}
Zokulko
  • 211
  • 4
  • 25

1 Answers1

1

The data that you want to send to the Tabs component is the currently matched item's questions array.

const { menuId, itemId } = useParams();
const { match } = JsonData;

const matchData = match.find((el) => el._id_menu === menuId)?._ids ?? [];
const item = matchData.find((el) => el._id === itemId);

Here item is the current menu item being rendered, "Pea Soup", or item "123ml66" for example:

{
  "_id": "123ml66",
  "name": "Pea Soup",
  "description": "Creamy pea soup topped with melted cheese and sourdough croutons.",
  "dishes": [
    {
      "meat": "N/A",
      "vegetables": "pea"
    }
  ],
  "questions": [
    {
      "name": "Tell me about yourself ",
      "typeProof": ""
    },
    {
      "name": "Why our restaurant?",
      "typeProof": ""
    },
    {
      "name": "What hours are you available to work ?",
      "typeProof": "Planning"
    }
  ],
  "invited": [
    {
      "name": "Jose Luis",
      "location": "LA"
    },
    {
      "name": "Smith",
      "location": "New York"
    }
  ],
  "taste": "Good",
  "comments": "3/4",
  "price": "Low",
  "availability": 0,
  "trust": 1
},

Pass item.questions to the Tabs component.

<Tabs data={item.questions} />

...

const Tabs = ({ data }) => (
  <TabsComponent>
    <TabList>
      {data.map(({ name }, i) => (
        <Tab key={name}>Question {i + 1}</Tab>
      ))}
    </TabList>
    {data.map(({ name }) => (
      <TabPanel key={name}>{name}</TabPanel>
    ))}
  </TabsComponent>
);

Edit react-multiple-tabs-dont-work-headers-and-body-dont-diplayed

enter image description here

Drew Reese
  • 165,259
  • 14
  • 153
  • 181
  • Hi, thanks for your reply, I've modified and added the `TextInput` fields in `Tabs`, I don't know why if I write in textboxes for first question, and click on Question2, the content of my first question is saved in the second and third one. Plus, if I write it's only from the center of my box. I tried to display `typeProof` next to `Proof` but doesn't work. – Zokulko Apr 26 '22 at 18:44
  • @Zokulko Sounds like you are using the same state value for all the tab contents. As for the centering, it looks like everything it `text-align: center`. – Drew Reese Apr 26 '22 at 18:54
  • Is there a way to see in detail this file : `react-tabs/style/react-tabs.css` ? – Zokulko Apr 26 '22 at 19:48
  • 1
    @Zokulko OFC there is! It's in the repo: https://github.com/reactjs/react-tabs/blob/main/style/react-tabs.css. FYI, the `text-align: center;` rule is in the `styles.css` file in the sandbox, not anything that was imported. – Drew Reese Apr 26 '22 at 19:54
  • Thanks I've tried : `onChangeText={(value) => setText(value)}` but it's the same state value for all the tabs again [here](https://codesandbox.io/s/react-how-do-i-display-specific-data-from-json-that-fullfill-a-condition-forked-z37eu1?file=/src/Tabs.jsx:1191-1231)... – Zokulko Apr 26 '22 at 20:57
  • For my proof I want to allow only URL, do I have to use Regex to check if the input begins with http:// or https:// otherwise it's an error ? – Zokulko Apr 26 '22 at 21:03
  • 1
    @Zokulko Refactor the UI into a React component with its own state and is passed mapped props. https://codesandbox.io/s/react-multiple-tabs-dont-work-headers-and-body-dont-diplayed-49xv8i – Drew Reese Apr 26 '22 at 21:24
  • I would never think about it ! I've added [here](https://codesandbox.io/s/react-how-do-i-display-specific-data-from-json-that-fullfill-a-condition-forked-z37eu1?file=/src/Tabs.jsx) my Regex (find in another issue [here] (https://stackoverflow.com/questions/10570286/check-if-string-contains-url-anywhere-in-string-using-javascript), but doesn't work. I don't know if it's my regex or the code... – Zokulko Apr 26 '22 at 22:04
  • 1
    @Zokulko Is this a new issue/question? Are you wanting to set a boolean flag if the proof text contains a valid URL in it? – Drew Reese Apr 26 '22 at 23:40
  • I suppose I can open a new issue for that? But yes it's the idea! – Zokulko Apr 26 '22 at 23:51
  • Hi, I've opened new [issue1](https://stackoverflow.com/questions/72056009/react-saving-submit-content-page) (save inputs fields with react),[issue2](https://stackoverflow.com/questions/72080906/react-turn-my-code-using-formik-or-textfield-if-i-want-to-use-yup), [issue3](https://stackoverflow.com/questions/72078650/react-how-do-i-raise-a-warning-if-a-specific-input-field-is-empty-with-yup). The last 2 are slightly similar (raise an error with `Yup`) – Zokulko May 02 '22 at 12:40
  • I've opened a new [issue](https://stackoverflow.com/questions/72406097/react-sending-data-from-my-child-component-to-my-parent-one-is-not-working) – Zokulko May 31 '22 at 12:19