UNPKG

@focuson/form_components

Version:

Components that can be used by @focuson/forms

25 lines (24 loc) 1.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ActionButton = void 0; const jsx_runtime_1 = require("react/jsx-runtime"); const state_1 = require("@focuson/state"); const pages_1 = require("@focuson/pages"); const rest_1 = require("@focuson/rest"); const focuson_1 = require("@focuson/focuson"); const common_1 = require("./common"); function ActionButton({ id, state, action, text, enabledBy, paths, preCommands, postCommands, buttonType, history }) { const pathsAsLens = Object.fromEntries(Object.entries(paths).map(([name, fn]) => [name, fn(state)])); function onClick(e) { const errorPrefix = `Action button ${id}`; const config = (0, focuson_1.makeInputProcessorsConfig)(state.main, state.context); const processor = (0, rest_1.commandButtonCommandProcessors)(config)(state.main); const pre = (0, rest_1.processChangeCommandProcessor)(errorPrefix, processor, preCommands); const actions = action(state, id, pathsAsLens, history); const post = (0, rest_1.processChangeCommandProcessor)(errorPrefix, processor, postCommands); const txs = [...pre, ...actions, ...post]; state.massTransform((0, state_1.reasonFor)('ActionButton', 'onClick', id))(...txs); } return (0, pages_1.wrapWithErrors)(id, enabledBy, [], (errorProps, error) => (0, jsx_runtime_1.jsx)("button", Object.assign({ id: id, onClick: onClick }, errorProps, { className: (0, common_1.getButtonClassName)(buttonType), disabled: error }, { children: text }))); } exports.ActionButton = ActionButton;