Skip to content

Commit 3be20a3

Browse files
feat(search-bar): clear button color support (#10903)
1 parent 5086b7c commit 3be20a3

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

packages/core/ui/search-bar/index.android.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Font } from '../styling/font';
2-
import { SearchBarBase, textProperty, hintProperty, textFieldHintColorProperty, textFieldBackgroundColorProperty } from './search-bar-common';
2+
import { SearchBarBase, textProperty, hintProperty, textFieldHintColorProperty, textFieldBackgroundColorProperty, clearButtonColorProperty } from './search-bar-common';
33
import { isUserInteractionEnabledProperty, isEnabledProperty } from '../core/view';
44
import { ad } from '../../utils';
55
import { Color } from '../../color';
@@ -33,6 +33,7 @@ function initializeNativeClasses(): void {
3333
constructor(private owner: SearchBar) {
3434
super();
3535

36+
// @ts-ignore
3637
return global.__native(this);
3738
}
3839

@@ -70,6 +71,7 @@ function initializeNativeClasses(): void {
7071
constructor(private owner: SearchBar) {
7172
super();
7273

74+
// @ts-ignore
7375
return global.__native(this);
7476
}
7577

@@ -272,6 +274,25 @@ export class SearchBar extends SearchBarBase {
272274
const color = value instanceof Color ? value.android : value;
273275
textView.setHintTextColor(color);
274276
}
277+
[clearButtonColorProperty.setNative](value: Color) {
278+
if (!this.nativeViewProtected || !value) {
279+
return;
280+
}
281+
282+
try {
283+
// The close (clear) button inside the SearchView can be found by its resource ID
284+
const closeButtonId = this.nativeViewProtected.getContext().getResources().getIdentifier('android:id/search_close_btn', null, null);
285+
const closeButton = this.nativeViewProtected.findViewById(closeButtonId) as android.widget.ImageView;
286+
287+
const color = value instanceof Color ? value.android : new Color(value).android;
288+
289+
if (closeButton) {
290+
closeButton.setColorFilter(color);
291+
}
292+
} catch (err) {
293+
console.log('Error setting clear button color:', err);
294+
}
295+
}
275296

276297
private _getTextView(): android.widget.TextView {
277298
if (!this._searchTextView) {

packages/core/ui/search-bar/index.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ export class SearchBar extends View {
6161
*/
6262
textFieldHintColor: Color;
6363

64+
/**
65+
* Gets or sets the Clear Button color of the SearchBar component.
66+
*
67+
* @nsProperty
68+
*/
69+
clearButtonColor: Color | string;
70+
6471
/**
6572
* Adds a listener for the specified event name.
6673
*

packages/core/ui/search-bar/index.ios.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Font } from '../styling/font';
2-
import { SearchBarBase, textProperty, hintProperty, textFieldHintColorProperty, textFieldBackgroundColorProperty } from './search-bar-common';
2+
import { SearchBarBase, textProperty, hintProperty, textFieldHintColorProperty, textFieldBackgroundColorProperty, clearButtonColorProperty } from './search-bar-common';
33
import { isEnabledProperty } from '../core/view';
44
import { Color } from '../../color';
55
import { colorProperty, backgroundColorProperty, backgroundInternalProperty, fontInternalProperty } from '../styling/style-properties';
@@ -220,4 +220,13 @@ export class SearchBar extends SearchBarBase {
220220
const attributedPlaceholder = NSAttributedString.alloc().initWithStringAttributes(stringValue, attributes);
221221
this._getTextField().attributedPlaceholder = attributedPlaceholder;
222222
}
223+
[clearButtonColorProperty.setNative](value: Color | UIColor) {
224+
const textField = this._getTextField();
225+
if (!textField) return;
226+
// Check if clear button is available in the text field
227+
const clearButton = textField.valueForKey('clearButton');
228+
if (!clearButton) return;
229+
230+
clearButton.tintColor = value instanceof Color ? value.ios : value;
231+
}
223232
}

packages/core/ui/search-bar/search-bar-common.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export abstract class SearchBarBase extends View implements SearchBarDefinition
1212
public hint: string;
1313
public textFieldBackgroundColor: Color;
1414
public textFieldHintColor: Color;
15+
public clearButtonColor: Color | string;
1516

1617
public abstract dismissSoftInput();
1718
}
@@ -44,3 +45,11 @@ export const textFieldBackgroundColorProperty = new Property<SearchBarBase, Colo
4445
valueConverter: (v) => new Color(v),
4546
});
4647
textFieldBackgroundColorProperty.register(SearchBarBase);
48+
49+
// --- Added property for clear button color ---
50+
export const clearButtonColorProperty = new Property<SearchBarBase, Color>({
51+
name: 'clearButtonColor',
52+
equalityComparer: Color.equals,
53+
valueConverter: (v) => new Color(v),
54+
});
55+
clearButtonColorProperty.register(SearchBarBase);

0 commit comments

Comments
 (0)