New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@zendeskgarden/container-selection

Package Overview
Dependencies
Maintainers
1
Versions
65
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@zendeskgarden/container-selection - npm Package Compare versions

Comparing version 1.3.6 to 1.3.7

11

CHANGELOG.md

@@ -6,2 +6,13 @@ # Change Log

## [1.3.7](https://github.com/zendeskgarden/react-containers/compare/@zendeskgarden/container-selection@1.3.6...@zendeskgarden/container-selection@1.3.7) (2020-10-23)
### Bug Fixes
* **selection:** update reducer to not run side effect ([#241](https://github.com/zendeskgarden/react-containers/issues/241)) ([1107475](https://github.com/zendeskgarden/react-containers/commit/11074759f12af08b9b0a36fd3d5b52cc4a735dfb))
## [1.3.6](https://github.com/zendeskgarden/react-containers/compare/@zendeskgarden/container-selection@1.3.5...@zendeskgarden/container-selection@1.3.6) (2020-06-10)

@@ -8,0 +19,0 @@

276

dist/index.cjs.js

@@ -12,8 +12,11 @@ /**

function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
var react = require('react');
var React = require('react');
var containerUtilities = require('@zendeskgarden/container-utilities');
var PropTypes = _interopDefault(require('prop-types'));
var PropTypes = require('prop-types');
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
var PropTypes__default = /*#__PURE__*/_interopDefaultLegacy(PropTypes);
function _objectWithoutPropertiesLoose(source, excluded) {

@@ -49,92 +52,21 @@ if (source == null) return {};

switch (action.type) {
case 'END':
case 'HOME':
case 'FOCUS':
{
if (action.onFocus) {
if (action.payload !== action.focusedItem) {
action.onFocus(action.payload);
}
return state;
}
return _extends({}, state, {
focusedItem: action.payload
});
}
case 'INCREMENT':
{
var controlledFocusedItem = containerUtilities.getControlledValue(action.focusedItem, state.focusedItem);
var controlledSelectedItem = containerUtilities.getControlledValue(action.selectedItem, state.selectedItem);
var currentItemIndex = controlledFocusedItem === undefined ? action.items.indexOf(controlledSelectedItem) : action.items.indexOf(controlledFocusedItem);
var newFocusedItem = action.items[(currentItemIndex + 1) % action.items.length];
if (action.onFocus) {
action.onFocus(newFocusedItem);
return state;
}
return _extends({}, state, {
focusedItem: newFocusedItem
});
}
case 'DECREMENT':
{
var _controlledFocusedItem = containerUtilities.getControlledValue(action.focusedItem, state.focusedItem);
var _controlledSelectedItem = containerUtilities.getControlledValue(action.selectedItem, state.selectedItem);
var _currentItemIndex = _controlledFocusedItem === undefined ? action.items.indexOf(_controlledSelectedItem) : action.items.indexOf(_controlledFocusedItem);
var _newFocusedItem = action.items[(_currentItemIndex + action.items.length - 1) % action.items.length];
if (action.onFocus) {
action.onFocus(_newFocusedItem);
return state;
}
return _extends({}, state, {
focusedItem: _newFocusedItem
focusedItem: action.payload
});
}
case 'HOME':
case 'MOUSE_SELECT':
{
if (action.onFocus) {
action.onFocus(action.items[0]);
return state;
}
return _extends({}, state, {
focusedItem: action.items[0]
selectedItem: action.payload,
focusedItem: undefined
});
}
case 'END':
{
if (action.onFocus) {
action.onFocus(action.items[action.items.length - 1]);
return state;
}
return _extends({}, state, {
focusedItem: action.items[action.items.length - 1]
});
}
case 'MOUSE_SELECT':
{
var isSelectControlled = false;
var isFocusControlled = false;
if (action.onSelect) {
action.onSelect(action.payload);
isSelectControlled = true;
}
if (action.onFocus) {
action.onFocus(undefined);
isFocusControlled = true;
}
if (isFocusControlled && isSelectControlled) {
return state;
}
var updatedState = _extends({}, state);
if (!isSelectControlled) {
updatedState.selectedItem = action.payload;
}
if (!isFocusControlled) {
updatedState.focusedItem = undefined;
}
return updatedState;
}
case 'KEYBOARD_SELECT':
{
if (action.onSelect) {
action.onSelect(action.payload);
return state;
}
return _extends({}, state, {

@@ -146,6 +78,2 @@ selectedItem: action.payload

{
if (action.onFocus) {
action.onFocus(undefined);
return state;
}
return _extends({}, state, {

@@ -171,5 +99,7 @@ focusedItem: undefined

onFocus = _ref.onFocus;
var isSelectedItemControlled = selectedItem !== undefined;
var isFocusedItemControlled = focusedItem !== undefined;
var refs = [];
var items = [];
var _useReducer = react.useReducer(stateReducer, {
var _useReducer = React.useReducer(stateReducer, {
selectedItem: selectedItem,

@@ -182,3 +112,3 @@ focusedItem: focusedItem

var controlledSelectedItem = containerUtilities.getControlledValue(selectedItem, state.selectedItem);
react.useEffect(function () {
React.useEffect(function () {
if (controlledFocusedItem !== undefined) {

@@ -189,9 +119,11 @@ var focusedIndex = items.indexOf(controlledFocusedItem);

}, [controlledFocusedItem]);
react.useEffect(function () {
React.useEffect(function () {
if (selectedItem === undefined && defaultSelectedIndex !== undefined) {
dispatch({
type: 'KEYBOARD_SELECT',
payload: items[defaultSelectedIndex],
onSelect: onSelect
});
onSelect && onSelect(items[defaultSelectedIndex]);
if (!isSelectedItemControlled) {
dispatch({
type: 'KEYBOARD_SELECT',
payload: items[defaultSelectedIndex]
});
}
}

@@ -207,3 +139,3 @@ }, []);

'data-garden-container-id': 'containers.selection',
'data-garden-container-version': '1.3.6'
'data-garden-container-version': '1.3.7'
}, other);

@@ -246,40 +178,62 @@ };

}, _extends2[selectedAriaKey] = selectedAriaKey ? isSelected : undefined, _extends2[refKey] = focusRef, _extends2.onFocus = containerUtilities.composeEventHandlers(onFocusCallback, function () {
dispatch({
type: 'FOCUS',
payload: item,
focusedItem: focusedItem,
onFocus: onFocus
});
onFocus && onFocus(item);
if (!isFocusedItemControlled) {
dispatch({
type: 'FOCUS',
payload: item
});
}
}), _extends2.onBlur = function onBlur(e) {
if (e.target.tabIndex === 0) {
if (!isFocusedItemControlled) {
dispatch({
type: 'EXIT_WIDGET'
});
}
onFocus && onFocus();
}
}, _extends2.onClick = containerUtilities.composeEventHandlers(onClick, function () {
onSelect && onSelect(item);
onFocus && onFocus();
if (!isSelectedItemControlled) {
dispatch({
type: 'EXIT_WIDGET',
onFocus: onFocus
type: 'MOUSE_SELECT',
payload: item
});
}
}, _extends2.onClick = containerUtilities.composeEventHandlers(onClick, function () {
dispatch({
type: 'MOUSE_SELECT',
payload: item,
onSelect: onSelect,
onFocus: onFocus
});
}), _extends2.onKeyDown = containerUtilities.composeEventHandlers(onKeyDown, function (e) {
var nextIndex;
var currentIndex;
if (isFocusedItemControlled) {
currentIndex = items.indexOf(focusedItem);
} else {
currentIndex = items.indexOf(state.focusedItem || state.selectedItem);
}
var onIncrement = function onIncrement() {
nextIndex = currentIndex + 1;
if (currentIndex === items.length - 1) {
nextIndex = 0;
}
!isFocusedItemControlled && dispatch({
type: 'INCREMENT',
payload: items[nextIndex]
});
onFocus && onFocus(items[nextIndex]);
};
var onDecrement = function onDecrement() {
nextIndex = currentIndex - 1;
if (currentIndex === 0) {
nextIndex = items.length - 1;
}
!isFocusedItemControlled && dispatch({
type: 'DECREMENT',
payload: items[nextIndex]
});
onFocus && onFocus(items[nextIndex]);
};
if (e.keyCode === containerUtilities.KEY_CODES.UP && verticalDirection || e.keyCode === containerUtilities.KEY_CODES.LEFT && horizontalDirection) {
if (rtl && !verticalDirection) {
dispatch({
type: 'INCREMENT',
items: items,
focusedItem: focusedItem,
selectedItem: selectedItem,
onFocus: onFocus
});
onIncrement();
} else {
dispatch({
type: 'DECREMENT',
items: items,
focusedItem: focusedItem,
selectedItem: selectedItem,
onFocus: onFocus
});
onDecrement();
}

@@ -289,39 +243,33 @@ e.preventDefault();

if (rtl && !verticalDirection) {
dispatch({
type: 'DECREMENT',
items: items,
focusedItem: focusedItem,
selectedItem: selectedItem,
onFocus: onFocus
});
onDecrement();
} else {
onIncrement();
}
e.preventDefault();
} else if (e.keyCode === containerUtilities.KEY_CODES.HOME) {
if (!isFocusedItemControlled) {
dispatch({
type: 'INCREMENT',
items: items,
focusedItem: focusedItem,
selectedItem: selectedItem,
onFocus: onFocus
type: 'HOME',
payload: items[0]
});
}
onFocus && onFocus(items[0]);
e.preventDefault();
} else if (e.keyCode === containerUtilities.KEY_CODES.HOME) {
dispatch({
type: 'HOME',
items: items,
onFocus: onFocus
});
e.preventDefault();
} else if (e.keyCode === containerUtilities.KEY_CODES.END) {
dispatch({
type: 'END',
items: items,
onFocus: onFocus
});
if (!isFocusedItemControlled) {
dispatch({
type: 'END',
payload: items[items.length - 1]
});
}
onFocus && onFocus(items[items.length - 1]);
e.preventDefault();
} else if (e.keyCode === containerUtilities.KEY_CODES.SPACE || e.keyCode === containerUtilities.KEY_CODES.ENTER) {
dispatch({
type: 'KEYBOARD_SELECT',
payload: item,
onSelect: onSelect
});
onSelect && onSelect(item);
if (!isSelectedItemControlled) {
dispatch({
type: 'KEYBOARD_SELECT',
payload: item
});
}
e.preventDefault();

@@ -344,14 +292,18 @@ }

options = _objectWithoutPropertiesLoose(_ref, ["children", "render"]);
return render(useSelection(options));
return React__default['default'].createElement(React__default['default'].Fragment, null, render(useSelection(options)));
};
SelectionContainer.defaultProps = {
direction: 'horizontal',
defaultFocusedIndex: 0
};
SelectionContainer.propTypes = {
children: PropTypes.func,
render: PropTypes.func,
rtl: PropTypes.bool,
direction: PropTypes.oneOf(['horizontal', 'vertical', 'both']),
defaultFocusedIndex: PropTypes.number,
focusedItem: PropTypes.any,
selectedItem: PropTypes.any,
onSelect: PropTypes.func,
onFocus: PropTypes.func
children: PropTypes__default['default'].func,
render: PropTypes__default['default'].func,
rtl: PropTypes__default['default'].bool,
direction: PropTypes__default['default'].oneOf(['horizontal', 'vertical', 'both']),
defaultFocusedIndex: PropTypes__default['default'].number,
focusedItem: PropTypes__default['default'].any,
selectedItem: PropTypes__default['default'].any,
onSelect: PropTypes__default['default'].func,
onFocus: PropTypes__default['default'].func
};

@@ -358,0 +310,0 @@

@@ -8,3 +8,3 @@ /**

import { useReducer, useEffect } from 'react';
import React, { useReducer, useEffect } from 'react';
import { getControlledValue, composeEventHandlers, KEY_CODES } from '@zendeskgarden/container-utilities';

@@ -43,92 +43,21 @@ import PropTypes from 'prop-types';

switch (action.type) {
case 'END':
case 'HOME':
case 'FOCUS':
{
if (action.onFocus) {
if (action.payload !== action.focusedItem) {
action.onFocus(action.payload);
}
return state;
}
return _extends({}, state, {
focusedItem: action.payload
});
}
case 'INCREMENT':
{
var controlledFocusedItem = getControlledValue(action.focusedItem, state.focusedItem);
var controlledSelectedItem = getControlledValue(action.selectedItem, state.selectedItem);
var currentItemIndex = controlledFocusedItem === undefined ? action.items.indexOf(controlledSelectedItem) : action.items.indexOf(controlledFocusedItem);
var newFocusedItem = action.items[(currentItemIndex + 1) % action.items.length];
if (action.onFocus) {
action.onFocus(newFocusedItem);
return state;
}
return _extends({}, state, {
focusedItem: newFocusedItem
});
}
case 'DECREMENT':
{
var _controlledFocusedItem = getControlledValue(action.focusedItem, state.focusedItem);
var _controlledSelectedItem = getControlledValue(action.selectedItem, state.selectedItem);
var _currentItemIndex = _controlledFocusedItem === undefined ? action.items.indexOf(_controlledSelectedItem) : action.items.indexOf(_controlledFocusedItem);
var _newFocusedItem = action.items[(_currentItemIndex + action.items.length - 1) % action.items.length];
if (action.onFocus) {
action.onFocus(_newFocusedItem);
return state;
}
return _extends({}, state, {
focusedItem: _newFocusedItem
focusedItem: action.payload
});
}
case 'HOME':
case 'MOUSE_SELECT':
{
if (action.onFocus) {
action.onFocus(action.items[0]);
return state;
}
return _extends({}, state, {
focusedItem: action.items[0]
selectedItem: action.payload,
focusedItem: undefined
});
}
case 'END':
{
if (action.onFocus) {
action.onFocus(action.items[action.items.length - 1]);
return state;
}
return _extends({}, state, {
focusedItem: action.items[action.items.length - 1]
});
}
case 'MOUSE_SELECT':
{
var isSelectControlled = false;
var isFocusControlled = false;
if (action.onSelect) {
action.onSelect(action.payload);
isSelectControlled = true;
}
if (action.onFocus) {
action.onFocus(undefined);
isFocusControlled = true;
}
if (isFocusControlled && isSelectControlled) {
return state;
}
var updatedState = _extends({}, state);
if (!isSelectControlled) {
updatedState.selectedItem = action.payload;
}
if (!isFocusControlled) {
updatedState.focusedItem = undefined;
}
return updatedState;
}
case 'KEYBOARD_SELECT':
{
if (action.onSelect) {
action.onSelect(action.payload);
return state;
}
return _extends({}, state, {

@@ -140,6 +69,2 @@ selectedItem: action.payload

{
if (action.onFocus) {
action.onFocus(undefined);
return state;
}
return _extends({}, state, {

@@ -165,2 +90,4 @@ focusedItem: undefined

onFocus = _ref.onFocus;
var isSelectedItemControlled = selectedItem !== undefined;
var isFocusedItemControlled = focusedItem !== undefined;
var refs = [];

@@ -184,7 +111,9 @@ var items = [];

if (selectedItem === undefined && defaultSelectedIndex !== undefined) {
dispatch({
type: 'KEYBOARD_SELECT',
payload: items[defaultSelectedIndex],
onSelect: onSelect
});
onSelect && onSelect(items[defaultSelectedIndex]);
if (!isSelectedItemControlled) {
dispatch({
type: 'KEYBOARD_SELECT',
payload: items[defaultSelectedIndex]
});
}
}

@@ -200,3 +129,3 @@ }, []);

'data-garden-container-id': 'containers.selection',
'data-garden-container-version': '1.3.6'
'data-garden-container-version': '1.3.7'
}, other);

@@ -239,40 +168,62 @@ };

}, _extends2[selectedAriaKey] = selectedAriaKey ? isSelected : undefined, _extends2[refKey] = focusRef, _extends2.onFocus = composeEventHandlers(onFocusCallback, function () {
dispatch({
type: 'FOCUS',
payload: item,
focusedItem: focusedItem,
onFocus: onFocus
});
onFocus && onFocus(item);
if (!isFocusedItemControlled) {
dispatch({
type: 'FOCUS',
payload: item
});
}
}), _extends2.onBlur = function onBlur(e) {
if (e.target.tabIndex === 0) {
if (!isFocusedItemControlled) {
dispatch({
type: 'EXIT_WIDGET'
});
}
onFocus && onFocus();
}
}, _extends2.onClick = composeEventHandlers(onClick, function () {
onSelect && onSelect(item);
onFocus && onFocus();
if (!isSelectedItemControlled) {
dispatch({
type: 'EXIT_WIDGET',
onFocus: onFocus
type: 'MOUSE_SELECT',
payload: item
});
}
}, _extends2.onClick = composeEventHandlers(onClick, function () {
dispatch({
type: 'MOUSE_SELECT',
payload: item,
onSelect: onSelect,
onFocus: onFocus
});
}), _extends2.onKeyDown = composeEventHandlers(onKeyDown, function (e) {
var nextIndex;
var currentIndex;
if (isFocusedItemControlled) {
currentIndex = items.indexOf(focusedItem);
} else {
currentIndex = items.indexOf(state.focusedItem || state.selectedItem);
}
var onIncrement = function onIncrement() {
nextIndex = currentIndex + 1;
if (currentIndex === items.length - 1) {
nextIndex = 0;
}
!isFocusedItemControlled && dispatch({
type: 'INCREMENT',
payload: items[nextIndex]
});
onFocus && onFocus(items[nextIndex]);
};
var onDecrement = function onDecrement() {
nextIndex = currentIndex - 1;
if (currentIndex === 0) {
nextIndex = items.length - 1;
}
!isFocusedItemControlled && dispatch({
type: 'DECREMENT',
payload: items[nextIndex]
});
onFocus && onFocus(items[nextIndex]);
};
if (e.keyCode === KEY_CODES.UP && verticalDirection || e.keyCode === KEY_CODES.LEFT && horizontalDirection) {
if (rtl && !verticalDirection) {
dispatch({
type: 'INCREMENT',
items: items,
focusedItem: focusedItem,
selectedItem: selectedItem,
onFocus: onFocus
});
onIncrement();
} else {
dispatch({
type: 'DECREMENT',
items: items,
focusedItem: focusedItem,
selectedItem: selectedItem,
onFocus: onFocus
});
onDecrement();
}

@@ -282,39 +233,33 @@ e.preventDefault();

if (rtl && !verticalDirection) {
dispatch({
type: 'DECREMENT',
items: items,
focusedItem: focusedItem,
selectedItem: selectedItem,
onFocus: onFocus
});
onDecrement();
} else {
onIncrement();
}
e.preventDefault();
} else if (e.keyCode === KEY_CODES.HOME) {
if (!isFocusedItemControlled) {
dispatch({
type: 'INCREMENT',
items: items,
focusedItem: focusedItem,
selectedItem: selectedItem,
onFocus: onFocus
type: 'HOME',
payload: items[0]
});
}
onFocus && onFocus(items[0]);
e.preventDefault();
} else if (e.keyCode === KEY_CODES.HOME) {
dispatch({
type: 'HOME',
items: items,
onFocus: onFocus
});
e.preventDefault();
} else if (e.keyCode === KEY_CODES.END) {
dispatch({
type: 'END',
items: items,
onFocus: onFocus
});
if (!isFocusedItemControlled) {
dispatch({
type: 'END',
payload: items[items.length - 1]
});
}
onFocus && onFocus(items[items.length - 1]);
e.preventDefault();
} else if (e.keyCode === KEY_CODES.SPACE || e.keyCode === KEY_CODES.ENTER) {
dispatch({
type: 'KEYBOARD_SELECT',
payload: item,
onSelect: onSelect
});
onSelect && onSelect(item);
if (!isSelectedItemControlled) {
dispatch({
type: 'KEYBOARD_SELECT',
payload: item
});
}
e.preventDefault();

@@ -337,4 +282,8 @@ }

options = _objectWithoutPropertiesLoose(_ref, ["children", "render"]);
return render(useSelection(options));
return React.createElement(React.Fragment, null, render(useSelection(options)));
};
SelectionContainer.defaultProps = {
direction: 'horizontal',
defaultFocusedIndex: 0
};
SelectionContainer.propTypes = {

@@ -341,0 +290,0 @@ children: PropTypes.func,

@@ -10,5 +10,7 @@ /**

export interface ISelectionContainerProps<Item> extends IUseSelectionProps<Item> {
/** A render prop function which receives selection state */
render?: (options: UseSelectionReturnValue<Item>) => React.ReactNode;
/** A children render prop function which receives selection state */
children?: (options: UseSelectionReturnValue<Item>) => React.ReactNode;
}
export declare const SelectionContainer: React.FunctionComponent<ISelectionContainerProps<any>>;

@@ -24,50 +24,42 @@ /**

export interface IUseSelectionProps<Item> {
/** Determines the orientation of the selection */
direction?: 'horizontal' | 'vertical' | 'both';
/** Sets the initial focused item */
defaultFocusedIndex?: number;
/** Sets the initial selected item */
defaultSelectedIndex?: number;
/** Determines if selection uses right-to-left writing direction */
rtl?: boolean;
/** Sets the selected item in a controlled selection */
selectedItem?: Item;
/** Sets the focused item in a controlled selection */
focusedItem?: Item;
/** A callback function that receives the selected item */
onSelect?: (selectedItem: Item) => void;
/** A callback function that receives the focused item */
onFocus?: (focusedItem?: Item) => void;
}
declare type onFocusCallback<Item> = (item?: Item) => void;
declare type onSelectCallback<Item> = (item?: Item) => void;
export declare type SELECTION_ACTION<Item> = {
export declare type SELECTION_ACTION = {
type: 'FOCUS';
onFocus?: onFocusCallback<Item>;
payload?: any;
focusedItem?: any;
} | {
type: 'INCREMENT';
focusedItem?: any;
selectedItem?: any;
items: any[];
onFocus?: onFocusCallback<Item>;
payload: any;
} | {
type: 'DECREMENT';
focusedItem?: any;
selectedItem?: any;
items: any[];
onFocus?: onFocusCallback<Item>;
payload: any;
} | {
type: 'HOME';
onFocus?: onFocusCallback<Item>;
items: any[];
payload: any;
} | {
type: 'END';
onFocus?: onFocusCallback<Item>;
items: any[];
payload: any;
} | {
type: 'MOUSE_SELECT';
onSelect?: onSelectCallback<Item>;
onFocus?: onFocusCallback<Item>;
payload: any;
} | {
type: 'KEYBOARD_SELECT';
onSelect?: onSelectCallback<Item>;
payload: any;
} | {
type: 'EXIT_WIDGET';
onFocus?: onFocusCallback<Item>;
};

@@ -74,0 +66,0 @@ /**

@@ -11,3 +11,3 @@ {

},
"version": "1.3.6",
"version": "1.3.7",
"main": "dist/index.cjs.js",

@@ -45,3 +45,3 @@ "module": "dist/index.esm.js",

"zendeskgarden:src": "src/index.ts",
"gitHead": "71e6fd562af566ecf62fb007994ddd7064b101be"
"gitHead": "bd572f26cb297240b4bcd32eb8dd110efca85763"
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc