Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions apps/example/src/TextInputScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export function TextInputScreen() {
const [message, setMessage] = useState(initialMessage)
const [status, setStatus] = useState('Ready')
const [selection, setSelection] = useState<TextRange | undefined>()
const [passwordHidden, setPasswordHidden] = useState(true)
const messageRef = useRef<TextInputRef>(null)
const focusGesture = useMemo(() => {
const gesture = new TapGestureRecognizer()
Expand All @@ -26,6 +27,11 @@ export function TextInputScreen() {
}
return gesture
}, [])
const passwordVisibilityGesture = useMemo(() => {
const gesture = new TapGestureRecognizer()
gesture.onTap = () => setPasswordHidden((hidden) => !hidden)
return gesture
}, [])
const blurGesture = useMemo(() => {
const gesture = new TapGestureRecognizer()
gesture.onTap = () => {
Expand Down Expand Up @@ -129,6 +135,17 @@ export function TextInputScreen() {
style={{ background: 0xf0f0f0ff, cornerRadius: 6 }}
/>
<Text>{`Name: ${name}`}</Text>
<Text layout={{ marginTop: 16 }}>Password input</Text>
<TextInput
secureTextEntry={passwordHidden}
placeholder="Password"
layout={{ padding: 10, marginTop: 8, marginBottom: 8 }}
style={{ background: 0xf0f0f0ff, cornerRadius: 6 }}
/>
<Text
layout={{ padding: 10, alignSelf: 'flex-start' }}
gestureRecognizers={[passwordVisibilityGesture]}
>{passwordHidden ? 'Show password' : 'Hide password'}</Text>
<Text layout={{ marginTop: 16 }}>Input wraps its content</Text>
<View layout={{ flexDirection: 'row', marginTop: 8, marginBottom: 8 }}>
<TextInput
Expand Down
15 changes: 15 additions & 0 deletions docs/content/docs/components/text-input.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,27 @@ export function NoteCode() {

The decision runs synchronously. The keyboard can finish composing a character before the field validates the committed edit. For a programmatic reset, call `setText('')` on a `TextInputRef`. Programmatic changes bypass the edit callbacks, so update any separate React preview in the same action.

## Enter a password

Set `secureTextEntry` to obscure typed characters. The keyboard offers password autofill and turns off suggestions. Multiline inputs ignore it.

Toggle the prop to show or hide the password. The field keeps its text and selection. On iOS, typing into a field that was just obscured again replaces its text, as in other iOS apps.

```tsx
import { TextInput } from 'nitro-ui-renderer'

export function PasswordField() {
return <TextInput secureTextEntry placeholder="Password" />
}
```

## Props

| Prop | Type | Use |
| --- | --- | --- |
| `defaultValue` | `string` | Set the initial text. Defaults to an empty string. |
| `placeholder` | `string` | Hint shown while the field is empty. |
| `secureTextEntry` | `boolean` | Obscure typed characters for passwords. Defaults to `false`. Ignored when `multiline`. |
| `multiline` | `boolean` | Allow line breaks and vertical scrolling. Defaults to `false`. Changing modes requires a new React key. |
| `onBeforeTextChange` | `(edit: TextInputEdit) => string` | Return the accepted text synchronously. Composing keyboards call this when an edit is confirmed. |
| `onTextChanged` | `(change: TextInputChange) => void` | Receive text changes, including provisional keyboard input. |
Expand Down
9 changes: 9 additions & 0 deletions packages/nitro-ui-renderer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,15 @@ observes the actual result; calling `setState` there does not decide the edit.
/>;
```

Set `secureTextEntry` for password fields. The input obscures typed characters
and the keyboard offers password autofill. Multiline inputs ignore it. Toggling
it keeps the text and selection. As in other iOS apps, typing into a field that
was just obscured again replaces its text.

```tsx
<TextInput secureTextEntry placeholder="Password" />;
```

`defaultValue` sets the initial text; later changes are ignored. Use
`ref.setText(text)` for programmatic changes. These calls bypass the decision
callback and do not emit edit events. Keep decision callbacks synchronous and do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,21 @@ final class HybridTextInput<InputView: UIView & NativeTextInput>: HybridTextInpu
}
}

var secureTextEntry: Bool? {
get {
guard let field = view as? UITextField else { return nil }
return field.isSecureTextEntry
}
set {
// UITextView has no secure entry, so multiline inputs ignore it.
guard let field = view as? UITextField else { return }
let secure = newValue ?? false
guard field.isSecureTextEntry != secure else { return }
field.isSecureTextEntry = secure
invalidateLayout()
}
}

var isFocused: Bool {
return view.isFirstResponder
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,19 @@ private constructor(
invalidateLayout()
}

override var secureTextEntry: Boolean? = null
set(value) {
if (field == value) return
field = value
// Multiline EditText has no password variation, matching iOS.
if (multiline) return
val variation =
if (value == true) InputType.TYPE_TEXT_VARIATION_PASSWORD
else InputType.TYPE_TEXT_VARIATION_NORMAL
editor.replaceInputType((editor.inputType and InputType.TYPE_MASK_VARIATION.inv()) or variation)
invalidateLayout()
}

override val isFocused: Boolean
get() = editor.hasFocus()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,24 @@ internal class NativeTextInputView(context: Context) : EditText(context) {
return decideEdit?.invoke(current, proposed) ?: proposed
}

/**
* Changes the input type without treating it as an edit. EditText reapplies its
* text, which would move the caret, and switches password types to monospace.
*/
fun replaceInputType(type: Int) {
val previousStart = selectionStart
val previousEnd = selectionEnd
val previousTypeface = typeface
applyingText = true
try {
inputType = type
typeface = previousTypeface
if (previousStart >= 0 && previousEnd >= 0) setSelection(previousStart, previousEnd)
} finally {
applyingText = false
}
}

fun replaceText(value: String, caret: Int? = null) {
val previousStart = selectionStart
val previousEnd = selectionEnd
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export type TextInputHostProps = Omit<SharedViewProps, 'children'> & {
initialText: string
/** The text displayed while empty. */
placeholder?: string
/** Whether typed characters are obscured. */
secureTextEntry?: boolean
}

/** Internal host for the React TextInput component. */
Expand All @@ -23,4 +25,7 @@ export const TextInputHost = registerView('TextInput', (props) => {
placeholder(input, value) {
input.placeholder = value
},
secureTextEntry(input, value) {
input.secureTextEntry = value
},
})
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ export type TextInputProps = Omit<SharedViewProps, 'children'> & RefAttributes<T
multiline?: boolean
/** The text displayed while empty. */
placeholder?: string
/**
* Obscures typed characters and offers password autofill, for password fields.
* Ignored when multiline. @default false
*/
secureTextEntry?: boolean
/** Reports native text or marked-text changes, including provisional keyboard input. */
onTextChanged?: (change: TextInputChange) => void
/** Reports native selection or marked-text changes, including changes without a text edit. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export declare class TextInput extends View {
readonly markedTextRange?: TextRange
/** The text displayed while empty. Defaults to undefined. */
placeholder?: string
/** Whether typed characters are obscured, for passwords. Ignored when multiline. Defaults to false. */
secureTextEntry?: boolean
/** Whether this input is the first responder. */
readonly isFocused: boolean

Expand Down