Skip to content

Commit

Permalink
feat(core-components-money-input): tune to latest requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
stepancar committed Jun 16, 2020
1 parent 251cb21 commit 62fcaf4
Show file tree
Hide file tree
Showing 6 changed files with 297 additions and 144 deletions.
1 change: 1 addition & 0 deletions packages/money-input/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
},
"dependencies": {
"@alfalab/core-components-input": "^1.3.1",
"@alfalab/core-components-form-control": "^1.3.1",
"text-mask-core": "^5.1.2"
}
}
12 changes: 12 additions & 0 deletions packages/money-input/src/Component.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { MoneyInput } from '@alfalab/core-components-money-input';
value={number('value', 123456)}
currency={text('currency', 'RUR')}
minorUnits={number('minorUnits', 100)}
bold={boolean('bold', true)}
block={boolean('block', false)}
size={select('size', ['s', 'm', 'l'], 's')}
disabled={boolean('disabled', false)}
Expand All @@ -54,6 +55,10 @@ import { MoneyInput } from '@alfalab/core-components-money-input';
<Preview>
{React.createElement(() => {
const [value, setValue] = React.useState(10000);
const [suggests] = React.useState([
{ currency: 'RUR', minorUnits: 100, value: 15600 },
{ currency: 'RUR', minorUnits: 100, value: 500 },
]);
const handleChange = (event, payload) => {
setValue(payload.value);
};
Expand All @@ -69,6 +74,13 @@ import { MoneyInput } from '@alfalab/core-components-money-input';
<Col>
{ value }
</Col>
{suggests.map((s) => (
<button
onClick={() => { setValue(s.value) }}
>
Установить {s.value}
</button>
))}
</Row>
</Container>
);
Expand Down
54 changes: 28 additions & 26 deletions packages/money-input/src/Component.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import cn from 'classnames';
import React, { useState, useEffect } from 'react';
import { Input, InputProps } from '@alfalab/core-components-input';
import { FormControl } from '@alfalab/core-components-form-control';

import { CURRENCY_CODES } from './utils/currencyCodes';
import { CURRENCY_CODES, THINSP } from './utils/currencyCodes';
import { getFormatedValue, getAmountValueFromStr, formatAmount } from './utils';
import styles from './index.module.css';

Expand All @@ -22,6 +23,11 @@ export type MoneyInputProps = Omit<InputProps, 'onChange' | 'rightAddons'> & {
*/
minorUnits?: number;

/**
* Жир
*/
bold?: boolean;

/**
* Дополнительный класс
*/
Expand Down Expand Up @@ -59,6 +65,7 @@ export const MoneyInput: React.FC<MoneyInputProps> = ({
minorUnits = 100,
currency = 'RUR',
label = 'Сумма',
bold = true,
className,
dataTestId,
onChange,
Expand Down Expand Up @@ -141,31 +148,26 @@ export const MoneyInput: React.FC<MoneyInputProps> = ({
};

return (
<Input
{...restProps}
label={label}
value={inputValue}
rightAddons={<span className={styles.currency}>{currencySymbol}</span>}
className={className}
inputClassName={cn(styles.input)}
onChange={handleChange}
dataTestId={dataTestId}
/>
);
};

/**
* Заготовка если минорные единицы нужно красить иначп
*/
export const ContentEditableMoneyInput = () => {
function handleInput(e: React.ChangeEvent<HTMLInputElement>) {
console.log(e);
}

return (
<div contentEditable={true} onInput={handleInput}>
1234
<span>,56</span>
<div className={cn({ [styles.bold]: bold })}>
<FormControl {...restProps} label={label} className={cn(styles.fakeValueWithCurrency)}>
{inputValue}
{inputValue && (
<span className={styles.currency}>
{THINSP}
{currencySymbol}
</span>
)}
</FormControl>

<Input
{...restProps}
label={label}
value={inputValue}
className={cn(styles.component, className)}
inputClassName={styles.input}
onChange={handleChange}
dataTestId={dataTestId}
/>
</div>
);
};

0 comments on commit 62fcaf4

Please sign in to comment.