input.mdx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. ---
  2. title: Input
  3. description: Text input field with cursor and focus states
  4. order: 3
  5. skill:
  6. entry: true
  7. intents: [input, form, editing, focus]
  8. ---
  9. # Input
  10. Text input field with cursor, placeholder text, and focus states. Focus the input to receive keyboard input.
  11. ## Basic usage
  12. ### Renderable api
  13. ```typescript
  14. import { InputRenderable, InputRenderableEvents, createCliRenderer } from "@opentui/core"
  15. const renderer = await createCliRenderer()
  16. const input = new InputRenderable(renderer, {
  17. id: "name-input",
  18. width: 25,
  19. placeholder: "Enter your name...",
  20. })
  21. input.on(InputRenderableEvents.CHANGE, (value) => {
  22. console.log("Input value:", value)
  23. })
  24. input.focus()
  25. renderer.root.add(input)
  26. ```
  27. ### Construct api
  28. ```typescript
  29. import { Input, createCliRenderer } from "@opentui/core"
  30. const renderer = await createCliRenderer()
  31. const input = Input({
  32. placeholder: "Enter your name...",
  33. width: 25,
  34. })
  35. input.focus()
  36. renderer.root.add(input)
  37. ```
  38. ## Focus states
  39. The input changes appearance when focused:
  40. ```typescript
  41. const input = new InputRenderable(renderer, {
  42. id: "styled-input",
  43. width: 30,
  44. placeholder: "Type here...",
  45. backgroundColor: "#1a1a1a",
  46. focusedBackgroundColor: "#2a2a2a",
  47. textColor: "#FFFFFF",
  48. cursorColor: "#00FF00",
  49. })
  50. ```
  51. ## Events
  52. ### Input event
  53. Fires on every keystroke as the value changes:
  54. ```typescript
  55. import { InputRenderableEvents } from "@opentui/core"
  56. input.on(InputRenderableEvents.INPUT, (value: string) => {
  57. console.log("Current value:", value)
  58. })
  59. ```
  60. ### Change event
  61. Fires when the input loses focus (blur) or when you press Enter, but only if the value changed since focus:
  62. ```typescript
  63. input.on(InputRenderableEvents.CHANGE, (value: string) => {
  64. console.log("Value committed:", value)
  65. })
  66. ```
  67. ### Enter event
  68. Fires when the user presses Enter/Return:
  69. ```typescript
  70. input.on(InputRenderableEvents.ENTER, (value: string) => {
  71. console.log("Submitted value:", value)
  72. })
  73. ```
  74. ### Getting the current value
  75. ```typescript
  76. const currentValue = input.value
  77. ```
  78. ### Setting the value
  79. ```typescript
  80. input.value = "New value"
  81. ```
  82. ## Properties
  83. | Property | Type | Default | Description |
  84. | ------------------------ | -------------------------------------- | ------------ | ---------------------------- |
  85. | `width` | `number` | - | Input field width |
  86. | `value` | `string` | `""` | Initial text value |
  87. | `placeholder` | `string` | `""` | Placeholder text when empty |
  88. | `maxLength` | `number` | `1000` | Maximum number of characters |
  89. | `backgroundColor` | `string \| RGBA` | - | Background when unfocused |
  90. | `focusedBackgroundColor` | `string \| RGBA` | - | Background when focused |
  91. | `textColor` | `string \| RGBA` | - | Text color |
  92. | `cursorColor` | `string \| RGBA` | - | Cursor color |
  93. | `position` | `"static" \| "relative" \| "absolute"` | `"relative"` | Positioning mode |
  94. ## Example: Login form
  95. ```typescript
  96. import { Box, Text, Input, delegate, createCliRenderer } from "@opentui/core"
  97. const renderer = await createCliRenderer()
  98. function LabeledInput(props: { id: string; label: string; placeholder: string }) {
  99. return delegate(
  100. { focus: `${props.id}-input` },
  101. Box(
  102. { flexDirection: "row", marginBottom: 1 },
  103. Text({ content: props.label.padEnd(12), fg: "#888888" }),
  104. Input({
  105. id: `${props.id}-input`,
  106. placeholder: props.placeholder,
  107. width: 20,
  108. backgroundColor: "#222",
  109. focusedBackgroundColor: "#333",
  110. textColor: "#FFF",
  111. cursorColor: "#0F0",
  112. }),
  113. ),
  114. )
  115. }
  116. const usernameInput = LabeledInput({
  117. id: "username",
  118. label: "Username:",
  119. placeholder: "Enter username",
  120. })
  121. const passwordInput = LabeledInput({
  122. id: "password",
  123. label: "Password:",
  124. placeholder: "Enter password",
  125. })
  126. const form = Box(
  127. {
  128. width: 40,
  129. borderStyle: "rounded",
  130. title: "Login",
  131. padding: 1,
  132. },
  133. usernameInput,
  134. passwordInput,
  135. )
  136. // Focus the username input
  137. usernameInput.focus()
  138. renderer.root.add(form)
  139. ```
  140. ## Tab navigation
  141. Add tab navigation between inputs:
  142. ```typescript
  143. const inputs = [usernameInput, passwordInput]
  144. let focusIndex = 0
  145. renderer.keyInput.on("keypress", (key) => {
  146. if (key.name === "tab") {
  147. focusIndex = (focusIndex + 1) % inputs.length
  148. inputs[focusIndex].focus()
  149. }
  150. })
  151. ```