File: NumberInputWidget.php

package info (click to toggle)
mediawiki 1%3A1.39.13-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 320,416 kB
  • sloc: php: 815,516; javascript: 601,264; sql: 11,218; python: 4,863; xml: 3,080; sh: 990; ruby: 82; makefile: 78
file content (87 lines) | stat: -rw-r--r-- 2,443 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php

namespace OOUI;

/**
 * Input widget with a number field.
 */
class NumberInputWidget extends TextInputWidget {

	/** @var float|null */
	protected $buttonStep;
	/** @var float|null */
	protected $pageStep;
	/** @var bool|null */
	protected $showButtons;

	/**
	 * @param array $config Configuration options
	 *      - float $config['min'] Minimum input allowed
	 *      - float $config['max'] Maximum input allowed
	 *      - float|null $config['step'] If specified, the field only accepts values that are
	 *          multiples of this. (default: null)
	 *      - float $config['buttonStep'] Delta when using the buttons or Up/Down arrow keys.
	 *          Defaults to `step` if specified, otherwise `1`.
	 *      - float $config['pageStep'] Delta when using the Page-up/Page-down keys.
	 *          Defaults to 10 times `buttonStep`.
	 *      - bool $config['showButtons'] Show increment and decrement buttons (default: true)
	 */
	public function __construct( array $config = [] ) {
		$config['type'] = 'number';
		$config['multiline'] = false;

		// 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'] ?? 'any' ] );

		if ( isset( $config['buttonStep'] ) ) {
			$this->buttonStep = $config['buttonStep'];
		}
		if ( isset( $config['pageStep'] ) ) {
			$this->pageStep = $config['pageStep'];
		}
		if ( isset( $config['showButtons'] ) ) {
			$this->showButtons = $config['showButtons'];
		}

		$this->addClasses( [
			'oo-ui-numberInputWidget',
			'oo-ui-numberInputWidget-php',
		] );
	}

	/** @inheritDoc */
	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;
		}
		$step = $this->input->getAttribute( 'step' );
		if ( $step !== 'any' ) {
			$config['step'] = $step;
		}
		if ( $this->pageStep !== null ) {
			$config['pageStep'] = $this->pageStep;
		}
		if ( $this->buttonStep !== null ) {
			$config['buttonStep'] = $this->buttonStep;
		}
		if ( $this->showButtons !== null ) {
			$config['showButtons'] = $this->showButtons;
		}
		return parent::getConfig( $config );
	}
}