Repository URL to install this package:
Version:
0.14.1 ▾
|
/* eslint-disable brace-style */
import React from 'react'
import { EMPTY_OBJ, isObj, fromCollectionToObj } from 'exotic'
import { action } from 'xmobx/mobx'
import { InputStateType } from './typings'
import ReactChain from './ReactChain'
// @todo import from proper place
const isReactProps = props => Object.isExtensible(props) === false
/**
* @todo the input should have the aria-error props and that should be tested in a unit test
*/
class InputChain<Props = any, State = any> extends ReactChain<Props, State> {
// static isSatisfiedByProps(props: Props): boolean {
// return true
// }
// validate(): void {}
parent?: any
constructor(parent: any) {
// @todo arguments.length?
if (isReactProps(parent)) {
super(parent)
} else {
super(EMPTY_OBJ)
}
this.parent = parent
this.store = new Map()
this.store.set('props', EMPTY_OBJ)
this.store.set('state', EMPTY_OBJ)
}
entries() {
return fromCollectionToObj(this.store)
}
/**
* @note - had to change to this for testing & react warnings
* @note - make sure it's always an object
*/
setPluginState(state: InputStateType) {
if (isObj(state) === false) {
console.warn(
'tried to set non object `state` in `InputChain` - expand for stack trace'
)
return this.store.set('state', {})
} else {
return this.store.set('state', state)
}
}
/**
* @action
*/
setPluginProps(props: Props) {
if (isObj(props) === false) {
// @todo default to EMPTY_OBJ?
console.warn(
'tried to set non object `props` in `InputChain` - expand for stack trace'
)
return this.store.set('props', {})
} else {
return this.store.set('props', props)
}
}
/**
* @action
*/
set isValid(isValid: boolean) {
// hm, will also put on state
this.set('isValid', isValid)
this.get('state').isValidInput = isValid
}
/**
* may also want to do relational
*
* @sriaarthi
* @example
* - this can be done the same with `isEnabled`
* and would allow only being enabled once
* this.parent.get('add-to-account').isSelected
*/
get isValid(): boolean {
const isValidInput = this.get('state').isValidInput
return isValidInput
}
/**
* === added these since they were commmon ===
*/
/**
* @computed
*/
get type(): string {
return this.get('state').value || this.get('props').value
}
/**
* @action
*/
@action.bound
setValue(value: any): void {
this.get('state').value = value
// return this
}
/**
* @computed
*/
getValue() {
return this.get('state').value
}
}
export { InputChain }
export default InputChain