Skip to content

Commit

Permalink
feat(space): codestyle
Browse files Browse the repository at this point in the history
  • Loading branch information
redzumi committed Dec 14, 2020
1 parent b85b459 commit fc6d760
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 34 deletions.
4 changes: 0 additions & 4 deletions packages/space/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@
"react": "^16.9.0"
},
"dependencies": {
"@alfalab/core-components-input": "^3.2.0",
"@alfalab/core-components-card-image": "^1.1.0",
"@alfalab/core-components-button": "^1.8.0",
"@alfalab/core-components-divider": "^1.3.4",
"classnames": "^2.2.6"
}
}
14 changes: 12 additions & 2 deletions packages/space/src/Component.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export const SIZES = ['s', 'm', 'l', 13, 37];
<Meta
title='Компоненты'
component={Space}
parameters={{ 'theme-switcher': { themes: ['click'] } }}
/>

<!-- Canvas -->
Expand Down Expand Up @@ -45,7 +44,6 @@ export const SIZES = ['s', 'm', 'l', 13, 37];
version={version}
package='@alfalab/core-components-space'
stage={1}
design='https://www.figma.com/file/KlFOLLkKO8rtvvQE3RXuhq/Click-Library?node-id=532%3A544'
/>

```tsx
Expand Down Expand Up @@ -78,6 +76,18 @@ import { Space } from '@alfalab/core-components-space';
</Space>
</Preview>

Пользовательское расстояние между карточками в виде массива [64, 32].

<Preview>
<div style={{ width: '700px' }}>
<Space direction="horizontal" size={[64, 32]} wrap>
<CardImage cardId='EG' />
<CardImage cardId='GQ' />
<CardImage cardId='SU' />
</Space>
</div>
</Preview>

С адаптивом.

<Preview>
Expand Down
34 changes: 16 additions & 18 deletions packages/space/src/Component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { ReactNode, Children, forwardRef } from 'react';
import classNames from 'classnames';
import Item from './Item';
import styles from './index.module.css';
import { Direction, Align, Size, SpaceContext } from './utils';
import { Direction, Align, Size } from './utils';

export type SpaceProps = {
/**
Expand Down Expand Up @@ -91,30 +91,32 @@ export const Space = forwardRef<HTMLDivElement, SpaceProps>((props, ref) => {
return null;
}

const directionCls = styles[direction];
const directionClassName = styles[direction];
const alignClassName = styles[align];

const alignCls = styles[`align_${align}`];

const containerCls = classNames(
const containerClassName = classNames(
styles.spaceContainer,
directionCls,
directionClassName,
{
[alignCls]: align,
[alignClassName]: align,
[styles.spaceContainerFullWidth]: fullWidth,
},
className,
);

const itemCls = classNames(styles.spaceItem, {
const itemClassName = classNames(styles.spaceItem, {
[styles.spaceItemFullWidth]: fullWidth,
});

const nodes = childNodes.map((child, i) => (
/* eslint-disable react/no-array-index-key */
<Item
className={itemCls}
key={`${itemCls}-${i}`}
className={itemClassName}
key={`${itemClassName}-${i}`}
direction={direction}
horizontalSize={horizontalSize}
verticalSize={verticalSize}
length={childNodes.length}
index={i}
wrap={wrap}
split={split}
Expand All @@ -124,24 +126,20 @@ export const Space = forwardRef<HTMLDivElement, SpaceProps>((props, ref) => {
/* eslint-enable */
));

const componentCls = classNames({
const componentClassName = classNames({
[styles.spaceFullWidth]: fullWidth,
});

return (
<div className={componentCls}>
<div className={componentClassName}>
<div
className={containerCls}
className={containerClassName}
style={{
...(wrap && { flexWrap: 'wrap', marginBottom: -verticalSize }),
}}
ref={ref}
>
<SpaceContext.Provider
value={{ horizontalSize, verticalSize, length: childNodes.length }}
>
{nodes}
</SpaceContext.Provider>
{nodes}
</div>
</div>
);
Expand Down
18 changes: 15 additions & 3 deletions packages/space/src/Item.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import * as React from 'react';
import { Direction, SpaceContext } from './utils';
import { Direction } from './utils';

export interface ItemProps {
className: string;
horizontalSize: number;
verticalSize: number;
length: number;
children: React.ReactNode;
index: number;
direction?: Direction;
Expand All @@ -11,8 +14,17 @@ export interface ItemProps {
}

const Item = (props: ItemProps) => {
const { className, direction, index, children, split, wrap } = props;
const { horizontalSize, verticalSize, length } = React.useContext(SpaceContext);
const {
className,
horizontalSize,
verticalSize,
length,
direction,
index,
children,
split,
wrap,
} = props;

let style: React.CSSProperties = {};

Expand Down
8 changes: 4 additions & 4 deletions packages/space/src/__snapshots__/Component.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Object {
class=""
>
<div
class="spaceContainer vertical align_start"
class="spaceContainer vertical start"
>
<div
class="spaceItem"
Expand Down Expand Up @@ -38,7 +38,7 @@ Object {
class=""
>
<div
class="spaceContainer vertical align_start"
class="spaceContainer vertical start"
>
<div
class="spaceItem"
Expand Down Expand Up @@ -124,7 +124,7 @@ Object {
class=""
>
<div
class="spaceContainer vertical align_start"
class="spaceContainer vertical start"
>
<div
class="spaceItem"
Expand All @@ -145,7 +145,7 @@ Object {
class=""
>
<div
class="spaceContainer vertical align_start"
class="spaceContainer vertical start"
>
<div
class="spaceItem"
Expand Down
6 changes: 3 additions & 3 deletions packages/space/src/index.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
flex-direction: column;
}

.align_center {
.center {
align-items: center;
}

.align_start {
.start {
align-items: flex-start;
}

.align_end {
.end {
align-items: flex-end;
}

Expand Down

0 comments on commit fc6d760

Please sign in to comment.