lucid-ui
Version:
A UI component library from AppNexus.
65 lines (55 loc) • 2.69 kB
JavaScript
import _isNumber from "lodash/isNumber";
import _isNull from "lodash/isNull";
import _isNil from "lodash/isNil";
import _last from "lodash/last";
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
export function onExpand(state) {
var selectedIndices = state.selectedIndices;
var focusedIndex = _last(selectedIndices);
return _objectSpread(_objectSpread({}, state), {}, {
isExpanded: true,
focusedIndex: _isNil(focusedIndex) ? null : focusedIndex
});
}
export function onCollapse(state) {
return _objectSpread(_objectSpread({}, state), {}, {
isExpanded: false
});
}
export function onSelect(state, optionIndex) {
return _objectSpread(_objectSpread({}, state), {}, {
selectedIndices: [optionIndex],
isExpanded: false
});
}
export function onFocusNext(state) {
var focusedIndex = state.focusedIndex;
var nextFocusedIndex = focusedIndex;
if (_isNull(focusedIndex)) {
nextFocusedIndex = 0;
} else if (_isNumber(focusedIndex)) {
nextFocusedIndex = focusedIndex + 1;
}
return _objectSpread(_objectSpread({}, state), {}, {
focusedIndex: nextFocusedIndex
});
}
export function onFocusPrev(state) {
var focusedIndex = state.focusedIndex;
var nextFocusedIndex = focusedIndex;
if (_isNull(focusedIndex) || focusedIndex === 0) {
nextFocusedIndex = null;
} else if (_isNumber(focusedIndex)) {
nextFocusedIndex = focusedIndex - 1;
}
return _objectSpread(_objectSpread({}, state), {}, {
focusedIndex: nextFocusedIndex
});
}
export function onFocusOption(state, optionIndex) {
return _objectSpread(_objectSpread({}, state), {}, {
focusedIndex: optionIndex
});
}