cpr-select
Advanced tools
Comparing version 2.1.1 to 2.2.0
@@ -6,4 +6,4 @@ /*! | ||
* license: MIT | ||
* version: 2.1.0 | ||
* version: 2.1.1 | ||
*/ | ||
!function(e){function t(i){if(s[i])return s[i].exports;var n=s[i]={exports:{},id:i,loaded:!1};return e[i].call(n.exports,n,n.exports,t),n.loaded=!0,n.exports}var s={};return t.m=e,t.c=s,t.p="",t(0)}([function(e,t,s){"use strict";function i(e,t){for(var s,i=!0;i;){i=!1;var n=e,o=t;if(!n)return!1;if(s=n===o)return s;e=n.parentElement,t=o,i=!0}}var n=function(e){return e&&e.__esModule?e["default"]:e},o=n(s(1)),l=s(2).findIndex,a="",r=void 0,c=o.createClass({displayName:"CanopySelect",componentWillMount:function(){document.body.addEventListener("click",this.state.close)},componentWillUnmount:function(){document.body.removeEventListener("click",this.state.close)},getInitialState:function(){var e=this;return{dialogDisplayed:!1,top:0,focused:!1,close:function(t){i(t.target,e.el)||e.setState({dialogDisplayed:!1,focused:!1})}}},displayDialog:function(e){this.props.disabled||this.setState({dialogDisplayed:!0})},getIndex:function(e){return l(this.props.options,{key:e})},onKeyDown:function(e){if(!this.props.disabled){var t=e.which,s=this.state.selectedIndex;9!==t&&e.preventDefault(),13===t?this.selectItem(s):38===t?0>=s?this.setState({dialogDisplayed:!0}):this.setState({dialogDisplayed:!0,selectedIndex:void 0===s?0:s-1}):40===t?s===this.props.options.length-1?this.setState({dialogDisplayed:!0}):this.setState({dialogDisplayed:!0,selectedIndex:s?s+1:0}):27===t?this.setState({dialogDisplayed:!1}):this.highlightByText(e.which)}},triggerItemChange:function(){this.props.onChange&&this.props.onChange.call(null,this.props.options[this.state.selectedIndex].key,this.props.options[this.state.selectedIndex],this.state.selectedIndex)},selectItem:function(e,t){var s=this;this.props.disabled||setTimeout(function(){s.setState({selectedIndex:e,focused:!0,dialogDisplayed:!1}),setTimeout(s.triggerItemChange)})},positionDialogAndGetTop:function(e,t){var s=e.length-t;return t>5&&6>s?e.length<11?-2+(36*t*-1-10)+"px":(this.positionDialog(t),-215-36*(5-s)+"px"):t>5?(this.positionDialog(t),"-203px"):-1+(36*t*-1-10)+"px"},positionDialog:function(e){var t=this;setTimeout(function(){var s=t.el.querySelector(".cp-select__menu");s&&(s.scrollTop=36*e-192)})},focusSelect:function(){this.state.focused||this.setState({focused:!0})},onBlur:function(){this.setState({focused:!1})},highlightByText:function(e){a+=String.fromCharCode(e);var t=this.getIndexFromString(a);t>-1&&this.selectItem(t),clearTimeout(r),r=setTimeout(function(){a=""},1e3)},getIndexFromString:function(e){var t=this;return e=e.toLowerCase(),l(this.props.options,function(s){return null!==t.getViewValue(s)?0===t.getViewValue(s).toLowerCase().indexOf(e):!1})},getViewValue:function(e){return null===e.value||void 0===e.value?null:e.value||e},getDialog:function(e,t){var s=this;if(e){var i=function(){var e=s.state.selectedIndex,i=t.map(function(t,i){return t.separator?o.createElement("li",{key:"separator"+i,className:"separator"}):o.createElement("li",{key:t.key,className:e===i?"+selected":"",onMouseDown:s.selectItem.bind(s,i)},o.createElement("a",{style:null!==t.value?{}:{color:"rgba(0,0,0,0)"}},null!==t.value?t.value:"null"))});return setTimeout(function(){try{s.el.querySelector(".cp-select__hidden-input").focus()}catch(e){if(-1===e.message.indexOf("Invariant Violation"))throw new Error(e.message)}},100),{v:o.createElement("div",null,o.createElement("ul",{className:"cp-select__menu cps-dropdown-menu",style:{top:s.positionDialogAndGetTop(t,e)}},i))}}();if("object"==typeof i)return i.v}},render:function(){var e="cp-select",t=this.props.options[this.getIndex(this.props.selected)],s=this;return this.props.disabled&&(e+=" +disabled"),this.state.focused&&!this.props.disabled&&(e+=" +focus"),o.createElement("div",{ref:function(e){e&&(s.el=e)},className:"cp-select-outer",role:"select"},o.createElement("input",{className:"cp-select__hidden-input",onFocus:this.focusSelect,onBlur:this.onBlur,onKeyDown:this.onKeyDown}),o.createElement("div",{className:e,onClick:this.displayDialog},o.createElement("div",{className:"cp-select__selected"},t?t.value:this.props.placeholder),o.createElement("div",{className:"cp-select__icon"})),this.getDialog(this.state.dialogDisplayed,this.props.options))}});"undefined"!=typeof window&&window&&!window.CanopySelect&&(window.CanopySelect=c),e.exports=c},function(e,t,s){e.exports=React},function(e,t,s){e.exports=_}]); | ||
!function(e){function t(s){if(i[s])return i[s].exports;var o=i[s]={exports:{},id:s,loaded:!1};return e[s].call(o.exports,o,o.exports,t),o.loaded=!0,o.exports}var i={};return t.m=e,t.c=i,t.p="",t(0)}([function(e,t,i){"use strict";function s(e,t){for(var i,s=!0;s;){s=!1;var o=e,n=t;if(!o)return!1;if(i=o===n)return i;e=o.parentElement,t=n,s=!0}}var o=function(e){return e&&e.__esModule?e["default"]:e},n=o(i(1)),l=i(2).findIndex,a="",r=void 0,c=n.createClass({displayName:"CanopySelect",componentWillMount:function(){document.body.addEventListener("click",this.state.close)},componentWillUnmount:function(){document.body.removeEventListener("click",this.state.close)},getInitialState:function(){var e=this;return{dialogDisplayed:!1,top:0,focused:!1,close:function(t){s(t.target,e.el)||e.setState({dialogDisplayed:!1,focused:!1})}}},displayDialog:function(e){this.props.disabled||this.setState({dialogDisplayed:!0})},getIndex:function(e){return l(this.props.options,{key:e})},onKeyDown:function(e){if(!this.props.disabled){var t=e.which,i=this.state.selectedIndex;9!==t&&e.preventDefault(),13===t?this.selectItem(i):38===t?0>=i?this.setState({dialogDisplayed:!0}):this.setState({dialogDisplayed:!0,selectedIndex:void 0===i?0:i-1}):40===t?i===this.props.options.length-1?this.setState({dialogDisplayed:!0}):this.setState({dialogDisplayed:!0,selectedIndex:i?i+1:0}):27===t?this.setState({dialogDisplayed:!1}):this.highlightByText(e.which)}},triggerItemChange:function(){this.props.onChange&&this.props.onChange.call(null,this.props.options[this.state.selectedIndex].key,this.props.options[this.state.selectedIndex],this.state.selectedIndex)},selectItem:function(e,t){var i=this;this.props.disabled||setTimeout(function(){i.setState({selectedIndex:e,focused:!0,dialogDisplayed:!1}),setTimeout(i.triggerItemChange)})},positionDialogAndGetTop:function(e,t,i){var s=e.length-t,o=Math.floor(i/36),n=Math.floor((o-1)/2);if(t>n&&n+1>s){if(e.length<o)return-2+(36*t*-1-10)+"px";this.positionDialog(t,i);var l=i/-2-15;return l-36*(5-s)+"px"}return t>n?(this.positionDialog(t,i),i/-2-3):-1+(36*t*-1-10)+"px"},positionDialog:function(e,t){var i=this;setTimeout(function(){var s=i.el.querySelector(".cp-select__menu");if(s){var o=t/2-8;s.scrollTop=36*e-o}})},focusSelect:function(){this.state.focused||this.setState({focused:!0})},onBlur:function(){this.setState({focused:!1})},highlightByText:function(e){a+=String.fromCharCode(e);var t=this.getIndexFromString(a);t>-1&&this.selectItem(t),clearTimeout(r),r=setTimeout(function(){a=""},1e3)},getIndexFromString:function(e){var t=this;return e=e.toLowerCase(),l(this.props.options,function(i){return null!==t.getViewValue(i)?0===t.getViewValue(i).toLowerCase().indexOf(e):!1})},getViewValue:function(e){return null===e.value||void 0===e.value?null:e.value||e},getDialog:function(e,t){var i=this;if(e){var s=function(){var e=i.state.selectedIndex,s=t.map(function(t,s){return t.separator?n.createElement("li",{key:"separator"+s,className:"separator"}):n.createElement("li",{key:t.key,className:e===s?"+selected":"",onMouseDown:i.selectItem.bind(i,s)},n.createElement("a",{style:null!==t.value?{}:{color:"rgba(0,0,0,0)"}},null!==t.value?t.value:"null"))});setTimeout(function(){try{i.el.querySelector(".cp-select__hidden-input").focus()}catch(e){if(-1===e.message.indexOf("Invariant Violation"))throw new Error(e.message)}},100);var o=i.props.maxHeight||400;return{v:n.createElement("div",null,n.createElement("ul",{className:"cp-select__menu cps-dropdown-menu",style:{top:i.positionDialogAndGetTop(t,e,o),maxHeight:o+"px"}},s))}}();if("object"==typeof s)return s.v}},render:function(){var e="cp-select",t=this.props.options[this.getIndex(this.props.selected)],i=this;return this.props.disabled&&(e+=" +disabled"),this.state.focused&&!this.props.disabled&&(e+=" +focus"),n.createElement("div",{ref:function(e){e&&(i.el=e)},className:"cp-select-outer",role:"select"},n.createElement("input",{className:"cp-select__hidden-input",onFocus:this.focusSelect,onBlur:this.onBlur,onKeyDown:this.onKeyDown}),n.createElement("div",{className:e,onClick:this.displayDialog},n.createElement("div",{className:"cp-select__selected"},t?t.value:this.props.placeholder),n.createElement("div",{className:"cp-select__icon"})),this.getDialog(this.state.dialogDisplayed,this.props.options))}});"undefined"!=typeof window&&window&&!window.CanopySelect&&(window.CanopySelect=c),e.exports=c},function(e,t,i){e.exports=React},function(e,t,i){e.exports=_}]); |
@@ -158,8 +158,11 @@ 'use strict'; | ||
positionDialogAndGetTop: function positionDialogAndGetTop(options, index) { | ||
positionDialogAndGetTop: function positionDialogAndGetTop(options, index, maxHeight) { | ||
var distanceFromEnd = options.length - index; | ||
var numVisibleOptions = Math.floor(maxHeight / 36); | ||
var numSurroundingOptions = Math.floor((numVisibleOptions - 1) / 2); | ||
var unusedPixels = maxHeight % 36; | ||
if (index > 5 && distanceFromEnd < 6) { | ||
if (index > numSurroundingOptions && distanceFromEnd < numSurroundingOptions + 1) { | ||
// Bottom 5 | ||
if (options.length < 11) { | ||
if (options.length < numVisibleOptions) { | ||
// Dialog doesn't have a scroll | ||
@@ -169,9 +172,10 @@ return -2 + (36 * index * -1 - 10) + "px"; | ||
// Dialog has a scroll | ||
this.positionDialog(index); | ||
return -215 - (5 - distanceFromEnd) * 36 + "px"; | ||
this.positionDialog(index, maxHeight); | ||
var start = maxHeight / -2 - 15; | ||
return start - (numSurroundingOptions - distanceFromEnd) * 36 + "px"; | ||
} | ||
} else if (index > 5) { | ||
} else if (index > numSurroundingOptions) { | ||
// Middle | ||
this.positionDialog(index); | ||
return '-203px'; | ||
this.positionDialog(index, maxHeight); | ||
return maxHeight / -2 - 0.0075 * maxHeight; | ||
} else { | ||
@@ -183,3 +187,3 @@ // Top 5 | ||
positionDialog: function positionDialog(index) { | ||
positionDialog: function positionDialog(index, maxHeight) { | ||
var _this3 = this; | ||
@@ -190,3 +194,4 @@ | ||
if (menuDialog) { | ||
menuDialog.scrollTop = 36 * index - 192; | ||
var dialogHeightImpact = maxHeight / 2 - 8; | ||
menuDialog.scrollTop = 36 * index - dialogHeightImpact; | ||
} | ||
@@ -274,2 +279,3 @@ }); | ||
var maxHeight = _this5.props.maxHeight || 400; | ||
return { | ||
@@ -281,3 +287,3 @@ v: _react2['default'].createElement( | ||
'ul', | ||
{ className: 'cp-select__menu cps-dropdown-menu', style: { top: _this5.positionDialogAndGetTop(options, selectedIndex) } }, | ||
{ className: 'cp-select__menu cps-dropdown-menu', style: { top: _this5.positionDialogAndGetTop(options, selectedIndex, maxHeight), maxHeight: maxHeight + 'px' } }, | ||
optionElements | ||
@@ -284,0 +290,0 @@ ) |
{ | ||
"name": "cpr-select", | ||
"version": "2.1.1", | ||
"version": "2.2.0", | ||
"description": "A consistently styled cross-browser and keyboard friendly select component", | ||
@@ -5,0 +5,0 @@ "main": "lib/select.js", |
@@ -125,30 +125,35 @@ import React from 'react'; | ||
positionDialogAndGetTop: function(options, index) { | ||
let distanceFromEnd = options.length - index; | ||
positionDialogAndGetTop: function(options, index, maxHeight) { | ||
const distanceFromEnd = options.length - index; | ||
const numVisibleOptions = Math.floor(maxHeight / 36); | ||
const numSurroundingOptions = Math.floor((numVisibleOptions - 1) / 2); | ||
const unusedPixels = maxHeight % 36; | ||
if (index > 5 && distanceFromEnd < 6) { | ||
if (index > numSurroundingOptions && distanceFromEnd < numSurroundingOptions + 1) { | ||
// Bottom 5 | ||
if(options.length < 11) { | ||
if (options.length < numVisibleOptions) { | ||
// Dialog doesn't have a scroll | ||
return ( -2 + (36 * index * -1 - 10) + "px" ); | ||
return -2 + (36 * index * -1 - 10) + "px"; | ||
} else { | ||
// Dialog has a scroll | ||
this.positionDialog(index); | ||
return ( -215 - (5 - distanceFromEnd) * 36 + "px" ); | ||
this.positionDialog(index, maxHeight); | ||
const start = (maxHeight / -2) - 15; | ||
return start - (numSurroundingOptions - distanceFromEnd) * 36 + "px"; | ||
} | ||
} else if (index > 5) { | ||
} else if (index > numSurroundingOptions) { | ||
// Middle | ||
this.positionDialog(index); | ||
return '-203px'; | ||
this.positionDialog(index, maxHeight); | ||
return (maxHeight / -2) - 0.0075 * maxHeight; | ||
} else { | ||
// Top 5 | ||
return ( -1 + (36 * index * -1 - 10) + "px" ); | ||
return -1 + (36 * index * -1 - 10) + "px"; | ||
} | ||
}, | ||
positionDialog: function(index) { | ||
positionDialog: function(index, maxHeight) { | ||
setTimeout(() => { | ||
let menuDialog = this.el.querySelector(".cp-select__menu"); | ||
if (menuDialog) { | ||
menuDialog.scrollTop = (36 * index - 192); | ||
const dialogHeightImpact = maxHeight / 2 - 8; | ||
menuDialog.scrollTop = (36 * index - dialogHeightImpact); | ||
} | ||
@@ -223,5 +228,6 @@ }); | ||
const maxHeight = this.props.maxHeight || 400; | ||
return ( | ||
<div> | ||
<ul className="cp-select__menu cps-dropdown-menu" style={{top: this.positionDialogAndGetTop(options, selectedIndex)}}> | ||
<ul className="cp-select__menu cps-dropdown-menu" style={{top: this.positionDialogAndGetTop(options, selectedIndex, maxHeight), maxHeight: maxHeight + 'px'}}> | ||
{optionElements} | ||
@@ -228,0 +234,0 @@ </ul> |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
35756
871