Repository URL to install this package:
|
Version:
2.8.0-studio-release ▾
|
import { EMPTY_ARRAY, isArray, isObj } from 'exotic'
import { omit } from '@skava/utils'
import toClassname from 'classnames'
import { SelectProps, OptionProps, OptionState } from '../typings'
import { SelectableState } from 'src/state/SelectionState'
export function toClassList(props: SelectProps, state: SelectableState) {
const className = toClassname(props.className, {
selected: props.isSelected,
select: true,
'input-error': state.isValidInput === false,
})
return className
}
export function toOnChange(props: SelectProps) {
return props.onDropDownStateChange || props.onChange || props.onChangeValue
}
export function toList(
props: SelectProps | OptionProps
): Array<SelectProps | OptionProps> {
// if elseif if
if (isArray(props.list) === true) {
return props.list
}
else if (isArray(props.options) === true) {
return props.options
}
else {
return EMPTY_ARRAY
}
}
const omitList = [
'list',
'options',
'onDropDownStateChange',
'onChangeValue',
'onChange',
'className',
'isActive',
]
Object.freeze(omitList)
export function toAccessibleAttributes(
props: SelectProps,
state: SelectableState
) {
// aria-haspopup="listbox"
// aria-activedescendant="exp_elem_Cm"
// aria-expanded="true"
// tabindex="-1"
// @todo .attributes
return '@@todo'
}
export function toAttributes(props: SelectProps, state: SelectableState) {
return {
onChange: toOnChange(props),
list: toList(props),
className: toClassList(props, state),
passthroughProps: omit(props, omitList),
}
}
export function defaultRenderEmpty(props: SelectProps, state: SelectableState) {
console.warn('SelectDropDown received no dropdowns')
return ''
}
export function toText(props: OptionProps, state?: OptionState): string {
const text =
// @todo :: @james @sri label is not getting update onChange(), renderActiveItem prop
props.label || props.value || props.children || ''
// try on props, then try on state
if (!text && isObj(state) && arguments.length === 2) {
return toText(state)
} else {
return String(text)
}
}
export default {
toClassList,
toOnChange,
toList,
toAccessibleAttributes,
toAttributes,
defaultRenderEmpty,
}