import React from 'react'; import PropTypes from 'prop-types'; import TranslatedComponent from './TranslatedComponent'; import Slider from '../components/Slider'; /** * Fuel slider * Requires an onChange() function of the form onChange(fuel), providing the fuel in tonnes, which is triggered on fuel level change */ export default class Fuel extends TranslatedComponent { static propTypes = { fuel: PropTypes.number.isRequired, fuelCapacity: PropTypes.number.isRequired, onChange: PropTypes.func.isRequired }; /** * Constructor * @param {Object} props React Component properties * @param {Object} context React Component context */ constructor(props, context) { super(props); this._fuelChange = this._fuelChange.bind(this); } /** * Update fuel level * @param {number} fuelLevel percentage level from 0 to 1 */ _fuelChange(fuelLevel) { const { fuel, fuelCapacity } = this.props; const newFuel = fuelLevel * fuelCapacity; // Only send an update if the fuel has changed significantly if (Math.round(fuel * 10) != Math.round(newFuel * 10)) { this.props.onChange(Math.round(newFuel * 10) / 10); } } /** * Render fuel slider * @return {React.Component} contents */ render() { const { language, onWindowResize, sizeRatio, tooltip, termtip } = this.context; const { formats, translate, units } = language; const { fuel, fuelCapacity } = this.props; return (

{translate('fuel carried')}: {formats.f1(fuel)}{units.T}

); } }