Skip to content

Commit

Permalink
Feat(web-react): Introduce isFluid prop to Container
Browse files Browse the repository at this point in the history
  • Loading branch information
crishpeen committed Dec 5, 2024
1 parent c706bc6 commit 2c364ed
Show file tree
Hide file tree
Showing 11 changed files with 125 additions and 15 deletions.
20 changes: 12 additions & 8 deletions packages/web-react/src/components/Container/Container.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@

import classNames from 'classnames';
import React from 'react';
import { useStyleProps } from '../../hooks/styleProps';
import { useClassNamePrefix } from '../../hooks/useClassNamePrefix';
import { ChildrenProps, StyleProps, TransferProps } from '../../types';
import { useStyleProps } from '../../hooks';
import { SpiritContainerProps } from '../../types';
import { useContainerStyleProps } from './useContainerStyleProps';

export interface ContainerProps extends ChildrenProps, StyleProps, TransferProps {}
const defaultProps: SpiritContainerProps = {
isFluid: false,
};

const Container = ({ children, ...restProps }: ContainerProps): JSX.Element => {
const containerClass = useClassNamePrefix('Container');
const { styleProps, props: otherProps } = useStyleProps(restProps);
const Container = (props: SpiritContainerProps): JSX.Element => {
const propsWithDefaults = { ...defaultProps, ...props };
const { children, ...restProps } = propsWithDefaults;
const { classProps, props: modifiedProps } = useContainerStyleProps(restProps);
const { styleProps, props: otherProps } = useStyleProps(modifiedProps);

return (
<div {...otherProps} {...styleProps} className={classNames(containerClass, styleProps.className)}>
<div {...otherProps} {...styleProps} className={classNames(classProps, styleProps.className)}>
{children}
</div>
);
Expand Down
17 changes: 15 additions & 2 deletions packages/web-react/src/components/Container/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,27 @@ Container centers your content horizontally and sets its max-width with horizont
<Container>Content</Container>
```

## Fluid Container

If you need a full-width container, you can use the `isFluid` prop.

```jsx
<Container isFluid>Content</Container>
```

## API

The components accept [additional attributes][readme-additional-attributes].
| Name | Type | Default | Required | Description |
| --------- | ------ | ------- | -------- | --------------------------- |
| `isFluid` | `bool` | `false` || If true, Container is fluid |

On top of the API options, the components accept [additional attributes][readme-additional-attributes].
If you need more control over the styling of a component, you can use [style props][readme-style-props]
and [escape hatches][readme-escape-hatches].

For detailed information see [Container](https://github.com/lmc-eu/spirit-design-system/blob/main/packages/web/src/scss/components/Container/README.md) component
For detailed information see [Container][web-container] component

[readme-additional-attributes]: https://github.com/lmc-eu/spirit-design-system/blob/main/packages/web-react/README.md#additional-attributes
[readme-escape-hatches]: https://github.com/lmc-eu/spirit-design-system/blob/main/packages/web-react/README.md#escape-hatches
[readme-style-props]: https://github.com/lmc-eu/spirit-design-system/blob/main/packages/web-react/README.md#style-props
[web-container]: https://github.com/lmc-eu/spirit-design-system/blob/main/packages/web/src/scss/components/Container/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { render } from '@testing-library/react';
import { render, screen } from '@testing-library/react';
import React from 'react';
import '@testing-library/jest-dom';
import { classNamePrefixProviderTest } from '../../../../tests/providerTests/classNamePrefixProviderTest';
Expand All @@ -7,16 +7,34 @@ import { stylePropsTest } from '../../../../tests/providerTests/stylePropsTest';
import Container from '../Container';

describe('Container', () => {
const text = 'Hello world';
const testId = 'flex-test-id';

classNamePrefixProviderTest(Container, 'Container');

stylePropsTest(Container);

restPropsTest(Container, 'div');

it('should render text children', () => {
const dom = render(<Container>Hello World</Container>);
it('should render', () => {
render(<Container data-testid={testId}>{text}</Container>);

expect(screen.getByText(text)).toBeInTheDocument();
});

it('should render with correct class', () => {
render(<Container data-testid={testId}>{text}</Container>);

expect(screen.getByTestId(testId)).toHaveClass('Container');
});

it('should render as fluid', () => {
render(
<Container isFluid data-testid={testId}>
{text}
</Container>,
);

const element = dom.container.querySelector('div') as HTMLElement;
expect(element.textContent).toBe('Hello World');
expect(screen.getByTestId(testId)).toHaveClass('Container Container--fluid');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { renderHook } from '@testing-library/react';
import { SpiritContainerProps } from '../../../types';
import { useContainerStyleProps } from '../useContainerStyleProps';

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

expect(result.current.classProps).toBe('Container');
});

it('should return fluid', () => {
const props = { isFluid: true } as SpiritContainerProps;
const { result } = renderHook(() => useContainerStyleProps(props));

expect(result.current.classProps).toBe('Container Container--fluid');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React from 'react';
import DocsBox from '../../../../docs/DocsBox';
import Container from '../Container';

const ContainerFluid = () => (
<Container isFluid>
<DocsBox>Container</DocsBox>
</Container>
);

export default ContainerFluid;
4 changes: 4 additions & 0 deletions packages/web-react/src/components/Container/demo/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ import React from 'react';
import ReactDOM from 'react-dom/client';
import DocsSection from '../../../../docs/DocsSections';
import ContainerDefault from './ContainerDefault';
import ContainerFluid from './ContainerFluid';

ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
<React.StrictMode>
<DocsSection title="Default" stackAlignment="stretch" container="heading-only">
<ContainerDefault />
</DocsSection>
<DocsSection title="Fluid" stackAlignment="stretch" container="heading-only">
<ContainerFluid />
</DocsSection>
</React.StrictMode>,
);
1 change: 1 addition & 0 deletions packages/web-react/src/components/Container/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client';

export * from './useContainerStyleProps';
export { default as Container } from './Container';
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,16 @@ const meta: Meta<typeof Container> = {
children: {
control: 'object',
},
isFluid: {
control: 'boolean',
table: {
defaultValue: { summary: 'false' },
},
},
},
args: {
children: <DocsBox>Container</DocsBox>,
isFluid: false,
},
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import classNames from 'classnames';
import { useClassNamePrefix } from '../../hooks';
import { SpiritContainerProps, ContainerProps } from '../../types';

export interface ContainerStyles {
/** className props */
classProps: string;
/** props to be passed to the element */
props: ContainerProps;
}

export function useContainerStyleProps(props: SpiritContainerProps): ContainerStyles {
const { isFluid, ...modifiedProps } = props;

const containerClass = useClassNamePrefix('Container');
const containerFluidClass = `${containerClass}--fluid`;
const classProps = classNames(containerClass, {
[containerFluidClass]: isFluid,
});

return {
classProps,
props: modifiedProps,
};
}
7 changes: 7 additions & 0 deletions packages/web-react/src/types/container.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { ChildrenProps, StyleProps, TransferProps } from './shared';

export interface ContainerProps extends ChildrenProps, StyleProps, TransferProps {}

export interface SpiritContainerProps extends ContainerProps {
isFluid?: boolean;
}
1 change: 1 addition & 0 deletions packages/web-react/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export * from './breadcrumbs';
export * from './button';
export * from './checkbox';
export * from './collapse';
export * from './container';
export * from './divider';
export * from './dropdown';
export * from './fieldGroup';
Expand Down

0 comments on commit 2c364ed

Please sign in to comment.