Source

reactDom/requestSetValue.js

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var isFunc_1 = __importDefault(require("../validators/isFunc"));
/**
 * Set state function which called "setValue" of useState of React,
 * but it goes through a "canUpdate" function before the state is actually set.
 * The "canUpdate" function can determine whether the next state should be set.
 *
 * Example use case: There are multiple forms, while only one form can be open each time
 *
 * @param nextValue The next value to be set, e.g. "isOpen" state.
 * @param setValue The React state setter for setting the "nextValue", .e.g. "setIsOpen".
 * @param canUpdate The function which receives the "nextValue",
 *        to return a boolean indicating whether the update is allowed.
 * @category reactDom
 * @module requestSetValue
 */
function requestSetValue(nextValue, setValue, canUpdate) {
    // Handle if nextValue is a function
    if ((0, isFunc_1.default)(nextValue)) {
        // Handle if nextValue is a value
        return setValue(function (prevValue) {
            var next = nextValue(prevValue);
            // Request to update
            if (canUpdate(next))
                return next;
            // Return previous value if an update is not allowed
            return prevValue;
        });
    }
    // Request to update
    if (canUpdate(nextValue))
        return setValue(nextValue);
}
exports.default = requestSetValue;