Skip to content
Merged
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
53 changes: 52 additions & 1 deletion packages/core/ui/editable-text-base/index.android.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() };
Expand Down Expand Up @@ -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);
});
});
20 changes: 15 additions & 5 deletions packages/core/ui/editable-text-base/index.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions packages/core/ui/editable-text-base/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Loading