Skip to content

Commit

Permalink
feat(core-components-amount): feat, change api, add PureAmount
Browse files Browse the repository at this point in the history
  • Loading branch information
stepancar committed Jun 26, 2020
1 parent b8b4efa commit f946776
Show file tree
Hide file tree
Showing 12 changed files with 183 additions and 71 deletions.
4 changes: 2 additions & 2 deletions packages/amount/src/__snapshots__/component.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

exports[`Amount should match snapshot 1`] = `
<span
class="component amount"
class="component"
data-test-id="test-id"
>
1
<span
class="minorPart"
class="minorPartAndCurrency"
>
,
00
Expand Down
68 changes: 54 additions & 14 deletions packages/amount/src/component.stories.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Meta, Story, Props, Title } from '@storybook/addon-docs/blocks';
import { Meta, Story, Props, Title, Preview } from '@storybook/addon-docs/blocks';
import { text, select, boolean, number } from '@storybook/addon-knobs';
import { Container, Row, Col } from 'storybook/blocks/grid';

import { Amount } from '.';
import { CURRENCY_CODES } from './utils/currencyCodes';
Expand All @@ -21,27 +22,66 @@ import { Amount } from '@alfalab/core-components-amount';
https://design.alfabank.ru/patterns/amount

<Story name='Песочница'>
<Amount
value={number('value', 100)}
currency={select('currency', Object.keys(CURRENCY_CODES), 'RUR')}
minority={number('minority', 100)}
hideMinority={boolean('hideMinority', false)}
className={select('className', '')}
dataTestId={text('dataTestId', '')}
pure={ boolean('pure', false) }
/>
{React.createElement(() => {
const value = number('value', 100);
const currency = select('currency', Object.keys(CURRENCY_CODES), 'RUR');
const minority = number('minority', 100);
const view = select('view', ['default', 'withZeroMinorPart'], 'default');
const className = select('className', '');
const dataTestId = text('dataTestId', '');
return (
<React.Fragment>
<Row>
<Col>Amount</Col>
<Col>Amount.Pure (Без стилей)</Col>
</Row>
<Row>
<Col>
<Amount
value={value}
currency={currency}
minority={minority}
view={view}
className={className}
dataTestId={dataTestId}
/>
</Col>
<Col>
<Amount.Pure
value={value}
currency={currency}
minority={minority}
view={view}
className={className}
dataTestId={dataTestId}
/>
</Col>
</Row>
</React.Fragment>
);
})}
</Story>

<Props of={Amount} />

## Контекст использования
<br/>

- `hideMinority="true"`: не показывает минорную часть, если её значение равно 0.

- `view="withZeroMinorPart"`: не показывает минорную часть, если её значение равно 0.
TODO: siebian хотели красивую таблицу которая показывает разницу
<Preview>
<Container>
<Amount value={1234500} currency='RUR' minority={100} hideMinority={true} />
<Row>
<Col>Значение</Col>
<Col>view="default"</Col>
<Col>view="withZeroMinorPart"</Col>
</Row>
<Row>
<Col>1234500</Col>
<Col><Amount value={1234500} currency='RUR' minority={100} /></Col>
<Col><Amount view="withZeroMinorPart" value={1234500} currency='RUR' minority={100} /></Col>
</Row>
<Amount value={1234500} currency='RUR' minority={100} />
<Amount value={1234567} currency='RUR' minority={100} hideMinority={true} />
</Container>
</Preview>
Expand Down Expand Up @@ -90,7 +130,7 @@ import { Amount } from '@alfalab/core-components-amount';
<Preview>
<Container>
<span>
<Amount value={1234500} currency='RUR' minority={100} pure={true} />
<Amount.Pure value={1234500} currency='RUR' minority={100} />
</span>
</Container>
</Preview>
59 changes: 9 additions & 50 deletions packages/amount/src/component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,19 @@ import React from 'react';
import cn from 'classnames';

import { formatAmount } from './utils';
import { AMOUNT_MAJOR_MINOR_PARTS_SEPARATOR, THINSP, CurrencyCodes } from './utils/currencyCodes';

import { AMOUNT_MAJOR_MINOR_PARTS_SEPARATOR, THINSP } from './utils/currencyCodes';
import { AmounProps } from './types';
import styles from './index.module.css';

type Props = {
/**
* Денежное значение в минорных единицах
*/
value: number;

/**
* Валюта
*/
currency: CurrencyCodes;

/**
* Идентификатор для систем автоматизированного тестирования
*/
minority: number;

// TODO: подумать над описанием
/**
* Отображение минорной части, если она нулевая
*/
hideMinority?: boolean;

/**
* Отключает стили
*/
pure?: boolean;

/**
* Дополнительный класс
*/
className?: string;

/**
* Идентификатор для систем автоматизированного тестирования
*/
dataTestId?: string;
};

/**
* Компонент для отображения суммы, согласно следующему гайдлайну:
* https://design.alfabank.ru/patterns/amount
*/
export const Amount: React.FC<Props> = ({
export const Amount: React.FC<AmounProps> = ({
value,
minority,
currency,
hideMinority = false,
pure = false,
view = 'default',
className,
dataTestId,
}) => {
Expand All @@ -63,17 +24,15 @@ export const Amount: React.FC<Props> = ({
code: currency,
minority,
},
withZeroMinorPart: view === 'withZeroMinorPart',
});

return (
<span
className={cn(styles.component, { [styles.amount]: !pure }, className)}
data-test-id={dataTestId}
>
<span className={cn(styles.component, className)} data-test-id={dataTestId}>
{majorPart}
<span className={cn(!pure && styles.minorPart)}>
{!hideMinority && minorPart && AMOUNT_MAJOR_MINOR_PARTS_SEPARATOR}
{!hideMinority && minorPart}
<span className={styles.minorPartAndCurrency}>
{minorPart && AMOUNT_MAJOR_MINOR_PARTS_SEPARATOR}
{minorPart}
{THINSP}
{currencySymbol}
</span>
Expand Down
5 changes: 1 addition & 4 deletions packages/amount/src/index.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@

.component {
white-space: nowrap;
}

.amount {
font-weight: bold;
}

.minorPart {
.minorPartAndCurrency {
opacity: 0.6;
font-weight: normal;
}
10 changes: 9 additions & 1 deletion packages/amount/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
export * from './component';
import { Amount as DefaultAmount } from './component';
import { PureAmount } from './pure/component';

type AmountType = typeof DefaultAmount & { Pure: typeof PureAmount };
// TODO: можно ли с этим что-то придумать?
const Amount: AmountType = DefaultAmount as AmountType;
Amount.Pure = PureAmount;

export { Amount };
14 changes: 14 additions & 0 deletions packages/amount/src/pure/__snapshots__/component.test.tsx.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`PureAmount should match snapshot 1`] = `
<span
class="component"
data-test-id="test-id"
>
1
,
00
</span>
`;
15 changes: 15 additions & 0 deletions packages/amount/src/pure/component.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from 'react';
import { render } from '@testing-library/react';
import { PureAmount } from '.';

describe('PureAmount', () => {
it('should match snapshot', () => {
const testId = 'test-id';

const { getByTestId } = render(
<PureAmount value={100} currency='RUR' minority={100} dataTestId={testId} />,
);
const amount = getByTestId(testId);
expect(amount).toMatchSnapshot();
});
});
40 changes: 40 additions & 0 deletions packages/amount/src/pure/component.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import React from 'react';
import cn from 'classnames';

import { formatAmount } from '../utils';
import { AMOUNT_MAJOR_MINOR_PARTS_SEPARATOR, THINSP } from '../utils/currencyCodes';
import { AmounProps } from '../types';
import styles from './index.module.css';

/**
* Компонент для отображения суммы, согласно следующему гайдлайну:
* https://design.alfabank.ru/patterns/amount
* Не содержит стилей кроме неразрывности строки
*/
export const PureAmount: React.FC<AmounProps> = ({
value,
minority,
currency,
view = 'default',
className,
dataTestId,
}) => {
const { majorPart, minorPart, currencySymbol } = formatAmount({
value,
currency: {
code: currency,
minority,
},
withZeroMinorPart: view === 'withZeroMinorPart',
});

return (
<span className={cn(styles.component, className)} data-test-id={dataTestId}>
{majorPart}
{minorPart && AMOUNT_MAJOR_MINOR_PARTS_SEPARATOR}
{minorPart}
{THINSP}
{currencySymbol}
</span>
);
};
3 changes: 3 additions & 0 deletions packages/amount/src/pure/index.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.component {
white-space: nowrap;
}
1 change: 1 addition & 0 deletions packages/amount/src/pure/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './component';
34 changes: 34 additions & 0 deletions packages/amount/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { CurrencyCodes } from '../utils/currencyCodes';

export type AmounProps = {
/**
* Денежное значение в минорных единицах
*/
value: number;

/**
* Валюта
*/
currency: CurrencyCodes;

/**
* Идентификатор для систем автоматизированного тестирования
*/
minority: number;

/**
* default - не отображаем копейки, если из значение 0
* withZeroMinorPart - отображаем копейки, даже если их значение равно 0
*/
view?: 'default' | 'withZeroMinorPart';

/**
* Дополнительный класс
*/
className?: string;

/**
* Идентификатор для систем автоматизированного тестирования
*/
dataTestId?: string;
};
1 change: 1 addition & 0 deletions packages/amount/src/utils/formatAmount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ type AmountType = {
code: CurrencyCodes;
minority: number;
};
withZeroMinorPart?: boolean;
};

export const formatAmount = ({ value, currency: { code, minority } }: AmountType) => {
Expand Down

0 comments on commit f946776

Please sign in to comment.