Close related section (#1207)

* Fix issue with closing accordian

* Use onSelect

* Remove change to AccoridanCard

* Fix className typo

* Restore conditional
This commit is contained in:
Tom 2024-05-31 16:20:52 +01:00 committed by GitHub
parent a7bc757514
commit 0576716c4a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 33 additions and 6 deletions

View File

@ -3,7 +3,9 @@ import AccordionContext from 'react-bootstrap/AccordionContext'
import { useAccordionButton } from 'react-bootstrap/AccordionButton' import { useAccordionButton } from 'react-bootstrap/AccordionButton'
import ArrowRight from '@/svgs/arrow-right-s-fill.svg' import ArrowRight from '@/svgs/arrow-right-s-fill.svg'
import ArrowDown from '@/svgs/arrow-down-s-fill.svg' import ArrowDown from '@/svgs/arrow-down-s-fill.svg'
import { useContext, useEffect } from 'react' import { useContext, useEffect, useState } from 'react'
const KEY_ID = '0'
function ContextAwareToggle ({ children, headerColor = 'var(--theme-grey)', eventKey, show }) { function ContextAwareToggle ({ children, headerColor = 'var(--theme-grey)', eventKey, show }) {
const { activeEventKey } = useContext(AccordionContext) const { activeEventKey } = useContext(AccordionContext)
@ -29,10 +31,20 @@ function ContextAwareToggle ({ children, headerColor = 'var(--theme-grey)', even
} }
export default function AccordianItem ({ header, body, headerColor = 'var(--theme-grey)', show }) { export default function AccordianItem ({ header, body, headerColor = 'var(--theme-grey)', show }) {
const [activeKey, setActiveKey] = useState()
useEffect(() => {
setActiveKey(show ? KEY_ID : null)
}, [show])
const handleOnSelect = () => {
setActiveKey(activeKey === KEY_ID ? null : KEY_ID)
}
return ( return (
<Accordion defaultActiveKey={show ? '0' : undefined}> <Accordion defaultActiveKey={activeKey} activeKey={activeKey} onSelect={handleOnSelect}>
<ContextAwareToggle show={show} eventKey='0'><div style={{ color: headerColor }}>{header}</div></ContextAwareToggle> <ContextAwareToggle show={show} eventKey={KEY_ID}><div style={{ color: headerColor }}>{header}</div></ContextAwareToggle>
<Accordion.Collapse eventKey='0' className='mt-2'> <Accordion.Collapse eventKey={KEY_ID} className='mt-2'>
<div>{body}</div> <div>{body}</div>
</Accordion.Collapse> </Accordion.Collapse>
</Accordion> </Accordion>

View File

@ -176,7 +176,14 @@ function TopLevelItem ({ item, noReply, ...props }) {
</article> </article>
{!noReply && {!noReply &&
<> <>
<Reply item={item} replyOpen placeholder={item.ncomments > 3 ? 'fractions of a penny for your thoughts?' : 'early comments get more zaps'} onCancelQuote={cancelQuote} onQuoteReply={quoteReply} quote={quote} /> <Reply
item={item}
replyOpen
placeholder={item.ncomments > 3 ? 'fractions of a penny for your thoughts?' : 'early comments get more zaps'}
onCancelQuote={cancelQuote}
onQuoteReply={quoteReply}
quote={quote}
/>
{ {
// Don't show related items for Saloon items (position is set but no subName) // Don't show related items for Saloon items (position is set but no subName)
(!item.position && item.subName) && (!item.position && item.subName) &&

View File

@ -31,7 +31,15 @@ export function ReplyOnAnotherPage ({ item }) {
) )
} }
export default forwardRef(function Reply ({ item, onSuccess, replyOpen, children, placeholder, onQuoteReply, onCancelQuote, quote }, ref) { export default forwardRef(function Reply ({
item,
replyOpen,
children,
placeholder,
onQuoteReply,
onCancelQuote,
quote
}, ref) {
const [reply, setReply] = useState(replyOpen || quote) const [reply, setReply] = useState(replyOpen || quote)
const me = useMe() const me = useMe()
const parentId = item.id const parentId = item.id