ldx-widgets
Version:
widgets
255 lines (211 loc) • 7.39 kB
text/coffeescript
React = require 'react'
createClass = require 'create-react-class'
PropTypes = require 'prop-types'
Spinner = React.createFactory(require './spinner')
InputMixin = require '../mixins/input_mixin'
{div, button, label, input} = require 'react-dom-factories'
###&
TextInput2 is the updated version of TextInput. This component uses the validation store.
.value - REQUIRED - [String]
The value of the input
w/ out this props, the input will not work
.name - OPTIONAL - [String]
The name of the input
.onChange - REQUIRED - [Function]
method that will change the value of the input prop
gets passed a single param that is the new value of the input
w/ out this method, the input will not update
.className - OPTIONAL - [String] - default 'text-input'
optional class to be added the input element itself
.inputTextClass - OPTIONAL - [String]
optional class to be added the input element itself when using the gridInputForm
.id - OPTIONAL - [String] - default null
optional id to be added the input element itself
.placeholder - OPTIONAL - [String] - default null
optional placeholder text for the input
.wrapperClass - OPTIONAL - [String] default null
class to be added to the wrapper div
.wrapperLabel - OPTIONAL - default null
text for wrapping label element
.loading - OPTIONAL - [Boolean]
indicator to determine that the input is loading a value from the server
.showClear - OPTIONAL - [Boolean]
indicator to determine whether or not to show the clear "X" button
.tabIndex - OPTIONAL - [Number] - default null
tab index for the input
.maxLength - OPTIONAL - [Number] - default null
max characters that can be entered in the input
.onKeyDown/onKeyPress/onKeyUp/onEnterKey/onBlur/onFocus
optional handlers for various events
.disabled - OPTIONAL - [Boolean] - default no
disabled state of the input
.validation - OPTIONAL - [Function]
a method that takes the value and returns an arry of validation objects
always return an empty array for a valid value
see the validation store for more documentation on validation objects
.isInPopover - OPTIONAL - [Boolean] - default no
set this to yes if the form is inside a popover or modal, forces the
validation to display above the popover/modal layer
Inconsequential if validation is not being used
.delayedActionOnChange - OPTIONAL - [Object]
Takes action and interval parameters, will fire the action after the set interval everytime the data changes
.textTransform - OPTIONAL - [Function]
runs the value through a transform method that will change the input before returning via getValue()
.focusOnMount - OPTIONAL - [Boolean]
Focuses the input once mounted
.tabId - OPTIONAL - [String]
If this input is within a tab, this id will be used for validation errors in the store
.returnNull - REQUIRED - [Boolean]
Defaults to false
For a select, when the default or 'placeholder' value is selected, instead of returning empty string, return null
For a text input, when the value is empty string, return null
&###
TextInput2 = createClass
displayName: 'TextInput2'
mixins: [InputMixin]
contextTypes:
clearValidationError: PropTypes.func
addValidationError: PropTypes.func
getValidationStatus: PropTypes.func
toggleValidationError: PropTypes.func
propTypes:
type: PropTypes.string
name: PropTypes.string
className: PropTypes.string
textTransform: PropTypes.func
focusOnMount: PropTypes.bool
delayedActionOnChange: PropTypes.object
tabIndex: PropTypes.number
maxLength: PropTypes.number
disabled: PropTypes.bool
onChange: PropTypes.func
onInput: PropTypes.func
onKeyDown: PropTypes.func
onKeyUp: PropTypes.func
onKeyPress: PropTypes.func
style: PropTypes.object
autoComplete: PropTypes.oneOfType [
PropTypes.string
PropTypes.bool
]
placeholder: PropTypes.oneOfType [
PropTypes.string
PropTypes.number
]
value: PropTypes.oneOfType([
PropTypes.string
PropTypes.number
])
id: PropTypes.oneOfType [
PropTypes.string
PropTypes.number
]
tabId: PropTypes.oneOfType([
PropTypes.string
PropTypes.number
])
returnNull: PropTypes.bool.isRequired
getDefaultProps: ->
type: 'text'
className: 'text-input'
id: null
name: null
placeholder: ''
wrapperClass: null
wrapperLabel: null
loading: no
showClear: no
tabIndex: null
maxLength: null
onKeyDown: null
onKeyPress: null
onFocus: null
onBlur: null
onKeyUp: null
onEnterKey: null
onChange: null
onInput: null
disabled: no
autoComplete: no
validation: off
isInPopover: no
delayedActionOnChange: null
spellCheck: no
style: null
focusOnMount: no
returnNull: no
render: ->
{value, name, placeholder, tabIndex, className, inputTextClass, maxLength, loading, type, showClear, onInput, onKeyDown, onKeyPress, onBlur, onFocus, wrapperClass, wrapperLabel, id, disabled, validation, autoComplete, spellCheck, style, focusOnMount} =
{error, forceShowAllErrors} = .getValidationStatus()
{valueHasChanged} =
isValid = not error?
outerClass = 'field-wrap'
outerClass += " #{wrapperClass}" if wrapperClass?
outerClass += ' invalid' if not isValid and (valueHasChanged or forceShowAllErrors)
inputClass = if className? then className else ''
inputClass += ' loading-spinner' if loading
inputClass += " #{inputTextClass}" if inputTextClass?
errorButtonClass = 'field-errors-show'
errorButtonClass += ' is-hidden' if loading
# Autocomplete
autoComplete = switch typeof autoComplete
when 'string' then autoComplete
when 'boolean'
if autoComplete then 'on' else 'off'
textInput = input {
key: 'textInput'
ref: 'input'
id: id
name: name
className: inputClass
type: type
value: value or ''
placeholder: placeholder
maxLength: maxLength
disabled: disabled
tabIndex: tabIndex
onChange: unless onInput?
onInput: if onInput?
onKeyUp:
onKeyDown: onKeyDown
onKeyPress: onKeyPress
onBlur: onBlur
onFocus: onFocus
autoComplete: autoComplete
spellCheck: spellCheck
}
# Add a wrapper label element if label prop is present
if wrapperLabel
textInput = label {
key: 'textInputLabel'
}, [wrapperLabel, textInput]
div {
className: "#{outerClass}"
style: style
}, [
textInput
div({
key: 'input-spinner'
className: 'input-spinner'
},
Spinner {
length: 3
}
) if loading
button {
className: 'search-clear'
title: 'Clear Search'
key: 'searchClearBtn'
onClick:
tabIndex: -1
} if value?.length and showClear
div {
className: errorButtonClass
key: 'textInputErrorsShow'
ref: 'errorAnchor'
onMouseOver:
onMouseOut:
}
]
module.exports = TextInput2