Form.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import telc from 'tui-lib/util/telchars'
  2. import {FocusElement} from 'tui-lib/ui/primitives'
  3. export default class Form extends FocusElement {
  4. constructor() {
  5. super()
  6. this.inputs = []
  7. this.curIndex = 0
  8. this.captureTab = true
  9. }
  10. addInput(input, asChild = true, opts = {}) {
  11. // Adds the given input as a child element and pushes it to the input
  12. // list. If the optional argument asChild is false, it won't add the
  13. // input element as a child of the form.
  14. this.inputs.push(input)
  15. if (asChild) {
  16. this.addChild(input, this.children.length, opts)
  17. }
  18. }
  19. removeInput(input, asChild = true, opts = {}) {
  20. // Removes the given input from the form's input list. If the optional
  21. // argument asChild is false, it won't try to removeChild the input.
  22. if (this.inputs.includes(input)) {
  23. this.inputs.splice(this.inputs.indexOf(input), 1)
  24. if (asChild) {
  25. this.removeChild(input, opts)
  26. }
  27. }
  28. }
  29. selectInput(input) {
  30. if (this.inputs.includes(input)) {
  31. this.curIndex = this.inputs.indexOf(input)
  32. this.updateSelectedElement()
  33. }
  34. }
  35. keyPressed(keyBuf) {
  36. // Don't do anything if captureTab is set to false. This is handy for
  37. // nested forms.
  38. if (!this.captureTab) {
  39. return
  40. }
  41. if (telc.isTab(keyBuf) || telc.isBackTab(keyBuf)) {
  42. // No inputs to tab through, so do nothing.
  43. if (this.inputs.length < 2) {
  44. return
  45. }
  46. if (telc.isTab(keyBuf)) {
  47. this.nextInput()
  48. } else {
  49. this.previousInput()
  50. }
  51. return false
  52. }
  53. }
  54. get selectable() {
  55. return this.inputs.some(inp => inp.selectable)
  56. }
  57. updateSelectedElement() {
  58. if (this.root.select && this.inputs.length) {
  59. if (this.curIndex > this.inputs.length - 1) {
  60. this.curIndex = this.inputs.length - 1
  61. }
  62. this.root.select(this.inputs[this.curIndex], {fromForm: true})
  63. }
  64. }
  65. previousInput() {
  66. if (this.inputs.length === 0) {
  67. return
  68. }
  69. do {
  70. this.curIndex = (this.curIndex - 1)
  71. if (this.curIndex < 0) {
  72. this.curIndex = (this.inputs.length - 1)
  73. }
  74. } while (!this.inputs[this.curIndex].selectable)
  75. this.updateSelectedElement()
  76. }
  77. nextInput() {
  78. if (this.inputs.length === 0) {
  79. return
  80. }
  81. do {
  82. this.curIndex = (this.curIndex + 1) % this.inputs.length
  83. } while (!this.inputs[this.curIndex].selectable)
  84. this.updateSelectedElement()
  85. }
  86. firstInput(selectForm = true) {
  87. if (this.inputs.length === 0) {
  88. return
  89. }
  90. this.curIndex = 0
  91. if (!this.inputs[this.curIndex].selectable) {
  92. this.nextInput()
  93. }
  94. if (selectForm || (
  95. this.root.isChildOrSelfSelected && this.root.isChildOrSelfSelected(this)
  96. )) {
  97. this.updateSelectedElement()
  98. }
  99. }
  100. lastInput(selectForm = true) {
  101. if (this.inputs.length === 0) {
  102. return
  103. }
  104. this.curIndex = this.inputs.length - 1
  105. if (!this.inputs[this.curIndex].selectable) {
  106. this.previousInput()
  107. }
  108. if (selectForm || (
  109. this.root.isChildOrSelfSelected && this.root.isChildOrSelfSelected(this)
  110. )) {
  111. this.updateSelectedElement()
  112. }
  113. }
  114. selected() {
  115. if (this.root.selectedElement === this) {
  116. this.updateSelectedElement()
  117. }
  118. }
  119. get curIndex() { return this.getDep('curIndex') }
  120. set curIndex(v) { return this.setDep('curIndex', v) }
  121. }