downshift 8.2.3 → 8.2.4-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1379,7 +1379,7 @@
1379
1379
 
1380
1380
  var reactIsExports = reactIs.exports;
1381
1381
 
1382
- const t=t=>"object"==typeof t&&null!=t&&1===t.nodeType,e=(t,e)=>(!e||"hidden"!==t)&&("visible"!==t&&"clip"!==t),n=(t,n)=>{if(t.clientHeight<t.scrollHeight||t.clientWidth<t.scrollWidth){const o=getComputedStyle(t,null);return e(o.overflowY,n)||e(o.overflowX,n)||(t=>{const e=(t=>{if(!t.ownerDocument||!t.ownerDocument.defaultView)return null;try{return t.ownerDocument.defaultView.frameElement}catch(t){return null}})(t);return !!e&&(e.clientHeight<t.scrollHeight||e.clientWidth<t.scrollWidth)})(t)}return !1},o=(t,e,n,o,l,r,i,s)=>r<t&&i>e||r>t&&i<e?0:r<=t&&s<=n||i>=e&&s>=n?r-t-o:i>e&&s<n||r<t&&s>n?i-e+l:0,l=t=>{const e=t.parentElement;return null==e?t.getRootNode().host||null:e},r=(e,r)=>{var i,s,d,h;if("undefined"==typeof document)return [];const{scrollMode:c,block:f,inline:u,boundary:a,skipOverflowHiddenElements:g}=r,p="function"==typeof a?a:t=>t!==a;if(!t(e))throw new TypeError("Invalid target");const m=document.scrollingElement||document.documentElement,w=[];let W=e;for(;t(W)&&p(W);){if(W=l(W),W===m){w.push(W);break}null!=W&&W===document.body&&n(W)&&!n(document.documentElement)||null!=W&&n(W,g)&&w.push(W);}const b=null!=(s=null==(i=window.visualViewport)?void 0:i.width)?s:innerWidth,H=null!=(h=null==(d=window.visualViewport)?void 0:d.height)?h:innerHeight,{scrollX:y,scrollY:M}=window,{height:v,width:E,top:x,right:C,bottom:I,left:R}=e.getBoundingClientRect(),{top:T,right:B,bottom:F,left:V}=(t=>{const e=window.getComputedStyle(t);return {top:parseFloat(e.scrollMarginTop)||0,right:parseFloat(e.scrollMarginRight)||0,bottom:parseFloat(e.scrollMarginBottom)||0,left:parseFloat(e.scrollMarginLeft)||0}})(e);let k="start"===f||"nearest"===f?x-T:"end"===f?I+F:x+v/2-T+F,D="center"===u?R+E/2-V+B:"end"===u?C+B:R-V;const L=[];for(let t=0;t<w.length;t++){const e=w[t],{height:n,width:l,top:r,right:i,bottom:s,left:d}=e.getBoundingClientRect();if("if-needed"===c&&x>=0&&R>=0&&I<=H&&C<=b&&x>=r&&I<=s&&R>=d&&C<=i)return L;const h=getComputedStyle(e),a=parseInt(h.borderLeftWidth,10),g=parseInt(h.borderTopWidth,10),p=parseInt(h.borderRightWidth,10),W=parseInt(h.borderBottomWidth,10);let T=0,B=0;const F="offsetWidth"in e?e.offsetWidth-e.clientWidth-a-p:0,V="offsetHeight"in e?e.offsetHeight-e.clientHeight-g-W:0,S="offsetWidth"in e?0===e.offsetWidth?0:l/e.offsetWidth:0,X="offsetHeight"in e?0===e.offsetHeight?0:n/e.offsetHeight:0;if(m===e)T="start"===f?k:"end"===f?k-H:"nearest"===f?o(M,M+H,H,g,W,M+k,M+k+v,v):k-H/2,B="start"===u?D:"center"===u?D-b/2:"end"===u?D-b:o(y,y+b,b,a,p,y+D,y+D+E,E),T=Math.max(0,T+M),B=Math.max(0,B+y);else {T="start"===f?k-r-g:"end"===f?k-s+W+V:"nearest"===f?o(r,s,n,g,W+V,k,k+v,v):k-(r+n/2)+V/2,B="start"===u?D-d-a:"center"===u?D-(d+l/2)+F/2:"end"===u?D-i+p+F:o(d,i,l,a,p+F,D,D+E,E);const{scrollLeft:t,scrollTop:h}=e;T=0===X?0:Math.max(0,Math.min(h+T/X,e.scrollHeight-n/X+V)),B=0===S?0:Math.max(0,Math.min(t+B/S,e.scrollWidth-l/S+F)),k+=h-T,D+=t-B;}L.push({el:e,top:T,left:B});}return L};
1382
+ const t=t=>"object"==typeof t&&null!=t&&1===t.nodeType,e=(t,e)=>(!e||"hidden"!==t)&&("visible"!==t&&"clip"!==t),n=(t,n)=>{if(t.clientHeight<t.scrollHeight||t.clientWidth<t.scrollWidth){const o=getComputedStyle(t,null);return e(o.overflowY,n)||e(o.overflowX,n)||(t=>{const e=(t=>{if(!t.ownerDocument||!t.ownerDocument.defaultView)return null;try{return t.ownerDocument.defaultView.frameElement}catch(t){return null}})(t);return !!e&&(e.clientHeight<t.scrollHeight||e.clientWidth<t.scrollWidth)})(t)}return !1},o=(t,e,n,o,i,l,r,d)=>l<t&&r>e||l>t&&r<e?0:l<=t&&d<=n||r>=e&&d>=n?l-t-o:r>e&&d<n||l<t&&d>n?r-e+i:0,i=t=>{const e=t.parentElement;return null==e?t.getRootNode().host||null:e},l=(e,l)=>{var r,d,s,h;if("undefined"==typeof document)return [];const{scrollMode:c,block:f,inline:u,boundary:a,skipOverflowHiddenElements:g}=l,m="function"==typeof a?a:t=>t!==a;if(!t(e))throw new TypeError("Invalid target");const p=document.scrollingElement||document.documentElement,w=[];let W=e;for(;t(W)&&m(W);){if(W=i(W),W===p){w.push(W);break}null!=W&&W===document.body&&n(W)&&!n(document.documentElement)||null!=W&&n(W,g)&&w.push(W);}const H=null!=(d=null==(r=window.visualViewport)?void 0:r.width)?d:innerWidth,b=null!=(h=null==(s=window.visualViewport)?void 0:s.height)?h:innerHeight,{scrollX:v,scrollY:y}=window,{height:E,width:M,top:x,right:I,bottom:C,left:R}=e.getBoundingClientRect();let T="start"===f||"nearest"===f?x:"end"===f?C:x+E/2,V="center"===u?R+M/2:"end"===u?I:R;const k=[];for(let t=0;t<w.length;t++){const e=w[t],{height:n,width:i,top:l,right:r,bottom:d,left:s}=e.getBoundingClientRect();if("if-needed"===c&&x>=0&&R>=0&&C<=b&&I<=H&&x>=l&&C<=d&&R>=s&&I<=r)return k;const h=getComputedStyle(e),a=parseInt(h.borderLeftWidth,10),g=parseInt(h.borderTopWidth,10),m=parseInt(h.borderRightWidth,10),W=parseInt(h.borderBottomWidth,10);let B=0,D=0;const L="offsetWidth"in e?e.offsetWidth-e.clientWidth-a-m:0,S="offsetHeight"in e?e.offsetHeight-e.clientHeight-g-W:0,X="offsetWidth"in e?0===e.offsetWidth?0:i/e.offsetWidth:0,Y="offsetHeight"in e?0===e.offsetHeight?0:n/e.offsetHeight:0;if(p===e)B="start"===f?T:"end"===f?T-b:"nearest"===f?o(y,y+b,b,g,W,y+T,y+T+E,E):T-b/2,D="start"===u?V:"center"===u?V-H/2:"end"===u?V-H:o(v,v+H,H,a,m,v+V,v+V+M,M),B=Math.max(0,B+y),D=Math.max(0,D+v);else {B="start"===f?T-l-g:"end"===f?T-d+W+S:"nearest"===f?o(l,d,n,g,W+S,T,T+E,E):T-(l+n/2)+S/2,D="start"===u?V-s-a:"center"===u?V-(s+i/2)+L/2:"end"===u?V-r+m+L:o(s,r,i,a,m+L,V,V+M,M);const{scrollLeft:t,scrollTop:h}=e;B=Math.max(0,Math.min(h+B/Y,e.scrollHeight-n/Y+S)),D=Math.max(0,Math.min(t+D/X,e.scrollWidth-i/X+L)),T+=h-B,V+=t-D;}k.push({el:e,top:B,left:D});}return k};
1383
1383
 
1384
1384
  var idCounter = 0;
1385
1385
 
@@ -1405,7 +1405,7 @@
1405
1405
  if (!node) {
1406
1406
  return;
1407
1407
  }
1408
- var actions = r(node, {
1408
+ var actions = l(node, {
1409
1409
  boundary: menuNode,
1410
1410
  block: 'nearest',
1411
1411
  scrollMode: 'if-needed'
@@ -3050,9 +3050,10 @@
3050
3050
  * @param {Function} reducer Reducer function from downshift.
3051
3051
  * @param {Object} props The hook props, also passed to createInitialState.
3052
3052
  * @param {Function} createInitialState Function that returns the initial state.
3053
+ * @param {Function} isStateEqual Function that checks if a previous state is equal to the next.
3053
3054
  * @returns {Array} An array with the state and an action dispatcher.
3054
3055
  */
3055
- function useEnhancedReducer(reducer, props, createInitialState) {
3056
+ function useEnhancedReducer(reducer, props, createInitialState, isStateEqual) {
3056
3057
  var prevStateRef = React.useRef();
3057
3058
  var actionRef = React.useRef();
3058
3059
  var enhancedReducer = React.useCallback(function (state, action) {
@@ -3075,11 +3076,19 @@
3075
3076
  }, [propsRef]);
3076
3077
  var action = actionRef.current;
3077
3078
  React.useEffect(function () {
3078
- if (action && prevStateRef.current && prevStateRef.current !== state) {
3079
+ console.log('effect');
3080
+ var shouldCallOnChangeProps = action && prevStateRef.current && !isStateEqual(prevStateRef.current, state);
3081
+ console.log('passed', prevStateRef.current, state);
3082
+ if (shouldCallOnChangeProps) {
3079
3083
  callOnChangeProps(action, getState(prevStateRef.current, action.props), state);
3080
3084
  }
3081
3085
  prevStateRef.current = state;
3082
- }, [state, props, action]);
3086
+ }, [state, action, isStateEqual]);
3087
+ React.useEffect(function () {
3088
+ if (props) {
3089
+ prevStateRef.current = getState(prevStateRef.current, props);
3090
+ }
3091
+ }, [props]);
3083
3092
  return [state, dispatchWithProps];
3084
3093
  }
3085
3094
 
@@ -3090,10 +3099,11 @@
3090
3099
  * @param {Function} reducer Reducer function from downshift.
3091
3100
  * @param {Object} props The hook props, also passed to createInitialState.
3092
3101
  * @param {Function} createInitialState Function that returns the initial state.
3102
+ * @param {Function} isStateEqual Function that checks if a previous state is equal to the next.
3093
3103
  * @returns {Array} An array with the state and an action dispatcher.
3094
3104
  */
3095
- function useControlledReducer$1(reducer, props, createInitialState) {
3096
- var _useEnhancedReducer = useEnhancedReducer(reducer, props, createInitialState),
3105
+ function useControlledReducer$1(reducer, props, createInitialState, isStateEqual) {
3106
+ var _useEnhancedReducer = useEnhancedReducer(reducer, props, createInitialState, isStateEqual),
3097
3107
  state = _useEnhancedReducer[0],
3098
3108
  dispatch = _useEnhancedReducer[1];
3099
3109
  return [getState(state, props), dispatch];
@@ -3375,6 +3385,18 @@
3375
3385
  }));
3376
3386
  }
3377
3387
 
3388
+ /**
3389
+ * Check if a state is equal for dropdowns, by comparing isOpen, inputValue, highlightedIndex and selected item.
3390
+ * Used by useSelect and useCombobox.
3391
+ *
3392
+ * @param {Object} prevState
3393
+ * @param {Object} newState
3394
+ * @returns {boolean} Wheather the states are deeply equal.
3395
+ */
3396
+ function isDropdownsStateEqual(prevState, newState) {
3397
+ return prevState.isOpen === newState.isOpen && prevState.inputValue === newState.inputValue && prevState.highlightedIndex === newState.highlightedIndex && prevState.selectedItem === newState.selectedItem;
3398
+ }
3399
+
3378
3400
  // Shared between all exports.
3379
3401
  var commonPropTypes = {
3380
3402
  environment: PropTypes.shape({
@@ -3728,14 +3750,13 @@
3728
3750
  getA11ySelectionMessage = props.getA11ySelectionMessage,
3729
3751
  getA11yStatusMessage = props.getA11yStatusMessage;
3730
3752
  // Initial state depending on controlled props.
3731
- var _useControlledReducer = useControlledReducer$1(downshiftSelectReducer, props, getInitialState$2),
3753
+ var _useControlledReducer = useControlledReducer$1(downshiftSelectReducer, props, getInitialState$2, isDropdownsStateEqual),
3732
3754
  state = _useControlledReducer[0],
3733
3755
  dispatch = _useControlledReducer[1];
3734
3756
  var isOpen = state.isOpen,
3735
3757
  highlightedIndex = state.highlightedIndex,
3736
3758
  selectedItem = state.selectedItem,
3737
3759
  inputValue = state.inputValue;
3738
-
3739
3760
  // Element efs.
3740
3761
  var toggleButtonRef = React.useRef(null);
3741
3762
  var menuRef = React.useRef(null);
@@ -4213,11 +4234,12 @@
4213
4234
  * @param {Function} reducer Reducer function from downshift.
4214
4235
  * @param {Object} props The hook props, also passed to createInitialState.
4215
4236
  * @param {Function} createInitialState Function that returns the initial state.
4237
+ * @param {Function} isStateEqual Function that checks if a previous state is equal to the next.
4216
4238
  * @returns {Array} An array with the state and an action dispatcher.
4217
4239
  */
4218
- function useControlledReducer(reducer, props, createInitialState) {
4240
+ function useControlledReducer(reducer, props, createInitialState, isStateEqual) {
4219
4241
  var previousSelectedItemRef = React.useRef();
4220
- var _useEnhancedReducer = useEnhancedReducer(reducer, props, createInitialState),
4242
+ var _useEnhancedReducer = useEnhancedReducer(reducer, props, createInitialState, isStateEqual),
4221
4243
  state = _useEnhancedReducer[0],
4222
4244
  dispatch = _useEnhancedReducer[1];
4223
4245
 
@@ -4391,7 +4413,7 @@
4391
4413
  getA11ySelectionMessage = props.getA11ySelectionMessage,
4392
4414
  itemToString = props.itemToString;
4393
4415
  // Initial state depending on controlled props.
4394
- var _useControlledReducer = useControlledReducer(downshiftUseComboboxReducer, props, getInitialState$1),
4416
+ var _useControlledReducer = useControlledReducer(downshiftUseComboboxReducer, props, getInitialState$1, isDropdownsStateEqual),
4395
4417
  state = _useControlledReducer[0],
4396
4418
  dispatch = _useControlledReducer[1];
4397
4419
  var isOpen = state.isOpen,
@@ -4876,6 +4898,18 @@
4876
4898
  itemToStringLocal = selectionParameters.itemToString;
4877
4899
  return itemToStringLocal(removedSelectedItem) + " has been removed.";
4878
4900
  }
4901
+
4902
+ /**
4903
+ * Check if a state is equal for taglist, by comparing active index and selected items.
4904
+ * Used by useSelect and useCombobox.
4905
+ *
4906
+ * @param {Object} prevState
4907
+ * @param {Object} newState
4908
+ * @returns {boolean} Wheather the states are deeply equal.
4909
+ */
4910
+ function isStateEqual(prevState, newState) {
4911
+ return prevState.selectedItems === newState.selectedItems && prevState.activeIndex === newState.activeIndex;
4912
+ }
4879
4913
  var propTypes = _extends({}, commonPropTypes, {
4880
4914
  selectedItems: PropTypes.array,
4881
4915
  initialSelectedItems: PropTypes.array,
@@ -5065,7 +5099,7 @@
5065
5099
  keyNavigationPrevious = props.keyNavigationPrevious;
5066
5100
 
5067
5101
  // Reducer init.
5068
- var _useControlledReducer = useControlledReducer$1(downshiftMultipleSelectionReducer, props, getInitialState),
5102
+ var _useControlledReducer = useControlledReducer$1(downshiftMultipleSelectionReducer, props, getInitialState, isStateEqual),
5069
5103
  state = _useControlledReducer[0],
5070
5104
  dispatch = _useControlledReducer[1];
5071
5105
  var activeIndex = state.activeIndex,