@wordpress/components
Version:
UI components for WordPress.
209 lines (208 loc) • 6.79 kB
JavaScript
;
Object.defineProperties(exports, {
__esModule: { value: true },
[Symbol.toStringTag]: { value: "Module" }
});
// \0rolldown/runtime.js
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
key = keys[i];
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
get: ((k) => from[k]).bind(null, key),
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
});
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
value: mod,
enumerable: true
}) : target, mod));
let _wordpress_element = require("@wordpress/element");
let __state_ts = require("./state.cjs");
let __actions_ts = require("./actions.cjs");
__actions_ts = __toESM(__actions_ts);
// packages/components/src/input-control/reducer/reducer.ts
/**
* Prepares initialState for the reducer.
*
* @param initialState The initial state.
* @return Prepared initialState for the reducer
*/
function mergeInitialState(initialState = __state_ts.initialInputControlState) {
const { value } = initialState;
return {
...__state_ts.initialInputControlState,
...initialState,
initialValue: value
};
}
/**
* Creates the base reducer which may be coupled to a specializing reducer.
* As its final step, for all actions other than CONTROL, the base reducer
* passes the state and action on through the specializing reducer. The
* exception for CONTROL actions is because they represent controlled updates
* from props and no case has yet presented for their specialization.
*
* @param composedStateReducers A reducer to specialize state changes.
* @return The reducer.
*/
function inputControlStateReducer(composedStateReducers) {
return (state, action) => {
const nextState = { ...state };
switch (action.type) {
case __actions_ts.CONTROL:
nextState.value = action.payload.value;
nextState.isDirty = false;
nextState._event = void 0;
return nextState;
/**
* Keyboard events
*/
case __actions_ts.PRESS_UP:
nextState.isDirty = false;
break;
case __actions_ts.PRESS_DOWN:
nextState.isDirty = false;
break;
/**
* Drag events
*/
case __actions_ts.DRAG_START:
nextState.isDragging = true;
break;
case __actions_ts.DRAG_END:
nextState.isDragging = false;
break;
/**
* Input events
*/
case __actions_ts.CHANGE:
nextState.error = null;
nextState.value = action.payload.value;
if (state.isPressEnterToChange) nextState.isDirty = true;
break;
case __actions_ts.COMMIT:
nextState.value = action.payload.value;
nextState.isDirty = false;
break;
case __actions_ts.RESET:
nextState.error = null;
nextState.isDirty = false;
nextState.value = action.payload.value || state.initialValue;
break;
/**
* Validation
*/
case __actions_ts.INVALIDATE: nextState.error = action.payload.error;
}
nextState._event = action.payload.event;
/**
* Send the nextState + action to the composedReducers via
* this "bridge" mechanism. This allows external stateReducers
* to hook into actions, and modify state if needed.
*/
return composedStateReducers(nextState, action);
};
}
/**
* A custom hook that connects and external stateReducer with an internal
* reducer. This hook manages the internal state of InputControl.
* However, by connecting an external stateReducer function, other
* components can react to actions as well as modify state before it is
* applied.
*
* This technique uses the "stateReducer" design pattern:
* https://kentcdodds.com/blog/the-state-reducer-pattern/
*
* @param stateReducer An external state reducer.
* @param initialState The initial state for the reducer.
* @param onChangeHandler A handler for the onChange event.
* @return State, dispatch, and a collection of actions.
*/
function useInputControlStateReducer(stateReducer = __state_ts.initialStateReducer, initialState = __state_ts.initialInputControlState, onChangeHandler) {
const [state, dispatch] = (0, _wordpress_element.useReducer)(inputControlStateReducer(stateReducer), mergeInitialState(initialState));
const createChangeEvent = (type) => (nextValue, event) => {
dispatch({
type,
payload: {
value: nextValue,
event
}
});
};
const createKeyEvent = (type) => (event) => {
dispatch({
type,
payload: { event }
});
};
const createDragEvent = (type) => (payload) => {
dispatch({
type,
payload
});
};
/**
* Actions for the reducer
*/
const change = createChangeEvent(__actions_ts.CHANGE);
const invalidate = (error, event) => dispatch({
type: __actions_ts.INVALIDATE,
payload: {
error,
event
}
});
const reset = createChangeEvent(__actions_ts.RESET);
const commit = createChangeEvent(__actions_ts.COMMIT);
const dragStart = createDragEvent(__actions_ts.DRAG_START);
const drag = createDragEvent(__actions_ts.DRAG);
const dragEnd = createDragEvent(__actions_ts.DRAG_END);
const pressUp = createKeyEvent(__actions_ts.PRESS_UP);
const pressDown = createKeyEvent(__actions_ts.PRESS_DOWN);
const pressEnter = createKeyEvent(__actions_ts.PRESS_ENTER);
const currentStateRef = (0, _wordpress_element.useRef)(state);
const refPropsRef = (0, _wordpress_element.useRef)({
value: initialState.value,
onChangeHandler
});
(0, _wordpress_element.useLayoutEffect)(() => {
currentStateRef.current = state;
refPropsRef.current = {
value: initialState.value,
onChangeHandler
};
});
(0, _wordpress_element.useLayoutEffect)(() => {
if (currentStateRef.current._event !== void 0 && state.value !== refPropsRef.current.value && !state.isDirty) refPropsRef.current.onChangeHandler(state.value ?? "", { event: currentStateRef.current._event });
}, [state.value, state.isDirty]);
(0, _wordpress_element.useLayoutEffect)(() => {
if (initialState.value !== currentStateRef.current.value && !currentStateRef.current.isDirty) dispatch({
type: __actions_ts.CONTROL,
payload: { value: initialState.value ?? "" }
});
}, [initialState.value]);
return {
change,
commit,
dispatch,
drag,
dragEnd,
dragStart,
invalidate,
pressDown,
pressEnter,
pressUp,
reset,
state
};
}
exports.useInputControlStateReducer = useInputControlStateReducer;
//# sourceMappingURL=reducer.cjs.map