Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HDS-2115 fix header children #1304

Merged
merged 4 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 8 additions & 2 deletions packages/react/src/components/header/Header.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { HeaderLink } from './components/headerLink/HeaderLink';
import { HeaderNavigationMenu } from './components/headerNavigationMenu';
import { HeaderTheme } from './Header.type';
import { LanguageOption } from './LanguageContext';
import { IconSignout, IconSignin } from '../../icons';
import { IconSignout, IconSignin, IconSearch } from '../../icons';
import { Logo, logoFi, logoFiDark, logoSv, logoSvDark } from '../logo';
import { useMediaQueryLessThan } from '../../hooks/useMediaQuery';

Expand Down Expand Up @@ -260,6 +260,12 @@ const FullFeaturedActionBar = ({ I18n, lang, theme }) => {
<Header.ActionBarSubItem label="InfoFinland.fi" external href="www.example.com" lang="fi" />
</Header.ActionBarSubItemGroup>
</Header.LanguageSelector>
<Header.ActionBarButton
label="Haku"
icon={<IconSearch />}
id="action-bar-search"
onClick={() => action(`Search clicked`)()}
/>
<Header.ActionBarButton
label="Kirjaudu"
icon={<IconSignin />}
Expand Down Expand Up @@ -703,7 +709,7 @@ export const WithFullFeaturesCustomTheme = (args) => {
);
};

export const Login = (args) => {
export const WithUserMenu = (args) => {
const lang = 'fi';
const I18n = translations[lang];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import React, {
useEffect,
useMemo,
useRef,
ReactNode,
} from 'react';

import styles from './HeaderActionBar.module.scss';
Expand Down Expand Up @@ -252,24 +253,20 @@ export const HeaderActionBar = ({
titleProps.onKeyPress = handleKeyPress;
titleProps.onClick = handleClick;
}
const childrenWithId = (Array.isArray(children) ? children : [children])
.filter((item) => React.isValidElement(item))
.map((item) => item as ReactElement)
.map((item, idx) => ((item.props as HeaderActionBarItemProps)?.id ? item : { ...item, id: `item${idx}` }));
const validatedChildren = (Array.isArray(children) ? children : [children]).filter((item) =>
React.isValidElement(item),
);

const getFixedRightPosition = (item: ReactElement | ReactNode) =>
!!((item as ReactElement).props as HeaderActionBarItemProps).fixedRightPosition;

const childrenLeft =
childrenWithId.length > 1
? childrenWithId.filter(
(item) => React.isValidElement(item) && !(item.props as HeaderActionBarItemProps).fixedRightPosition,
)
: [childrenWithId];
const childrenRight =
childrenWithId.length > 1
? childrenWithId.filter(
(item) => React.isValidElement(item) && !!(item.props as HeaderActionBarItemProps).fixedRightPosition,
)
: [];
const { children: lsChildren, props: lsProps, componentExists } = getLanguageSelectorComponentProps(childrenWithId);
const childrenLeft = validatedChildren.filter((item) => !getFixedRightPosition(item));
const childrenRight = validatedChildren.filter((item) => getFixedRightPosition(item));
const {
children: lsChildren,
props: lsProps,
componentExists,
} = getLanguageSelectorComponentProps(validatedChildren);
const languageSelectorChildren = useMemo(() => {
return lsChildren ? getChildElementsEvenIfContainersInbetween(lsChildren) : null;
}, [lsChildren]);
Expand All @@ -293,8 +290,8 @@ export const HeaderActionBar = ({
{componentExists && (
<HeaderLanguageSelectorConsumer {...lsProps}>{languageSelectorChildren}</HeaderLanguageSelectorConsumer>
)}
{!isSmallScreen && childrenLeft}
{(hasNavigationContent || childrenWithId.length > 0) && isSmallScreen && (
{childrenLeft}
{(hasNavigationContent || childrenRight.length > 0) && isSmallScreen && (
<HeaderActionBarItem
id="Menu"
label={menuButtonLabel}
Expand All @@ -319,7 +316,7 @@ export const HeaderActionBar = ({
logo={logo}
logoProps={logoProps}
openFrontPageLinksAriaLabel={openFrontPageLinksAriaLabel}
actionBarItems={(Array.isArray(children) ? children : [children]) as HeaderActionBarItemProps[]}
actionBarItems={childrenRight as HeaderActionBarItemProps[]}
/>
)}
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export const LanguageButton = ({ value, label }: LanguageOption) => {

const renderLanguageNode = (language: LanguageOption, primary: boolean) => {
const Label = <LanguageButton key={language.value} value={language.value} label={language.label} />;
return primary ? Label : <HeaderActionBarSubItem label={Label} />;
return primary ? Label : <HeaderActionBarSubItem label={Label} key={language.value} />;
};

export const SimpleLanguageOptions = ({ languages }: { languages: LanguageOption[] }) => {
Expand Down
Loading