|
| 1 | +/*\ |
| 2 | +|*| :: Bootstrap Switch Button :: |
| 3 | +|*| |
| 4 | +|*| Bootstrap Switch Button (React) |
| 5 | +|*| https://github.com/gitbrent/bootstrap-switch-button-react |
| 6 | +|*| |
| 7 | +|*| This library is released under the MIT Public License (MIT) |
| 8 | +|*| |
| 9 | +|*| Dream Journal App (C) 2019-present Brent Ely (https://github.com/gitbrent) |
| 10 | +|*| |
| 11 | +|*| Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | +|*| of this software and associated documentation files (the "Software"), to deal |
| 13 | +|*| in the Software without restriction, including without limitation the rights |
| 14 | +|*| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | +|*| copies of the Software, and to permit persons to whom the Software is |
| 16 | +|*| furnished to do so, subject to the following conditions: |
| 17 | +|*| |
| 18 | +|*| The above copyright notice and this permission notice shall be included in all |
| 19 | +|*| copies or substantial portions of the Software. |
| 20 | +|*| |
| 21 | +|*| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | +|*| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | +|*| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | +|*| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | +|*| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | +|*| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 27 | +|*| SOFTWARE. |
| 28 | +\*/ |
| 29 | + |
| 30 | +import React from "react"; |
| 31 | +//import PropTypes from "prop-types"; |
| 32 | +import "./style.css"; |
| 33 | +/* |
| 34 | +export default class BootstrapSwitchButton extends React.Component { |
| 35 | + static propTypes = { |
| 36 | + onChange, |
| 37 | + enable, |
| 38 | + toggle, |
| 39 | + // TODO: add `on` and `off` too so they can be set via code |
| 40 | + // TODO: implemnt willRecvProps or whatever so these 3 work ^^^ |
| 41 | +
|
| 42 | + checked, |
| 43 | + disabled, |
| 44 | + onlabel, |
| 45 | + onstyle, |
| 46 | + offlabel, |
| 47 | + offstyle, |
| 48 | + size, |
| 49 | + style, |
| 50 | + width, |
| 51 | + height |
| 52 | + }; |
| 53 | + static get defaultProps() { |
| 54 | + return; |
| 55 | + { |
| 56 | + checked; |
| 57 | + disabled; |
| 58 | + onlabel; |
| 59 | + onstyle; |
| 60 | + offlabel; |
| 61 | + offstyle; |
| 62 | + size; |
| 63 | + style; |
| 64 | + width; |
| 65 | + height; |
| 66 | + } |
| 67 | + } |
| 68 | +*/ |
| 69 | +export default class BootstrapSwitchButton extends React.Component { |
| 70 | + constructor(props) { |
| 71 | + super(props); |
| 72 | + |
| 73 | + this.props.onChange = this.props.onChange.bind(this); |
| 74 | + |
| 75 | + this.state = { |
| 76 | + checked: this.props.checked || true, |
| 77 | + disabled: this.props.disabled || false, |
| 78 | + onlabel: this.props.onlabel || "On", |
| 79 | + onstyle: this.props.onstyle || "primary", |
| 80 | + offlabel: this.props.offlabel || "Off", |
| 81 | + offstyle: this.props.offstyle || "light", |
| 82 | + size: this.props.size || "", |
| 83 | + style: this.props.style || "", |
| 84 | + width: this.props.width || null, |
| 85 | + height: this.props.height || null |
| 86 | + }; |
| 87 | + } |
| 88 | + |
| 89 | + toggle = event => { |
| 90 | + if (this.state.checked) this.off(); |
| 91 | + else this.on(); |
| 92 | + }; |
| 93 | + off = () => { |
| 94 | + if (!this.state.disabled) { |
| 95 | + this.setState({ |
| 96 | + checked: false |
| 97 | + }); |
| 98 | + if (this.props.onChange) this.props.onChange(this.state.checked); |
| 99 | + } |
| 100 | + }; |
| 101 | + on = () => { |
| 102 | + if (!this.state.disabled) { |
| 103 | + this.setState({ |
| 104 | + checked: true |
| 105 | + }); |
| 106 | + /* |
| 107 | + this.state.classList.remove( 'btn-'+this.options.offstyle ); |
| 108 | + this.state.classList.add( 'btn-'+this.options.onstyle ); |
| 109 | + this.state.classList.remove( 'off' ); |
| 110 | + this.state.checked = true |
| 111 | + */ |
| 112 | + if (this.props.onChange) this.props.onChange(this.state.checked); |
| 113 | + } |
| 114 | + }; |
| 115 | + enable = () => { |
| 116 | + this.setState({ |
| 117 | + disabled: false |
| 118 | + }); |
| 119 | + }; |
| 120 | + disable = () => { |
| 121 | + this.setState({ |
| 122 | + disabled: true |
| 123 | + }); |
| 124 | + }; |
| 125 | + |
| 126 | + calcH = el => { |
| 127 | + const styles = window.getComputedStyle(el); |
| 128 | + const height = el.offsetHeight; |
| 129 | + const borderTopWidth = parseFloat(styles.borderTopWidth); |
| 130 | + const borderBottomWidth = parseFloat(styles.borderBottomWidth); |
| 131 | + const paddingTop = parseFloat(styles.paddingTop); |
| 132 | + const paddingBottom = parseFloat(styles.paddingBottom); |
| 133 | + |
| 134 | + //return height - borderBottomWidth - borderTopWidth - paddingTop - paddingBottom |
| 135 | + // TODO: set new state, then have button use height style on re-render? |
| 136 | + }; |
| 137 | + |
| 138 | + // style={{ width: '81.875px', height: '48px' }}> |
| 139 | + render = () => { |
| 140 | + let style = {}; |
| 141 | + this.props.width ? (style["width"] = this.props.width + "px") : null; |
| 142 | + this.props.height ? (style["height"] = this.props.height + "px") : null; |
| 143 | + |
| 144 | + return ( |
| 145 | + <div |
| 146 | + className={ |
| 147 | + "switch btn " + |
| 148 | + (this.state.checked |
| 149 | + ? "on btn-" + this.state.onstyle |
| 150 | + : "off btn-" + this.state.offstyle) + |
| 151 | + (this.state.size ? " btn-" + this.state.size : "") + |
| 152 | + (this.state.style ? " " + this.state.style : "") |
| 153 | + } |
| 154 | + style={style} |
| 155 | + onClick={this.toggle} |
| 156 | + onTouchStart={this.toggle} |
| 157 | + > |
| 158 | + <div className="switch-group"> |
| 159 | + <label |
| 160 | + className={ |
| 161 | + "switch-on btn btn-" + |
| 162 | + this.state.onstyle + |
| 163 | + (this.state.size ? " btn-" + this.state.size : "") |
| 164 | + } |
| 165 | + > |
| 166 | + {this.state.onlabel} |
| 167 | + </label> |
| 168 | + <label |
| 169 | + className={ |
| 170 | + "switch-off btn btn-" + |
| 171 | + this.state.offstyle + |
| 172 | + (this.state.size ? " btn-" + this.state.size : "") |
| 173 | + } |
| 174 | + > |
| 175 | + {this.state.offlabel} |
| 176 | + </label> |
| 177 | + <span |
| 178 | + className={ |
| 179 | + "switch-handle btn btn-light" + |
| 180 | + (this.state.size ? "btn-" + this.state.size : "") |
| 181 | + } |
| 182 | + /> |
| 183 | + </div> |
| 184 | + </div> |
| 185 | + ); |
| 186 | + }; |
| 187 | + |
| 188 | + /* TODO: |
| 189 | + if ( this.options.height ) { |
| 190 | + switchOn.style.lineHeight = calcH(switchOn)+'px'; |
| 191 | + switchOff.style.lineHeight = calcH(switchOff)+'px'; |
| 192 | + } |
| 193 | + */ |
| 194 | +} |
0 commit comments