import React, { useState } from 'react'; import { IFablab } from '../../models/fablab'; import { FabInput } from '../base/fab-input'; import { FabButton } from '../base/fab-button'; import { Price } from '../../models/price'; import FormatLib from '../../lib/format'; declare let Fablab: IFablab; interface EditablePriceProps { price: Price, onSave: (price: Price) => void, } /** * Display the given price. * When the user clics on the price, switch to the edition mode to allow him modifying the price. */ export const EditablePrice: React.FC = ({ price, onSave }) => { const [edit, setEdit] = useState(false); const [tempPrice, setTempPrice] = useState(`${price.amount}`); /** * Saves the new price */ const handleValidateEdit = (): void => { const newPrice: Price = Object.assign({}, price); newPrice.amount = parseFloat(tempPrice); onSave(newPrice); toggleEdit(); }; /** * Enable or disable the edit mode */ const toggleEdit = (): void => { setEdit(!edit); }; return ( {!edit && {FormatLib.price(price.amount)}} {edit && } className="approve-button" onClick={handleValidateEdit} /> } className="cancel-button" onClick={toggleEdit} /> } ); };