feat(currencies): add currency select.

This commit is contained in:
elforjani13
2022-02-17 21:10:54 +02:00
parent 7a27ea9a65
commit a6ec3f1be7
3 changed files with 78 additions and 1 deletions

View File

@@ -0,0 +1,76 @@
import React from 'react';
import intl from 'react-intl-universal';
import { MenuItem, Button } from '@blueprintjs/core';
import { FSelect } from '../Forms';
/**
*
* @param {*} query
* @param {*} currency
* @param {*} _index
* @param {*} exactMatch
* @returns
*/
const currencyItemPredicate = (query, currency, _index, exactMatch) => {
const normalizedTitle = currency.currency_code.toLowerCase();
const normalizedQuery = query.toLowerCase();
if (exactMatch) {
return normalizedTitle === normalizedQuery;
} else {
return (
`${currency.currency_code}. ${normalizedTitle}`.indexOf(
normalizedQuery,
) >= 0
);
}
};
/**
* @param {*} currency
* @returns
*/
const currencyItemRenderer = (currency, { handleClick, modifiers, query }) => {
return (
<MenuItem
active={modifiers.active}
disabled={modifiers.disabled}
text={currency.currency_name}
label={currency.currency_code.toString()}
key={currency.id}
onClick={handleClick}
/>
);
};
const currencySelectProps = {
itemPredicate: currencyItemPredicate,
itemRenderer: currencyItemRenderer,
valueAccessor: 'currency_code',
labelAccessor: 'currency_code',
};
/**
*
* @param {*} currencies
* @returns
*/
export function CurrencySelect({ currencies, ...rest }) {
return (
<FSelect
{...currencySelectProps}
{...rest}
items={currencies}
input={CurrnecySelectButton}
/>
);
}
/**
* @param {*} label
* @returns
*/
function CurrnecySelectButton({ label }) {
return <Button text={label ? label : intl.get('select_currency_code')} />;
}

View File

@@ -0,0 +1 @@
export * from './CurrencySelect'

View File

@@ -101,6 +101,7 @@ export * from './FeatureGuard';
export * from './ExchangeRate';
export * from './Branches';
export * from './Warehouses';
export * from './Currencies';
const Hint = FieldHint;
@@ -169,5 +170,4 @@ export {
BaseCurrency,
MoreMenuItems,
CustomSelectList,
};