ldx-widgets
Version:
widgets
252 lines (209 loc) • 7.05 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, textarea} = require 'react-dom-factories'
###
Textarea Props
@props.value - REQUIRED - String
The value of the textarea
w/ out this props, the textarea will not work
@props.name - OPTIONAL - [String]
The name of the input
@props.onChange - REQUIRED
method that will change the value of the textarea prop
gets passed a single param that is the new value of the textarea
w/ out this method, the textarea will not update
@props.className - OPTIONAL - string - default 'text-textarea'
optional class to be added the textarea element itself
@props.inputTextClass - OPTIONAL - string
optional class to be added the input element itself when using the gridInputForm
@props.id - OPTIONAL - string - default null
optional id to be added the textarea element itself
@props.placeholder - OPTIONAL - string - default null
optional placeholder text for the textarea
@props.wrapperClass - OPTIONAL - string default null
class to be added to the wrapper div
@props.wrapperLabel - OPTIONAL - default null
text for wrapping label element
@props.loading - OPTIONAL - Boolean
indicator to determine that the textarea is loading a value from the server
@props.showClear - OPTIONAL - Boolean
indicator to determine whether or not to show the clear "X" button
@props.tabIndex - OPTIONAL - int - default null
tab index for the textarea
@props.maxLength - OPTIONAL - int - default null
max characters that can be entered in the textarea
@props.onKeyDown/onKeyPress/onKeyUp/onEnterKey/onBlur/onFocus
optional handlers for various events
@props.disabled - OPTIONAL - Boolean - default no
disabled state of the textarea
@props.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
@props.isInPopover - OPTIONAL - 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
@props.delayedActionOnChange - OPTIONAL - Object
Takes action and interval parameters, will fire the action after the set interval everytime the data changes
@props.textTransform - OPTIONAL - Function
runs the value through a transform method that will change the textarea before returning via getValue()
@props.focusOnMount - OPTIONAL - Boolean
Focuses the textarea once mounted
@props.selectOnMount - OPTIONAL - [Boolean]
Selects all the input's text once mounted (TextInput and Textarea only)
@props.rows - OPTIONAL - Number string - default 4
Height of textarea
@props.cols - OPTIONAL - Number string - default 50
Width of textarea
###
Textarea = createClass
displayName: 'Textarea'
mixins: [InputMixin]
contextTypes:
clearValidationError: PropTypes.func
addValidationError: PropTypes.func
getValidationStatus: PropTypes.func
toggleValidationError: PropTypes.func
propTypes:
name: PropTypes.string
className: PropTypes.string
textTransform: PropTypes.func
focusOnMount: PropTypes.bool
selectOnMount: PropTypes.bool
delayedActionOnChange: PropTypes.object
tabIndex: PropTypes.number
maxLength: PropTypes.number
disabled: PropTypes.bool
onChange: PropTypes.func
onKeyDown: PropTypes.func
onKeyUp: PropTypes.func
onKeyPress: PropTypes.func
style: PropTypes.object
rows: PropTypes.string
cols: PropTypes.string
autoComplete: PropTypes.oneOfType [
PropTypes.string
PropTypes.bool
]
placeholder: PropTypes.oneOfType [
PropTypes.string
PropTypes.number
]
value: PropTypes.oneOfType([
PropTypes.string
PropTypes.number
]).isRequired
id: PropTypes.oneOfType [
PropTypes.string
PropTypes.number
]
tabId: PropTypes.oneOfType([
PropTypes.string
PropTypes.number
])
getDefaultProps: ->
className: 'textarea'
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
disabled: no
autoComplete: no
validation: off
isInPopover: no
delayedActionOnChange: null
spellCheck: yes
style: null
focusOnMount: false
selectOnMount: false
rows: "4"
cols: "50"
render: ->
{name, value, placeholder, tabIndex, className, maxLength, loading, showClear, onKeyDown, onKeyPress, onBlur, onFocus, wrapperClass, wrapperLabel, id, disabled, validation, autoComplete, spellCheck, style, focusOnMount, rows, cols} = @props
{error, forceShowAllErrors} = @context.getValidationStatus(@inputId)
{valueHasChanged} = @state
isValid = not error?
outerClass = 'field-wrap'
outerClass += " #{wrapperClass}" if wrapperClass?
outerClass += ' invalid' if not isValid and (valueHasChanged or forceShowAllErrors)
textareaClass = className if className?
textareaClass += ' loading-spinner' if loading
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'
textArea = textarea {
key: 'textArea'
ref: 'input'
name: name
id: id
className: textareaClass
value: value
placeholder: placeholder
maxLength: maxLength
disabled: disabled
tabIndex: tabIndex
onChange: @handleChange
onKeyUp: @handleKeyUp
onKeyDown: onKeyDown
onKeyPress: onKeyPress
onBlur: onBlur
onFocus: onFocus
autoComplete: autoComplete
spellCheck: spellCheck
rows
cols
}
# Add a wrapper label element if label prop is present
if wrapperLabel
textArea = label {
key: 'textAreaLabel'
}, [wrapperLabel, textArea]
div {
className: "#{outerClass}"
style: style
}, [
textArea
div({
key: 'input-spinner'
className: 'input-spinner'
},
Spinner {
length: 3
}
) if loading
button {
className: 'search-clear'
title: 'Clear Search'
key: 'searchClearBtn'
onClick: @clear
tabIndex: -1
} if value?.length > 0 and showClear
div {
className: errorButtonClass
key: 'textAreaErrorsShow'
ref: 'errorAnchor'
onMouseOver: @handleErrorMouseOver
onMouseOut: @handleErrorMouseOut
}
]
module.exports = Textarea