Skip to content

Commit

Permalink
fixup! Feat(web-react): Introduce NavigationItem subcomponent #DS-1411
Browse files Browse the repository at this point in the history
  • Loading branch information
curdaj committed Jan 14, 2025
1 parent 4f5519d commit d88ae9a
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const Navigation = (props: SpiritNavigationProps): JSX.Element => {
const { styleProps, props: otherProps } = useStyleProps(restProps);

return (
<nav {...otherProps} className={classNames(classProps, styleProps.className)} style={styleProps.style}>
<nav {...otherProps} className={classNames(classProps.root, styleProps.className)} style={styleProps.style}>
<ul>{children}</ul>
</nav>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import React, { ElementType, forwardRef } from 'react';
import { useStyleProps } from '../../hooks';
import { PolymorphicRef, SpiritNavigationActionProps } from '../../types';
import { useNavigationActionProps } from './useNavigationActionProps';
import { useNavigationActionStyleProps } from './useNavigationActionStyleProps';
import { useNavigationStyleProps } from './useNavigationStyleProps';

const defaultProps: Partial<SpiritNavigationActionProps> = {
elementType: 'a',
Expand All @@ -22,15 +22,15 @@ const _NavigationAction = <E extends ElementType = 'a'>(
const ElementTag = propsWithDefaults.isDisabled ? 'span' : elementType;

const { navigationActionProps } = useNavigationActionProps(propsWithDefaults);
const { classProps, props: modifiedProps } = useNavigationActionStyleProps(restProps);
const { classProps, props: modifiedProps } = useNavigationStyleProps(restProps);
const { styleProps, props: otherProps } = useStyleProps(modifiedProps);

return (
<ElementTag
{...otherProps}
{...styleProps}
{...navigationActionProps}
className={classNames(classProps, styleProps.className)}
className={classNames(classProps.action, styleProps.className)}
ref={ref}
>
{children}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import classNames from 'classnames';
import React from 'react';
import { useStyleProps } from '../../hooks';
import { SpiritNavigationItemProps } from '../../types';
import { useNavigationItemStyleProps } from './useNavigationItemStyleProps';
import { useNavigationStyleProps } from './useNavigationStyleProps';

const defaultProps: Partial<SpiritNavigationItemProps> = {
isStretchingChildren: false,
Expand All @@ -14,11 +14,11 @@ const NavigationItem = (props: SpiritNavigationItemProps): JSX.Element => {
const propsWithDefaults = { ...defaultProps, ...props };
const { children, ...restProps } = propsWithDefaults;

const { classProps, props: modifiedProps } = useNavigationItemStyleProps(restProps);
const { classProps, props: modifiedProps } = useNavigationStyleProps(restProps);
const { styleProps, props: otherProps } = useStyleProps(modifiedProps);

return (
<li {...otherProps} className={classNames(classProps, styleProps.className)} style={styleProps.style}>
<li {...otherProps} className={classNames(classProps.item, styleProps.className)} style={styleProps.style}>
{children}
</li>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import '@testing-library/jest-dom';
import { render, screen } from '@testing-library/react';
import React from 'react';
import { classNamePrefixProviderTest } from '../../../../tests/providerTests/classNamePrefixProviderTest';
import { restPropsTest } from '../../../../tests/providerTests/restPropsTest';
import { stylePropsTest } from '../../../../tests/providerTests/stylePropsTest';
import { classNamePrefixProviderTest, restPropsTest, stylePropsTest } from '@local/tests';
import Navigation from '../Navigation';

describe('Navigation', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import '@testing-library/jest-dom';
import { render, screen } from '@testing-library/react';
import React from 'react';
import { classNamePrefixProviderTest } from '../../../../tests/providerTests/classNamePrefixProviderTest';
import { restPropsTest } from '../../../../tests/providerTests/restPropsTest';
import { stylePropsTest } from '../../../../tests/providerTests/stylePropsTest';
import { classNamePrefixProviderTest, restPropsTest, stylePropsTest } from '@local/tests';
import NavigationAction from '../NavigationAction';

describe('NavigationAction', () => {
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,34 @@
import { renderHook } from '@testing-library/react';
import { SpiritNavigationActionProps } from '../../../types';
import { useNavigationStyleProps } from '../useNavigationStyleProps';

describe('useNavigationStyleProps', () => {
it('should return defaults', () => {
const { result } = renderHook(() => useNavigationStyleProps());

expect(result.current.classProps).toBe('Navigation');
expect(result.current.classProps.root).toBe('Navigation');
expect(result.current.classProps.action).toBe('NavigationAction');
expect(result.current.classProps.item).toBe('NavigationItem');
});

it('should return disabled class', () => {
const props: SpiritNavigationActionProps = { isDisabled: true };
const { result } = renderHook(() => useNavigationStyleProps(props));

expect(result.current.classProps.action).toBe('NavigationAction NavigationAction--disabled');
});

it('should return selected class', () => {
const props = { isSelected: true };
const { result } = renderHook(() => useNavigationStyleProps(props));

expect(result.current.classProps.action).toBe('NavigationAction NavigationAction--selected');
});

it('should return stretchChildren class', () => {
const props = { isStretchingChildren: true };
const { result } = renderHook(() => useNavigationStyleProps(props));

expect(result.current.classProps.item).toBe('NavigationItem NavigationItem--stretchChildren');
});
});
1 change: 0 additions & 1 deletion packages/web-react/src/components/Navigation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@ export { default as NavigationAction } from './NavigationAction';
export { default as NavigationItem } from './NavigationItem';
export * from './useNavigationStyleProps';
export * from './useNavigationActionProps';
export * from './useNavigationActionStyleProps';

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,50 @@
import classNames from 'classnames';
import { ElementType } from 'react';
import { useClassNamePrefix } from '../../hooks';
import { SpiritNavigationActionProps, SpiritNavigationItemProps } from '../../types';

export interface NavigationStyles {
classProps: string;
export interface UseNavigationStyleProps {
isDisabled?: boolean;
isSelected?: boolean;
isStretchingChildren?: boolean;
}

export const useNavigationStyleProps = (): NavigationStyles => {
export interface UseNavigationStyleReturn<E extends ElementType = 'a'> {
classProps: {
root: string;
action: string;
item: string;
};
props: SpiritNavigationItemProps | SpiritNavigationActionProps<E>;
}

export const useNavigationStyleProps = <E extends ElementType = 'a'>({
isDisabled = false,
isSelected = false,
isStretchingChildren = false,
...restProps
}: UseNavigationStyleProps = {}): UseNavigationStyleReturn<E> => {
const navigationClass = useClassNamePrefix('Navigation');
const navigationActionClass = useClassNamePrefix('NavigationAction');
const navigationItemClass = useClassNamePrefix('NavigationItem');

const navigationActionDisabledClass = `${navigationActionClass}--disabled`;
const navigationActionSelectedClass = `${navigationActionClass}--selected`;
const navigationItemStretchingClass = `${navigationItemClass}--stretchChildren`;

const classProps = {
root: navigationClass,
action: classNames(navigationActionClass, {
[navigationActionDisabledClass]: isDisabled,
[navigationActionSelectedClass]: isSelected,
}),
item: classNames(navigationItemClass, {
[navigationItemStretchingClass]: isStretchingChildren,
}),
};

return {
classProps: navigationClass,
classProps,
props: restProps,
};
};

0 comments on commit d88ae9a

Please sign in to comment.