import * as PlusIcon from 'material-design-icons/content/svg/production/ic_add_24px.svg'; // Libs import * as React from 'react'; // Misc import { Select, SelectValue } from '../select/Select'; import { Text } from '../text/Text'; import InputGroup from '../inputGroup/InputGroup'; import { BaseInputProps, BaseInputState, BaseInput, defaultBaseTranslations, } from '../base/input/BaseInput'; import { Button } from '../button/Button'; import { withFormContext } from '../form/withFormContext'; import { withThemeContext } from '../themeProvider/withThemeContext'; export interface MoneyWithCurrency { value: number | undefined; currency: SelectValue; } export const defaultMoneyTranslations = { ...defaultBaseTranslations, cannotRemoveDefaultCurrency: 'Cannot remove default currency', removePrice: 'Remove price', addPrice: 'Add price', addNewCurrency: 'Add new currency', }; export type MoneyTranslations = Partial; export interface MoneyProps extends BaseInputProps { onChange?: (e: React.ChangeEvent) => void; onPricesChange: (prices: MoneyWithCurrency[]) => void; allowMultiple?: boolean; currencies: SelectValue[]; prices: MoneyWithCurrency[]; disableDelete?: boolean; } export interface MoneyState extends BaseInputState { } export class MoneyRaw extends BaseInput< MoneyProps, MoneyState, never, MoneyTranslations > { public static defaultProps = Object.assign({}, BaseInput.defaultProps, { type: 'money', allowMultiple: false, onChange: undefined, }); constructor(props: MoneyProps) { super(props, false); if (!props.currencies || !props.currencies.length) { throw Error('No currencies supplied to money input'); } this.state = Object.assign(this.state, { isValid: props.required ? props.prices.length > 0 : true, errors: props.required && props.prices.length === 0 ? [this.getTranslations(defaultMoneyTranslations).required] : [], handleValueChangeEnabled: false, }); const forcedCurrencies = props.currencies.filter((c) => c.forceSelected); const missingForced = forcedCurrencies.filter( (c) => !props.prices.find((p) => p.currency.value === c.value) ); if (missingForced.length > 0) { props.onPricesChange( props.prices.concat( missingForced.map((c) => ({ currency: c, value: undefined })) ) ); } this.subscribeSelf(props); } public render() { let unusedCurrencies = this.props.currencies; const translations = this.getTranslations(defaultMoneyTranslations); return (
{this.props.prices && this.props.prices.map((item, index) => { let currentCurrencies = this.props.currencies.filter( (c) => this.props.prices.filter( (priceCurrency, priceIndex) => priceIndex !== index && priceCurrency.currency.value === c.value ).length === 0 ); const currency = this.props.currencies.find( (c) => c.value === item.currency.value ); let retComponents = currentCurrencies.length ? (
{currentCurrencies.length > 1 ? (