HTMLComboboxField.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. /**
  3. * A combo box field.
  4. *
  5. * You can think of it as a dropdown select with the ability to add custom options,
  6. * or as a text field with input suggestions (autocompletion).
  7. *
  8. * When JavaScript is not supported or enabled, it uses HTML5 `<datalist>` element.
  9. *
  10. * Besides the parameters recognized by HTMLTextField, the following are
  11. * recognized:
  12. * options-messages - As for HTMLSelectField
  13. * options - As for HTMLSelectField
  14. * options-message - As for HTMLSelectField
  15. */
  16. class HTMLComboboxField extends HTMLTextField {
  17. // FIXME Ewww, this shouldn't be adding any attributes not requested in $list :(
  18. public function getAttributes( array $list ) {
  19. $attribs = [
  20. 'type' => 'text',
  21. 'list' => $this->mName . '-datalist',
  22. ] + parent::getAttributes( $list );
  23. return $attribs;
  24. }
  25. public function getInputHTML( $value ) {
  26. $datalist = new XmlSelect( false, $this->mName . '-datalist' );
  27. $datalist->setTagName( 'datalist' );
  28. $datalist->addOptions( $this->getOptions() );
  29. return parent::getInputHTML( $value ) . $datalist->getHTML();
  30. }
  31. public function getInputOOUI( $value ) {
  32. $disabled = false;
  33. $allowedParams = [ 'tabindex' ];
  34. $attribs = OOUI\Element::configFromHtmlAttributes(
  35. $this->getAttributes( $allowedParams )
  36. );
  37. if ( $this->mClass !== '' ) {
  38. $attribs['classes'] = [ $this->mClass ];
  39. }
  40. if ( !empty( $this->mParams['disabled'] ) ) {
  41. $disabled = true;
  42. }
  43. return new OOUI\ComboBoxInputWidget( [
  44. 'name' => $this->mName,
  45. 'id' => $this->mID,
  46. 'options' => $this->getOptionsOOUI(),
  47. 'value' => strval( $value ),
  48. 'disabled' => $disabled,
  49. ] + $attribs );
  50. }
  51. protected function shouldInfuseOOUI() {
  52. return true;
  53. }
  54. }