SelectWithInputWidget.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace MediaWiki\Widget;
  3. use OOUI\DropdownInputWidget;
  4. use OOUI\TextInputWidget;
  5. /**
  6. * Select and input widget.
  7. *
  8. * @copyright 2011-2017 MediaWiki Widgets Team and others; see AUTHORS.txt
  9. * @license MIT
  10. */
  11. class SelectWithInputWidget extends \OOUI\Widget {
  12. /** @var array */
  13. protected $config;
  14. /** @var TextInputWidget */
  15. protected $textinput;
  16. /** @var DropdownInputWidget */
  17. protected $dropdowninput;
  18. /**
  19. * A version of the SelectWithInputWidget, with `or` set to true.
  20. *
  21. * @param array $config Configuration options
  22. * - array $config['textinput'] Configuration for the TextInputWidget
  23. * - array $config['dropdowninput'] Configuration for the DropdownInputWidget
  24. * - bool $config['or'] Configuration for whether the widget is dropdown AND input
  25. * or dropdown OR input
  26. * - bool $config['required'] Configuration for whether the widget is a required input.
  27. */
  28. public function __construct( array $config = [] ) {
  29. // Configuration initialization
  30. $config = array_merge(
  31. [
  32. 'textinput' => [],
  33. 'dropdowninput' => [],
  34. 'or' => false,
  35. 'required' => false,
  36. ],
  37. $config
  38. );
  39. if ( isset( $config['disabled'] ) && $config['disabled'] ) {
  40. $config['textinput']['disabled'] = true;
  41. $config['dropdowninput']['disabled'] = true;
  42. }
  43. $config['textinput']['required'] = $config['or'] ? false : $config['required'];
  44. $config['dropdowninput']['required'] = $config['required'];
  45. parent::__construct( $config );
  46. // Properties
  47. $this->config = $config;
  48. $this->textinput = new TextInputWidget( $config['textinput'] );
  49. $this->dropdowninput = new DropdownInputWidget( $config['dropdowninput'] );
  50. // Initialization
  51. $this
  52. ->addClasses( [ 'mw-widget-selectWithInputWidget' ] )
  53. ->appendContent( $this->dropdowninput, $this->textinput );
  54. }
  55. protected function getJavaScriptClassName() {
  56. return 'mw.widgets.SelectWithInputWidget';
  57. }
  58. public function getConfig( &$config ) {
  59. $config['textinput'] = $this->config['textinput'];
  60. $config['dropdowninput'] = $this->config['dropdowninput'];
  61. $config['dropdowninput']['dropdown']['$overlay'] = true;
  62. $config['or'] = $this->config['or'];
  63. $config['required'] = $this->config['required'];
  64. return parent::getConfig( $config );
  65. }
  66. }