|
| 1 | +/*\ |
| 2 | +|*| :: Bootstrap Switch Button :: |
| 3 | +|*| |
| 4 | +|*| Dream Journal App - Record and Search Daily Dream Entries |
| 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 './style.css' |
| 32 | + |
| 33 | +export default class BootstrapSwitchButton extends React.Component< |
| 34 | + { |
| 35 | + onChange?: Function |
| 36 | + enable?: Function |
| 37 | + toggle?: Function |
| 38 | + // TODO: add `on` and `off` too so they can be set via code |
| 39 | + // TODO: implemnt willRecvProps or whatever so these 3 work ^^^ |
| 40 | + |
| 41 | + checked?: boolean |
| 42 | + disabled?: boolean |
| 43 | + onlabel?: string |
| 44 | + onstyle?: string |
| 45 | + offlabel?: string |
| 46 | + offstyle?: string |
| 47 | + size?: string |
| 48 | + style?: string |
| 49 | + width?: number |
| 50 | + height?: number |
| 51 | + }, |
| 52 | + { |
| 53 | + checked: boolean |
| 54 | + disabled: boolean |
| 55 | + onlabel: string |
| 56 | + onstyle: string |
| 57 | + offlabel: string |
| 58 | + offstyle: string |
| 59 | + size: string |
| 60 | + style: string |
| 61 | + width: number |
| 62 | + height: number |
| 63 | + } |
| 64 | +> { |
| 65 | + constructor( |
| 66 | + props: Readonly<{ |
| 67 | + toggle?: Function |
| 68 | + |
| 69 | + checked?: boolean |
| 70 | + disabled?: boolean |
| 71 | + onlabel?: string |
| 72 | + onstyle?: string |
| 73 | + offlabel?: string |
| 74 | + offstyle?: string |
| 75 | + size?: string |
| 76 | + style?: string |
| 77 | + width?: number |
| 78 | + height?: number |
| 79 | + }> |
| 80 | + ) { |
| 81 | + super(props) |
| 82 | + |
| 83 | + this.state = { |
| 84 | + checked: this.props.checked || true, |
| 85 | + disabled: this.props.disabled || false, |
| 86 | + onlabel: this.props.onlabel || 'On', |
| 87 | + onstyle: this.props.onstyle || 'primary', |
| 88 | + offlabel: this.props.offlabel || 'Off', |
| 89 | + offstyle: this.props.offstyle || 'light', |
| 90 | + size: this.props.size || '', |
| 91 | + style: this.props.style || '', |
| 92 | + width: this.props.width || null, |
| 93 | + height: this.props.height || null, |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + toggle = event => { |
| 98 | + if (this.state.checked) this.off() |
| 99 | + else this.on() |
| 100 | + } |
| 101 | + off = () => { |
| 102 | + if (!this.state.disabled) { |
| 103 | + this.setState({ |
| 104 | + checked: false, |
| 105 | + }) |
| 106 | + if (this.props.onChange) this.props.onChange(this.state.checked) |
| 107 | + } |
| 108 | + } |
| 109 | + on = () => { |
| 110 | + if (!this.state.disabled) { |
| 111 | + this.setState({ |
| 112 | + checked: true, |
| 113 | + }) |
| 114 | + /* |
| 115 | + this.state.classList.remove( 'btn-'+this.options.offstyle ); |
| 116 | + this.state.classList.add( 'btn-'+this.options.onstyle ); |
| 117 | + this.state.classList.remove( 'off' ); |
| 118 | + this.state.checked = true |
| 119 | + */ |
| 120 | + if (this.props.onChange) this.props.onChange(this.state.checked) |
| 121 | + } |
| 122 | + } |
| 123 | + enable = () => { |
| 124 | + this.setState({ |
| 125 | + disabled: false, |
| 126 | + }) |
| 127 | + } |
| 128 | + disable = () => { |
| 129 | + this.setState({ |
| 130 | + disabled: true, |
| 131 | + }) |
| 132 | + } |
| 133 | + |
| 134 | + calcH = el => { |
| 135 | + const styles = window.getComputedStyle(el) |
| 136 | + const height = el.offsetHeight |
| 137 | + const borderTopWidth = parseFloat(styles.borderTopWidth) |
| 138 | + const borderBottomWidth = parseFloat(styles.borderBottomWidth) |
| 139 | + const paddingTop = parseFloat(styles.paddingTop) |
| 140 | + const paddingBottom = parseFloat(styles.paddingBottom) |
| 141 | + |
| 142 | + //return height - borderBottomWidth - borderTopWidth - paddingTop - paddingBottom |
| 143 | + // TODO: set new state, then have button use height style on re-render? |
| 144 | + } |
| 145 | + |
| 146 | + // style={{ width: '81.875px', height: '48px' }}> |
| 147 | + render() { |
| 148 | + let style = {} |
| 149 | + this.props.width ? (style['width'] = this.props.width + 'px') : null |
| 150 | + this.props.height ? (style['height'] = this.props.height + 'px') : null |
| 151 | + |
| 152 | + return ( |
| 153 | + <div |
| 154 | + className={ |
| 155 | + 'switch btn ' + |
| 156 | + (this.state.checked ? 'on btn-' + this.state.onstyle : 'off btn-' + this.state.offstyle) + |
| 157 | + (this.state.size ? ' btn-' + this.state.size : '') + |
| 158 | + (this.state.style ? ' ' + this.state.style : '') |
| 159 | + } |
| 160 | + style={style} |
| 161 | + onClick={this.toggle} |
| 162 | + onTouchStart={this.toggle}> |
| 163 | + <div className='switch-group'> |
| 164 | + <label |
| 165 | + className={ |
| 166 | + 'switch-on btn btn-' + |
| 167 | + this.state.onstyle + |
| 168 | + (this.state.size ? ' btn-' + this.state.size : '') |
| 169 | + }> |
| 170 | + {this.state.onlabel} |
| 171 | + </label> |
| 172 | + <label |
| 173 | + className={ |
| 174 | + 'switch-off btn btn-' + |
| 175 | + this.state.offstyle + |
| 176 | + (this.state.size ? ' btn-' + this.state.size : '') |
| 177 | + }> |
| 178 | + {this.state.offlabel} |
| 179 | + </label> |
| 180 | + <span |
| 181 | + className={'switch-handle btn btn-light' + (this.state.size ? 'btn-' + this.state.size : '')} |
| 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