lbry-desktop/ui/component/common/form-components/form-field-price.jsx

82 lines
2.1 KiB
React
Raw Normal View History

2018-03-26 23:32:43 +02:00
// @flow
import * as React from 'react';
import { FormField } from './form-field';
2019-04-24 16:02:08 +02:00
type FormPrice = {
amount: ?number,
currency: string,
};
2018-03-26 23:32:43 +02:00
type Props = {
2019-04-24 16:02:08 +02:00
price: FormPrice,
onChange: FormPrice => void,
2018-03-26 23:32:43 +02:00
placeholder: number,
min: number,
disabled: boolean,
name: string,
step: ?number,
};
export class FormFieldPrice extends React.PureComponent<Props> {
constructor(props: Props) {
super(props);
(this: any).handleAmountChange = this.handleAmountChange.bind(this);
(this: any).handleCurrencyChange = this.handleCurrencyChange.bind(this);
}
handleAmountChange(event: SyntheticInputEvent<*>) {
const { price, onChange } = this.props;
2019-04-24 16:02:08 +02:00
const amount = event.target.value ? parseFloat(event.target.value) : undefined;
2018-03-26 23:32:43 +02:00
onChange({
currency: price.currency,
amount,
2018-03-26 23:32:43 +02:00
});
}
handleCurrencyChange(event: SyntheticInputEvent<*>) {
const { price, onChange } = this.props;
onChange({
currency: event.target.value,
amount: price.amount,
});
}
render() {
2019-02-13 17:27:20 +01:00
const { price, placeholder, min, disabled, name, step } = this.props;
2018-03-26 23:32:43 +02:00
return (
2019-02-13 17:27:20 +01:00
<fieldset-group class="fieldset-group--smushed">
2019-08-19 18:10:48 +02:00
<FormField
name={`${name}_amount`}
label={__('Price')}
type="number"
className="form-field--price-amount"
min={min}
value={price.amount}
onWheel={e => e.preventDefault()}
2019-08-19 18:10:48 +02:00
onChange={this.handleAmountChange}
placeholder={placeholder || 5}
disabled={disabled}
step={step || 'any'}
/>
<FormField
label={__('Currency')}
name={`${name}_currency`}
type="select"
id={`${name}_currency`}
className="input--currency-select"
disabled={disabled}
onChange={this.handleCurrencyChange}
value={price.currency}
>
<option value="LBC">{__('LBRY Credits (LBC)')}</option>
<option value="USD">{__('US Dollars')}</option>
</FormField>
2019-02-13 17:27:20 +01:00
</fieldset-group>
2018-03-26 23:32:43 +02:00
);
}
}
export default FormFieldPrice;