Skip to content

Commit

Permalink
refactor(accordion): lift up toggle and isOpen
Browse files Browse the repository at this point in the history
  • Loading branch information
yoshinorin committed Aug 8, 2023
1 parent 3857cfd commit dc0ebea
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
11 changes: 10 additions & 1 deletion components/content.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { useState } from 'react';
import { Content, ContentMeta } from '../models/content';
import { Insight } from '../models/insight';
import containerStyles from '../styles/components/container.module.scss';
import contentStyles from '../styles/components/content.module.scss';
import Accordion from './accordion';
import Accordion from './contentAccordion';

const ContentComponent: React.FunctionComponent<{ content: Content, insight: Insight | null }> = ({content, insight}) => {

Expand All @@ -22,11 +23,19 @@ const ContentComponent: React.FunctionComponent<{ content: Content, insight: Ins
insight: insight
}, null, 2);

const [isOpen, setIsOpen] = useState(false);

const toggle = () => {
setIsOpen(!isOpen);
};

return(
<article className={contentStyles.content}>
<div className={containerStyles.container} >
<div className={`${contentStyles['accordion-wrap']}`} >
<Accordion
open={isOpen}
onclick={toggle}
title="Attributes / Insight ▼"
content={json}
/>
Expand Down
13 changes: 3 additions & 10 deletions components/accordion.tsx → components/contentAccordion.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
import { useState } from 'react';
import styles from '../styles/accordion.module.scss';

const Accordion: React.FunctionComponent<{ title, content: string }> = ({title, content}) => {
const [isOpen, setIsOpen] = useState(false);

const toggle = () => {
setIsOpen(!isOpen);
};

const Accordion: React.FunctionComponent<{ open: boolean, onclick: () => void, title: string, content: string }> = ({open, onclick, title, content}) => {
return (
<div className={styles.accordion} >
<span className={`'unstyled' ${styles['menu-button']}`}
onClick={toggle}
onClick={onclick}
dangerouslySetInnerHTML={{ __html: title }} >
</span>
{isOpen && (
{open && (
<pre className="accordion-content">
{content}
</pre>
Expand Down

0 comments on commit dc0ebea

Please sign in to comment.