|
| 1 | +/* |
| 2 | +Copyright (c) 2015, salesforce.com, inc. All rights reserved. |
| 3 | +
|
| 4 | +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: |
| 5 | +Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. |
| 6 | +Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. |
| 7 | +Neither the name of salesforce.com, inc. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. |
| 8 | +
|
| 9 | +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 10 | +*/ |
| 11 | +/* eslint-disable indent */ |
| 12 | + |
| 13 | +// # Checkbox Component |
| 14 | + |
| 15 | +// Implements the [Checkbox design pattern](https://www.lightningdesignsystem.com/components/forms/#checkbox) in React. |
| 16 | + |
| 17 | +// ### React |
| 18 | +import React from 'react'; |
| 19 | + |
| 20 | +// ### isFunction |
| 21 | +import isFunction from 'lodash.isfunction'; |
| 22 | + |
| 23 | +// ### Event Helpers |
| 24 | +import { KEYS, EventUtil } from "../utils"; |
| 25 | + |
| 26 | +// ### classNames |
| 27 | +import classNames from 'classnames'; |
| 28 | + |
| 29 | +// Remove the need for `React.PropTypes` |
| 30 | +const { PropTypes } = React; |
| 31 | + |
| 32 | +export const COMPONENT = 'Checkbox'; |
| 33 | + |
| 34 | +/** |
| 35 | + * The ability to style checkboxes with CSS varies across browsers. Using this component ensures checkboxes look the same everywhere. |
| 36 | + */ |
| 37 | +const Checkbox = React.createClass({ |
| 38 | + // ### Display Name |
| 39 | + // Always use the canonical component name as the React display name. |
| 40 | + displayName: COMPONENT, |
| 41 | + |
| 42 | + // ### Prop Types |
| 43 | + propTypes: { |
| 44 | + /** |
| 45 | + * Text that is visually hidden but read aloud by screenreaders to tell the user what the Checkbox is for. |
| 46 | + * If the Checkbox has a visible label, you can omit the <code>assistiveText</code> prop and use the <code>label</code> prop. |
| 47 | + */ |
| 48 | + assistiveText: React.PropTypes.string, |
| 49 | + /** |
| 50 | + * The Checkbox is a controlled component, and will always be in this state. |
| 51 | + */ |
| 52 | + checked: React.PropTypes.bool, |
| 53 | + /** |
| 54 | + * Class names to be added to the outer container of the Checkbox. |
| 55 | + */ |
| 56 | + className: PropTypes.oneOfType([ |
| 57 | + PropTypes.array, |
| 58 | + PropTypes.object, |
| 59 | + PropTypes.string |
| 60 | + ]), |
| 61 | + /** |
| 62 | + * Disables the Checkbox and prevents clicking it. |
| 63 | + */ |
| 64 | + disabled: PropTypes.bool, |
| 65 | + /** |
| 66 | + * Message to display when the Checkbox is in an error state. When this is present, also visually highlights the component as in error. |
| 67 | + */ |
| 68 | + errorText: React.PropTypes.string, |
| 69 | + /** |
| 70 | + * An optional label for the Checkbox. |
| 71 | + */ |
| 72 | + label: React.PropTypes.string, |
| 73 | + /** |
| 74 | + * This event fires when the Checkbox changes. |
| 75 | + */ |
| 76 | + onChange: PropTypes.func, |
| 77 | + /** |
| 78 | + * Highlights the Checkbox as a required field (does not perform any validation). |
| 79 | + */ |
| 80 | + required: PropTypes.bool |
| 81 | + }, |
| 82 | + |
| 83 | + // ### Render |
| 84 | + render () { |
| 85 | + const { |
| 86 | + assistiveText, |
| 87 | + checked, |
| 88 | + className, |
| 89 | + disabled, |
| 90 | + errorText, |
| 91 | + label, |
| 92 | + onChange, |
| 93 | + required, |
| 94 | + |
| 95 | + // ### Additional properties |
| 96 | + // Using [object destructuring](https://facebook.github.io/react/docs/transferring-props.html#transferring-with-...-in-jsx) to pass on any properties which are not explicitly defined. |
| 97 | + ...props |
| 98 | + } = this.props; |
| 99 | + |
| 100 | + return ( |
| 101 | + <div className={classNames('slds-form-element', { |
| 102 | + 'is-required': required, |
| 103 | + 'slds-has-error': errorText |
| 104 | + }, |
| 105 | + className)} |
| 106 | + onKeyDown={this.handleKeyDown} |
| 107 | + > |
| 108 | + <div className="slds-form-element__control"> |
| 109 | + <label className="slds-checkbox"> |
| 110 | + {required ? <abbr className="slds-required" title="required">*</abbr> : null} |
| 111 | + <input |
| 112 | + {...props} |
| 113 | + checked={checked} |
| 114 | + disabled={disabled} |
| 115 | + onChange={this.handleChange} |
| 116 | + type="checkbox" |
| 117 | + /> |
| 118 | + <span className="slds-checkbox--faux"></span> |
| 119 | + {label |
| 120 | + ? <span className="slds-form-element__label"> |
| 121 | + {label} |
| 122 | + </span> |
| 123 | + : null} |
| 124 | + {assistiveText |
| 125 | + ? <span className="slds-assistive-text"> |
| 126 | + {assistiveText} |
| 127 | + </span> |
| 128 | + : null} |
| 129 | + </label> |
| 130 | + </div> |
| 131 | + {errorText ? <div className="slds-form-element__help">{errorText}</div> : null} |
| 132 | + </div> |
| 133 | + ); |
| 134 | + }, |
| 135 | + |
| 136 | + handleChange (e) { |
| 137 | + if (isFunction(this.props.onChange)) { |
| 138 | + this.props.onChange(!this.props.checked, e); |
| 139 | + } |
| 140 | + }, |
| 141 | + |
| 142 | + handleKeyDown (e){ |
| 143 | + if (e.keyCode) { |
| 144 | + if (e.keyCode === KEYS.ENTER || |
| 145 | + e.keyCode === KEYS.SPACE) { |
| 146 | + EventUtil.trap(e); |
| 147 | + this.handleChange(e); |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | +}); |
| 152 | + |
| 153 | +export default Checkbox; |
0 commit comments