index.js
89 lines
| 1 | import hasNumericValue from '../../utils/has-numeric-value'; |
| 2 | |
| 3 | // Import CSS |
| 4 | import './editor.scss'; |
| 5 | |
| 6 | import { |
| 7 | Component, |
| 8 | } from '@wordpress/element'; |
| 9 | |
| 10 | import { |
| 11 | RangeControl, |
| 12 | TextControl, |
| 13 | } from '@wordpress/components'; |
| 14 | |
| 15 | export default class RangeControlInput extends Component { |
| 16 | render() { |
| 17 | const { |
| 18 | label, |
| 19 | value, |
| 20 | onChange, |
| 21 | rangeMin = 0, |
| 22 | rangeMax = 100, |
| 23 | inputMin = '', |
| 24 | inputMax = '', |
| 25 | step = 1, |
| 26 | help = '', |
| 27 | beforeIcon = '', |
| 28 | initialPosition = '', |
| 29 | placeholder = '', |
| 30 | disabled = false, |
| 31 | } = this.props; |
| 32 | |
| 33 | return ( |
| 34 | <div className="components-gblocks-range-control"> |
| 35 | { label && |
| 36 | <div className="components-gblocks-range-control--label"> |
| 37 | { label } |
| 38 | </div> |
| 39 | } |
| 40 | |
| 41 | <div className="components-gblocks-range-control--wrapper"> |
| 42 | <div className="components-gblocks-range-control--range"> |
| 43 | <RangeControl |
| 44 | className={ 'gblocks-range-control-range' } |
| 45 | beforeIcon={ beforeIcon } |
| 46 | value={ hasNumericValue( value ) ? parseFloat( value ) : '' } |
| 47 | onChange={ ( newVal ) => onChange( newVal ) } |
| 48 | min={ rangeMin } |
| 49 | max={ rangeMax } |
| 50 | step={ step } |
| 51 | withInputField={ false } |
| 52 | initialPosition={ initialPosition } |
| 53 | disabled={ disabled } |
| 54 | /> |
| 55 | </div> |
| 56 | |
| 57 | <div className="components-gblocks-range-control-input"> |
| 58 | <TextControl |
| 59 | type="number" |
| 60 | placeholder={ '' !== placeholder ? placeholder : '' } |
| 61 | min={ inputMin } |
| 62 | max={ inputMax } |
| 63 | step={ step } |
| 64 | value={ hasNumericValue( value ) ? value : '' } |
| 65 | disabled={ disabled } |
| 66 | onChange={ ( newVal ) => onChange( newVal ) } |
| 67 | onBlur={ () => { |
| 68 | if ( hasNumericValue( value ) ) { |
| 69 | onChange( parseFloat( value ) ); |
| 70 | } |
| 71 | } } |
| 72 | onClick={ ( e ) => { |
| 73 | // Make sure onBlur fires in Firefox. |
| 74 | e.currentTarget.focus(); |
| 75 | } } |
| 76 | /> |
| 77 | </div> |
| 78 | </div> |
| 79 | |
| 80 | { help && |
| 81 | <p className="components-base-control__help"> |
| 82 | { help } |
| 83 | </p> |
| 84 | } |
| 85 | </div> |
| 86 | ); |
| 87 | } |
| 88 | } |
| 89 |