Last active
April 11, 2018 15:42
-
-
Save mainframe98/8370dc2b887c0b081ab16e131762a6e8 to your computer and use it in GitHub Desktop.
PHP NumberInputWidget for OOUI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/demos/demos.php b/demos/demos.php | |
index ab8ba742..6a232d95 100644 | |
--- a/demos/demos.php | |
+++ b/demos/demos.php | |
@@ -157,6 +157,7 @@ | |
<script src="node_modules/jquery/dist/jquery.js"></script> | |
<script src="node_modules/oojs/dist/oojs.jquery.js"></script> | |
<script src="dist/oojs-ui-core.js"></script> | |
+ <script src="dist/oojs-ui-widgets.js"></script> | |
<script src="dist/oojs-ui-<?php echo $theme; ?>.js"></script> | |
<script>window.Demo = {};</script> | |
<script src="classes/ButtonStyleShowcaseWidget.js"></script> | |
diff --git a/demos/pages/widgets.php b/demos/pages/widgets.php | |
index 04a5bb76..5b70d6fd 100644 | |
--- a/demos/pages/widgets.php | |
+++ b/demos/pages/widgets.php | |
@@ -436,6 +436,41 @@ $demoContainer->appendContent( new OOUI\FieldsetLayout( [ | |
'label' => "MultilineTextInputWidget (icon, indicator)\xE2\x80\x8E", | |
'align' => 'top' | |
] | |
+ ), | |
+ new OOUI\FieldLayout( | |
+ new OOUI\NumberInputWidget(), | |
+ [ | |
+ 'label' => "NumberInputWidget\xE2\x80\x8E", | |
+ 'align' => 'top' | |
+ ] | |
+ ), | |
+ new OOUI\FieldLayout( | |
+ new OOUI\NumberInputWidget( [ 'disabled' => true ] ), | |
+ [ | |
+ 'label' => "NumberInputWidget (disabled)\xE2\x80\x8E", | |
+ 'align' => 'top' | |
+ ] | |
+ ), | |
+ new OOUI\FieldLayout( | |
+ new OOUI\NumberInputWidget( [ 'min' => 0, 'max' => 5, 'isInteger' => true ] ), | |
+ [ | |
+ 'label' => "NumberInputWidget (1-5, ints only)\xE2\x80\x8E", | |
+ 'align' => 'top' | |
+ ] | |
+ ), | |
+ new OOUI\FieldLayout( | |
+ new OOUI\NumberInputWidget( [ 'min' => 0, 'max' => 1, 'step' => 0.1, 'pageStep' => 0.25 ] ), | |
+ [ | |
+ 'label' => "NumberInputWidget (0–1, step by .1, page by .25)\xE2\x80\x8E", | |
+ 'align' => 'top' | |
+ ] | |
+ ), | |
+ new OOUI\FieldLayout( | |
+ new OOUI\NumberInputWidget( [ 'showButtons' => false ] ), | |
+ [ | |
+ 'label' => "NumberInputWidget (no buttons)\xE2\x80\x8E", | |
+ 'align' => 'top' | |
+ ] | |
) | |
] | |
] ) ); | |
diff --git a/php/widgets/NumberInputWidget.php b/php/widgets/NumberInputWidget.php | |
new file mode 100644 | |
index 00000000..d1d5e0c9 | |
--- /dev/null | |
+++ b/php/widgets/NumberInputWidget.php | |
@@ -0,0 +1,111 @@ | |
+<?php | |
+ | |
+namespace OOUI; | |
+ | |
+/** | |
+ * Input widget with a number field. | |
+ */ | |
+class NumberInputWidget extends TextInputWidget { | |
+ | |
+ /** | |
+ * @param array $config Configuration options | |
+ * @param int $config['placeholder'] Placeholder number | |
+ * @param bool $config['autofocus'] Ask the browser to focus this widget, using the 'autofocus' | |
+ * HTML attribute (default: false) | |
+ * @param bool $config['readOnly'] Prevent changes (default: false) | |
+ * @param number $config['min'] Minimum input allowed | |
+ * @param number $config['max'] Maximum input allowed | |
+ * @param number $config['step'] Stepping delta (default: any) | |
+ * @param number $config['pageStep'] Stepping delta (page-up and page-down) (default: step * 10) | |
+ * @param number $config['isInteger'] Only integers are allowed | |
+ * @param number $config['showButtons'] Show increment and decrement buttons (default: true) | |
+ * @param bool $config['required'] Mark the field as required. | |
+ * Implies `indicator: 'required'`. Note that `false` & setting `indicator: 'required' | |
+ */ | |
+ public function __construct( array $config = [] ) { | |
+ // Config initialization | |
+ $config = array_merge( [ | |
+ 'step' => 'any', | |
+ 'showButtons' => true | |
+ ], $config ); | |
+ | |
+ $config['type'] = 'number'; | |
+ $config['multiline'] = false; | |
+ | |
+ if ( $config['step'] !== 'any' && !isset( $config['pageStep'] ) | |
+ ) { | |
+ $config['pageStep'] = $config['step'] * 10; | |
+ } | |
+ | |
+ // Parent constructor | |
+ parent::__construct( $config ); | |
+ | |
+ if ( isset( $config['min'] ) ) { | |
+ $this->input->setAttributes( [ 'min' => $config['min'] ] ); | |
+ } | |
+ | |
+ if ( isset( $config['max'] ) ) { | |
+ $this->input->setAttributes( [ 'max' => $config['max'] ] ); | |
+ } | |
+ | |
+ $this->input->setAttributes( [ 'step' => $config['step'] ] ); | |
+ if ( isset( $config['pageStep'] ) ) { | |
+ $this->input->setAttributes( [ 'pageStep' => $config['pageStep'] ] ); | |
+ } | |
+ | |
+ if ( isset( $config['isInteger'] ) ) { | |
+ $this->input->setAttributes( [ 'isInteger' => $config['isInteger'] ] ); | |
+ } | |
+ | |
+ if ( !$config['showButtons'] ) { | |
+ $this->setAttributes( [ 'showButtons' => false ] ); | |
+ } | |
+ | |
+ $this->addClasses( [ | |
+ 'oo-ui-numberInputWidget', | |
+ 'oo-ui-numberInputWidget-php', | |
+ ] ); | |
+ } | |
+ | |
+ public function getConfig( &$config ) { | |
+ $min = $this->input->getAttribute( 'min' ); | |
+ if ( $min !== null ) { | |
+ $config['min'] = $min; | |
+ } | |
+ $max = $this->input->getAttribute( 'max' ); | |
+ if ( $max !== null ) { | |
+ $config['max'] = $max; | |
+ } | |
+ $config['step'] = $this->input->getAttribute( 'step' ); | |
+ $pageStep = $this->input->getAttribute( 'pageStep' ); | |
+ if ( $pageStep !== null ) { | |
+ $config['pageStep'] = $pageStep; | |
+ } | |
+ $isInteger = $this->input->getAttribute( 'isInteger' ); | |
+ if ( $isInteger !== null ) { | |
+ $config['isInteger'] = $isInteger; | |
+ } | |
+ $showButtons = $this->input->getAttribute( 'showButtons' ); | |
+ if ( $showButtons !== null ) { | |
+ $config['showButtons'] = $showButtons; | |
+ } | |
+ return parent::getConfig( $config ); | |
+ } | |
+ | |
+ protected function getInputElement( $config ) { | |
+ $attributes = [ | |
+ 'step' => $config['step'], | |
+ 'type' => 'number', | |
+ ]; | |
+ | |
+ if ( isset( $config['min'] ) ) { | |
+ $attributes['min'] = $config['min']; | |
+ } | |
+ | |
+ if ( isset( $config['max'] ) ) { | |
+ $attributes['max'] = $config['max']; | |
+ } | |
+ | |
+ return ( new Tag( 'input' ) )->setAttributes( $attributes ); | |
+ } | |
+} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment