Skip to content

feat: Pass to render a custom clear button #3546

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
31 changes: 20 additions & 11 deletions src/components/textField/ClearButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const TIMING_CONFIG = {
easing: Easing.bezier(0.33, 1, 0.68, 1)
};

const ClearButton = ({testID, onClear, onChangeText, clearButtonStyle}: ClearButtonProps) => {
const ClearButton = ({testID, onClear, onChangeText, clearButtonStyle,ClearButtonContentComponent}: ClearButtonProps) => {
const {hasValue} = useContext(FieldContext);
const animatedValue = useSharedValue(hasValue ? VISIBLE_POSITION : NON_VISIBLE_POSITION);
const animatedOpacity = useSharedValue(hasValue ? 1 : 0);
Expand Down Expand Up @@ -49,16 +49,25 @@ const ClearButton = ({testID, onClear, onChangeText, clearButtonStyle}: ClearBut

return (
<View reanimated style={style} testID={`${testID}.container`}>
<Button
link
iconSource={Assets.icons.xFlat}
iconStyle={styles.clearIcon}
onPress={clear}
hitSlop={hitSlop}
accessible={hasValue}
accessibilityLabel={'clear'}
testID={testID}
/>

{ClearButtonContentComponent?
<ClearButtonContentComponent
onPress={clear}
hitSlop={hitSlop}
accessible={hasValue}
accessibilityLabel={'clear'}
testID={testID}/> :
<Button
link
iconSource={Assets.icons.xFlat}
iconStyle={styles.clearIcon}
onPress={clear}
hitSlop={hitSlop}
accessible={hasValue}
accessibilityLabel={'clear'}
testID={testID}
/>}

</View>
);
};
Expand Down
3 changes: 3 additions & 0 deletions src/components/textField/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ const TextField = (props: InternalTextFieldProps) => {
topTrailingAccessory,
bottomAccessory,
showClearButton,
ClearButtonContentComponent,
onClear,
// Validation
enableErrors, // TODO: rename to enableValidation
Expand Down Expand Up @@ -210,11 +211,13 @@ const TextField = (props: InternalTextFieldProps) => {
</View>
)}
{showClearButton && (

<ClearButton
onClear={onClear}
testID={`${props.testID}.clearButton`}
onChangeText={onChangeText}
clearButtonStyle={clearButtonStyle}
ClearButtonContentComponent={ClearButtonContentComponent}
/>
)}
{trailingAccessory}
Expand Down
7 changes: 7 additions & 0 deletions src/components/textField/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ export interface ClearButtonProps extends Pick<TextInputProps, 'testID' | 'onCha
* The style of the clear button
*/
clearButtonStyle?: StyleProp<ViewStyle>;
/** Pass to render a custom clear button */
ClearButtonContentComponent?: ReactElement;
}

export interface LabelProps extends MandatoryIndication, Pick<ValidationMessageProps, 'enableErrors'> {
Expand Down Expand Up @@ -226,6 +228,11 @@ export type TextFieldProps = MarginModifiers &
* Should show a clear button when there is a value
*/
showClearButton?: boolean;

/**
* Pass to render a custom clear button
*/
ClearButtonContentComponent?: ReactElement;
/**
* Text to display under the input
*/
Expand Down