components.regions.ScrollSpy.Section.jsx Maven / Gradle / Ivy
The newest version!
import React, { forwardRef, useCallback } from 'react'
import get from 'lodash/get'
import classNames from 'classnames'
import PropTypes from 'prop-types'
import { RegionContent } from '../RegionContent'
import { Title } from './Title'
const Section = forwardRef(({ id, title, headlines, active, children }, forwardedRef) => {
/* simplifies auto testing */
const hasActiveChild = Array.isArray(children) &&
children.some(child => get(child, 'props.id', null) === active)
return (
{children}
)
})
Section.propTypes = {
id: PropTypes.string.isRequired,
title: PropTypes.string,
headlines: PropTypes.bool,
children: PropTypes.any,
active: PropTypes.string,
}
/**
* Рекурсивная отрисовка вложенных друг в друга секций и регистрация рефок на них
*/
export function SectionGroup({ id, title, headlines, content, menu: items, active, setSectionRef, pageId }) {
let contentElement = null
const hasContent = !(items?.length)
const setRef = useCallback((ref) => {
if (hasContent) {
setSectionRef(id, ref)
}
}, [id, hasContent, setSectionRef])
if (hasContent) {
contentElement = (
)
} else {
contentElement = items.map(item => (
))
}
return (
{contentElement}
)
}
SectionGroup.propTypes = {
id: PropTypes.string.isRequired,
title: PropTypes.string,
headlines: PropTypes.bool,
active: PropTypes.string,
setSectionRef: PropTypes.func.isRequired,
menu: PropTypes.array,
pageId: PropTypes.string,
content: PropTypes.shape(SectionGroup.propTypes),
}