@wordpress/components
Version:
UI components for WordPress.
88 lines (81 loc) • 2.49 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.buildPadInputStateReducer = buildPadInputStateReducer;
exports.from12hTo24h = from12hTo24h;
exports.from24hTo12h = from24hTo12h;
exports.inputToDate = inputToDate;
exports.validateInputElementTarget = validateInputElementTarget;
var _dateFns = require("date-fns");
var _actions = require("../input-control/reducer/actions");
/**
* External dependencies
*/
/**
* Internal dependencies
*/
/**
* Like date-fn's toDate, but tries to guess the format when a string is
* given.
*
* @param input Value to turn into a date.
*/
function inputToDate(input) {
if (typeof input === 'string') {
return new Date(input);
}
return (0, _dateFns.toDate)(input);
}
/**
* Converts a 12-hour time to a 24-hour time.
* @param hours
* @param isPm
*/
function from12hTo24h(hours, isPm) {
return isPm ? (hours % 12 + 12) % 24 : hours % 12;
}
/**
* Converts a 24-hour time to a 12-hour time.
* @param hours
*/
function from24hTo12h(hours) {
return hours % 12 || 12;
}
/**
* Creates an InputControl reducer used to pad an input so that it is always a
* given width. For example, the hours and minutes inputs are padded to 2 so
* that '4' appears as '04'.
*
* @param pad How many digits the value should be.
*/
function buildPadInputStateReducer(pad) {
return (state, action) => {
const nextState = {
...state
};
if (action.type === _actions.COMMIT || action.type === _actions.PRESS_UP || action.type === _actions.PRESS_DOWN) {
if (nextState.value !== undefined) {
nextState.value = nextState.value.toString().padStart(pad, '0');
}
}
return nextState;
};
}
/**
* Validates the target of a React event to ensure it is an input element and
* that the input is valid.
* @param event
*/
function validateInputElementTarget(event) {
var _ownerDocument$defaul;
// `instanceof` checks need to get the instance definition from the
// corresponding window object — therefore, the following logic makes
// the component work correctly even when rendered inside an iframe.
const HTMLInputElementInstance = (_ownerDocument$defaul = event.target?.ownerDocument.defaultView?.HTMLInputElement) !== null && _ownerDocument$defaul !== void 0 ? _ownerDocument$defaul : HTMLInputElement;
if (!(event.target instanceof HTMLInputElementInstance)) {
return false;
}
return event.target.validity.valid;
}
//# sourceMappingURL=utils.js.map