From ebd04620e054fb125e0f70afb5f45e2c1ab79c3c Mon Sep 17 00:00:00 2001 From: b Date: Thu, 26 Nov 2026 09:50:54 +0100 Subject: [PATCH] fix(android): keep read-only text selectable `editable=false` turned focus, click and long click off even with `selectable=true`, so read-only text could not be selected. The `selectable` setter also targeted `nativeViewProtected`, which is the wrapping layout of a view like the one of ui-textview, not the EditText. Add `selectable` to the typings. Co-Authored-By: Claude Sonnet 5.5 --- .../editable-text-base/index.android.spec.ts | 53 ++++++++++++++++++- .../ui/editable-text-base/index.android.ts | 20 +++++-- .../core/ui/editable-text-base/index.d.ts | 8 +++ 3 files changed, 75 insertions(+), 6 deletions(-) diff --git a/packages/core/ui/editable-text-base/index.android.spec.ts b/packages/core/ui/editable-text-base/index.android.spec.ts index d42108c813..243ef38678 100644 --- a/packages/core/ui/editable-text-base/index.android.spec.ts +++ b/packages/core/ui/editable-text-base/index.android.spec.ts @@ -12,7 +12,7 @@ vi.hoisted(() => { }); import { EditableTextBase } from './index.android'; -import { keyboardTypeProperty } from './editable-text-base-common'; +import { editableProperty, keyboardTypeProperty, selectableProperty } from './editable-text-base-common'; function inputTypeFor(value: unknown): number { const target = { _setInputType: vi.fn() }; @@ -42,3 +42,54 @@ describe('EditableTextBase keyboardType (Android)', () => { expect(inputTypeFor('integer')).toBe(InputType.TYPE_CLASS_NUMBER); }); }); + +describe('EditableTextBase editable and selectable (Android)', () => { + function applyEditable(editable: boolean, selectable: boolean) { + const nativeView = { + getKeyListener: vi.fn(), + setKeyListener: vi.fn(), + setFocusable: vi.fn(), + setFocusableInTouchMode: vi.fn(), + setLongClickable: vi.fn(), + setClickable: vi.fn(), + setShowSoftInputOnFocus: vi.fn(), + setTextIsSelectable: vi.fn(), + }; + const target = Object.create(EditableTextBase.prototype); + Object.defineProperties(target, { + nativeTextViewProtected: { value: nativeView }, + editable: { value: editable }, + selectable: { value: selectable }, + }); + (EditableTextBase.prototype as any)[editableProperty.setNative].call(target, editable); + return { target, nativeView }; + } + + it('keeps read-only text selectable', () => { + const { nativeView } = applyEditable(false, true); + expect(nativeView.setKeyListener).toHaveBeenCalledWith(null); + expect(nativeView.setFocusable).toHaveBeenCalledWith(true); + expect(nativeView.setLongClickable).toHaveBeenCalledWith(true); + expect(nativeView.setShowSoftInputOnFocus).toHaveBeenCalledWith(false); + }); + + it('makes read-only text not selectable when asked', () => { + const { nativeView } = applyEditable(false, false); + expect(nativeView.setFocusable).toHaveBeenCalledWith(false); + expect(nativeView.setLongClickable).toHaveBeenCalledWith(false); + }); + + it('shows the keyboard of editable text', () => { + const { nativeView } = applyEditable(true, false); + expect(nativeView.setFocusable).toHaveBeenCalledWith(true); + expect(nativeView.setShowSoftInputOnFocus).toHaveBeenCalledWith(true); + }); + + it('keeps the editable and selectable state when selectable changes', () => { + const { target, nativeView } = applyEditable(false, true); + nativeView.setFocusable.mockClear(); + (EditableTextBase.prototype as any)[selectableProperty.setNative].call(target, true); + expect(nativeView.setTextIsSelectable).toHaveBeenCalledWith(true); + expect(nativeView.setFocusable).toHaveBeenLastCalledWith(true); + }); +}); diff --git a/packages/core/ui/editable-text-base/index.android.ts b/packages/core/ui/editable-text-base/index.android.ts index 9614762e8d..b1864ae012 100644 --- a/packages/core/ui/editable-text-base/index.android.ts +++ b/packages/core/ui/editable-text-base/index.android.ts @@ -373,17 +373,27 @@ export abstract class EditableTextBase extends EditableTextBaseCommon { } nativeView.setKeyListener(null); } - nativeView.setFocusable(value); - nativeView.setFocusableInTouchMode(value); - nativeView.setLongClickable(value); - nativeView.setClickable(value); + nativeView.setShowSoftInputOnFocus(value); + this.applyInteractivity(); } [selectableProperty.getDefault](): boolean { return true; } [selectableProperty.setNative](value: boolean) { - this.nativeViewProtected.setTextIsSelectable(value); + this.nativeTextViewProtected.setTextIsSelectable(value); + // it resets what `editable` set + this.applyInteractivity(); + } + + /** Read-only text stays focusable and long clickable while it is selectable */ + private applyInteractivity() { + const nativeView = this.nativeTextViewProtected; + const interactive = this.editable || this.selectable; + nativeView.setFocusable(interactive); + nativeView.setFocusableInTouchMode(interactive); + nativeView.setLongClickable(interactive); + nativeView.setClickable(interactive); } [autocapitalizationTypeProperty.getDefault](): 'none' | 'words' | 'sentences' | 'allcharacters' | string { diff --git a/packages/core/ui/editable-text-base/index.d.ts b/packages/core/ui/editable-text-base/index.d.ts index a699d0f35d..fad8a6da0c 100644 --- a/packages/core/ui/editable-text-base/index.d.ts +++ b/packages/core/ui/editable-text-base/index.d.ts @@ -72,6 +72,14 @@ export class EditableTextBase extends TextBase { */ editable: boolean; + /** + * Gets or sets whether the text can be selected, also when it is not `editable`. + * + * @default true + * @nsProperty + */ + selectable: boolean; + /** * Enables or disables autocorrection. *