UNPKG

@zohodesk/components

Version:

Dot UI is a customizable React component library built to deliver a clean, accessible, and developer-friendly UI experience. It offers a growing set of reusable components designed to align with modern design systems and streamline application development

227 lines (208 loc) • 5.59 kB
/** * Methods ** */ import { getIsNewValueType, getIsNumberTyped, getIsDeleteTyped, getHourDetails, getMinuteDetails, getNoonDetails, getIsNoonValueTyped, getIsEmptyHour } from "./index"; import { getKeyValue, getIsEmptyValue } from "../../utils/Common"; export function getChangedHour(values, event, focusOrders, keyActions, { is24Hour = false } = {}) { const { keyCode, which } = event; const { hour = '' } = values; let isChanged = false; let newHour = Number.parseInt(hour) || 0; const { focusOrder } = focusOrders; let newFocusOrder = focusOrder; const isNewType = getIsNewValueType(focusOrders, keyActions); const { startPoint, endPoint } = getHourDetails(is24Hour); if (keyCode === 38) { //up arrow if (newHour === endPoint || getIsEmptyHour(newHour, is24Hour)) { isChanged = true; newHour = startPoint; } else if (!getIsEmptyHour(newHour, is24Hour)) { isChanged = true; newHour += 1; } } else if (keyCode === 40) { //down arrow if (newHour === startPoint || getIsEmptyHour(newHour, is24Hour)) { isChanged = true; newHour = endPoint; } else if (!getIsEmptyHour(newHour, is24Hour)) { isChanged = true; newHour -= 1; } } else if (getIsNumberTyped(event)) { const keyString = getKeyValue(event); let newValue = isNewType ? keyString : hour.toString() + keyString; newValue = Number.parseInt(newValue); if (isNewType) { isChanged = true; if (newValue * 10 > endPoint) { newFocusOrder += 1; } newHour = newValue; } else { isChanged = true; newFocusOrder += 1; if (newValue > endPoint) { newHour = endPoint; } else if (!newValue) { newHour = startPoint; } else { newHour = newValue; } } } else if (getIsDeleteTyped(keyCode)) { isChanged = true; newHour = ''; } return { hour: newHour, isChanged, focusOrder: newFocusOrder }; } export function getChangedMinute(values, event, focusOrders, keyActions) { const { keyCode, which } = event; const { minute = '' } = values; let isChanged = false; const isEmptyMinute = getIsEmptyValue(minute); let newMinute = isEmptyMinute ? '' : Number.parseInt(minute); const { focusOrder } = focusOrders; let newFocusOrder = focusOrder; const isNewType = getIsNewValueType(focusOrders, keyActions); const { startPoint, endPoint } = getMinuteDetails(); if (keyCode === 38) { //up arrow if (newMinute === endPoint || isEmptyMinute) { isChanged = true; newMinute = startPoint; } else if (!isEmptyMinute) { isChanged = true; newMinute += 1; } } else if (keyCode === 40) { //down arrow if (newMinute === startPoint || isEmptyMinute) { isChanged = true; newMinute = endPoint; } else if (newMinute) { isChanged = true; newMinute -= 1; } } else if (getIsNumberTyped(event)) { const keyString = getKeyValue(event); let newValue = isNewType ? keyString : minute.toString() + keyString; newValue = Number.parseInt(newValue); if (isNewType) { isChanged = true; if (newValue * 10 > endPoint) { newFocusOrder += 1; } newMinute = newValue; } else { isChanged = true; newFocusOrder += 1; if (newValue > endPoint) { newMinute = endPoint; } else { newMinute = newValue; } } } else if (getIsDeleteTyped(keyCode)) { isChanged = true; newMinute = ''; } return { minute: newMinute, isChanged, focusOrder: newFocusOrder }; } export function getChangedNoon(values, event, focusOrders /*keyActions*/ ) { const { keyCode, which } = event; const { noon = '' } = values; let isChanged = false; let newNoon = noon; const { focusOrder } = focusOrders; const newFocusOrder = focusOrder; // const isNewType = getIsNewValueType(focusOrders, keyActions); const { allowedValues } = getNoonDetails(); const [startPoint] = allowedValues; const endPoint = allowedValues[allowedValues.length - 1]; const currentNoonIndex = allowedValues.indexOf(newNoon); if (keyCode === 38) { //up arrow if (newNoon === endPoint || !newNoon) { isChanged = true; newNoon = startPoint; } else if (newNoon) { isChanged = true; newNoon = allowedValues[currentNoonIndex + 1]; } } else if (keyCode === 40) { //down arrow if (newNoon === startPoint || !newNoon) { isChanged = true; newNoon = endPoint; } else if (newNoon) { isChanged = true; newNoon = allowedValues[currentNoonIndex - 1]; } } else if (getIsNoonValueTyped(event)) { const keyString = getKeyValue(event).toUpperCase(); // let newValue = isNewType ? keyString : noon.toString() + keyString; const newValue = keyString; const needChangeNoon = newNoon.indexOf(newValue) === -1 ? true : false; if (needChangeNoon) { let newValueIndex = currentNoonIndex; allowedValues.some((val, index) => { if (val.indexOf(newValue) >= 0) { newValueIndex = index; return true; } return false; }); if (currentNoonIndex !== newValueIndex) { isChanged = true; newNoon = allowedValues[newValueIndex]; } } } else if (getIsDeleteTyped(keyCode)) { isChanged = true; newNoon = ''; } return { noon: newNoon, isChanged, focusOrder: newFocusOrder }; }