// ============================================================================== // Copyright (C) 2019 - Philip Paquette, Steven Bocco // // This program is free software: you can redistribute it and/or modify it under // the terms of the GNU Affero General Public License as published by the Free // Software Foundation, either version 3 of the License, or (at your option) any // later version. // // This program is distributed in the hope that it will be useful, but WITHOUT // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more // details. // // You should have received a copy of the GNU Affero General Public License along // with this program. If not, see . // ============================================================================== import React from "react"; import {UTILS} from "../../diplomacy/utils/utils"; import {Button} from "./button"; export class Forms { static createOnChangeCallback(component, callback) { return (event) => { const value = UTILS.html.isCheckBox(event.target) ? event.target.checked : event.target.value; const fieldName = UTILS.html.isRadioButton(event.target) ? event.target.name : event.target.id; const update = {[fieldName]: value}; const state = Object.assign({}, component.state, update); if (callback) callback(state); component.setState(state); }; } static createOnSubmitCallback(component, callback, resetState) { return (event) => { if (callback) callback(Object.assign({}, component.state)); if (resetState) component.setState(resetState); event.preventDefault(); }; } static createOnResetCallback(component, onChangeCallback, resetState) { return (event) => { if (onChangeCallback) onChangeCallback(resetState); component.setState(resetState); if (event && event.preventDefault) event.preventDefault(); }; } static getValue(fieldValues, fieldName, defaultValue) { return fieldValues.hasOwnProperty(fieldName) ? fieldValues[fieldName] : defaultValue; } static createReset(title, large, onReset) { return