import { ClassNamesState , CommonPropsAndClassName , GroupBase , InputActionMeta , MultiValue , OnChangeValue , Options , PropsValue , SingleValue , } from './types' ; export const noop = ( ) => { } ; export const emptyString = ( ) => '' ; returns returns function applyPrefixToName ( prefix , name ) { if ( ! name ) { return prefix ; } else if ( name [ 0 ] === '-' ) { return prefix + name ; } else { return prefix + '__' + name ; } } export function classNames ( prefix ? , state ? , className ? ) { const arr = [ className ] ; if ( state && prefix ) { for ( let key in state ) { if ( state . hasOwnProperty ( key ) && state [ key ] ) { arr . push ( `${ applyPrefixToName ( prefix , key ) }` ) ; } } } return arr . filter ( ( i ) => i ) . map ( ( i ) => String ( i ) . trim ( ) ) . join ( ' ' ) ; } export const cleanValue = < Option > ( value ) : Options < Option > => { if ( isArray ( value ) ) return value . filter ( Boolean ) ; if ( typeof value === 'object' && value !== null ) return [ value ] ; return [ ] ; } ; export const cleanCommonProps = < Option , IsMulti extends boolean , Group extends GroupBase < Option > , AdditionalProps > ( props ) : Omit < AdditionalProps , keyof CommonPropsAndClassName < Option , IsMulti , Group > > => { const { className , clearValue , cx , getStyles , getValue , hasValue , isMulti , isRtl , options , selectOption , selectProps , setValue , theme , ... innerProps } = props ; return { ... innerProps } ; } ; export function handleInputChange ( inputValue , actionMeta , onInputChange ? ) { if ( onInputChange ) { const newValue = onInputChange ( inputValue , actionMeta ) ; if ( typeof newValue === 'string' ) return newValue ; } return inputValue ; } export function isDocumentElement ( el ) { return [ document . documentElement , document . body , window ] . indexOf ( el ) > - 1 ; } export function normalizedHeight ( el ) { if ( isDocumentElement ( el ) ) { return window . innerHeight ; } return el . clientHeight ; } export function getScrollTop ( el ) { if ( isDocumentElement ( el ) ) { return window . pageYOffset ; } return el . scrollTop ; } export function scrollTo ( el , top ) { if ( isDocumentElement ( el ) ) { window . scrollTo ( 0 , top ) ; return ; } el . scrollTop = top ; } export function getScrollParent ( element ) { let style = getComputedStyle ( element ) ; const excludeStaticParent = style . position === 'absolute' ; const overflowRx = /(auto|scroll)/ ; if ( style . position === 'fixed' ) return document . documentElement ; for ( let parent = element ; ( parent = parent . parentElement ) ; ) { style = getComputedStyle ( parent ) ; if ( excludeStaticParent && style . position === 'static' ) { continue ; } if ( overflowRx . test ( style . overflow + style . overflowY + style . overflowX ) ) { return parent ; } } return document . documentElement ; } param t param b param c param d function easeOutCubic ( t , b , c , d ) { return c * ( ( t = t / d - 1 ) * t * t + 1 ) + b ; } export function animatedScrollTo ( element , to , duration = 200 , callback = noop ) { const start = getScrollTop ( element ) ; const change = to - start ; const increment = 10 ; let currentTime = 0 ; function animateScroll ( ) { currentTime += increment ; const val = easeOutCubic ( currentTime , start , change , duration ) ; scrollTo ( element , val ) ; if ( currentTime < duration ) { window . requestAnimationFrame ( animateScroll ) ; } else { callback ( element ) ; } } animateScroll ( ) ; } export function scrollIntoView ( menuEl , focusedEl ) { const menuRect = menuEl . getBoundingClientRect ( ) ; const focusedRect = focusedEl . getBoundingClientRect ( ) ; const overScroll = focusedEl . offsetHeight / 3 ; if ( focusedRect . bottom + overScroll > menuRect . bottom ) { scrollTo ( menuEl , Math . min ( focusedEl . offsetTop + focusedEl . clientHeight - menuEl . offsetHeight + overScroll , menuEl . scrollHeight ) ) ; } else if ( focusedRect . top - overScroll < menuRect . top ) { scrollTo ( menuEl , Math . max ( focusedEl . offsetTop - overScroll , 0 ) ) ; } } export function getBoundingClientObj ( element ) { const rect = element . getBoundingClientRect ( ) ; return { bottom : rect . bottom , height : rect . height , left : rect . left , right : rect . right , top : rect . top , width : rect . width , } ; } export interface RectType { left : number ; right : number ; bottom : number ; height : number ; width : number ; } export function toKey ( str ) { return str . replace ( /\W/g , '-' ) ; } export function isTouchCapable ( ) { try { document . createEvent ( 'TouchEvent' ) ; return true ; } catch ( e ) { return false ; } } export function isMobileDevice ( ) { try { return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i . test ( navigator . userAgent ) ; } catch ( e ) { return false ; } } let passiveOptionAccessed = false ; const options = { get passive ( ) { return ( passiveOptionAccessed = true ) ; } , } ; const w = typeof window !== 'undefined' ? window : { } ; if ( w . addEventListener && w . removeEventListener ) { w . addEventListener ( 'p' , noop , options ) ; w . removeEventListener ( 'p' , noop , false ) ; } export const supportsPassiveEvents = passiveOptionAccessed ; export function notNullish < T > ( item ) { return item != null ; } export function isArray < T > ( arg ) { return Array . isArray ( arg ) ; } export function valueTernary < Option , IsMulti extends boolean > ( isMulti , multiValue , singleValue ) { return ( isMulti ? multiValue : singleValue ) as OnChangeValue < Option , IsMulti > ; } export function singleValueAsValue < Option , IsMulti extends boolean > ( singleValue ) { return singleValue as OnChangeValue < Option , IsMulti > ; } export function multiValueAsValue < Option , IsMulti extends boolean > ( multiValue ) { return multiValue as OnChangeValue < Option , IsMulti > ; }