Skip to content

Commit a647cbb

Browse files
authored
TextField - ClearButton - fix right margin (#3482)
* TextField - ClearButton - fix right margin * Add ClearButtonProps and move style to preset
1 parent 1ffc372 commit a647cbb

File tree

5 files changed

+27
-12
lines changed

5 files changed

+27
-12
lines changed

src/components/textField/ClearButton.tsx

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import {StyleSheet} from 'react-native';
33
import {useAnimatedStyle, useSharedValue, withTiming, Easing} from 'react-native-reanimated';
44
import {useDidUpdate} from '../../hooks';
55
import Assets from '../../assets';
6-
import {Spacings, Colors} from '../../style';
6+
import {Colors} from '../../style';
77
import View from '../view';
88
import Button from '../button';
99
import FieldContext from './FieldContext';
10-
import {TextFieldProps} from './types';
10+
import {ClearButtonProps} from './types';
1111

1212
const hitSlop = {top: 20, bottom: 20, left: 20, right: 20};
1313
const NON_VISIBLE_POSITION = 18;
@@ -17,7 +17,7 @@ const TIMING_CONFIG = {
1717
easing: Easing.bezier(0.33, 1, 0.68, 1)
1818
};
1919

20-
const ClearButton = ({testID, onClear, onChangeText}: Pick<TextFieldProps, 'onClear' | 'testID' | 'onChangeText'>) => {
20+
const ClearButton = ({testID, onClear, onChangeText, clearButtonStyle}: ClearButtonProps) => {
2121
const {hasValue} = useContext(FieldContext);
2222
const animatedValue = useSharedValue(hasValue ? VISIBLE_POSITION : NON_VISIBLE_POSITION);
2323
const animatedOpacity = useSharedValue(hasValue ? 1 : 0);
@@ -29,7 +29,7 @@ const ClearButton = ({testID, onClear, onChangeText}: Pick<TextFieldProps, 'onCl
2929
};
3030
});
3131

32-
const style = useMemo(() => [styles.container, animatedStyle], [animatedStyle]);
32+
const style = useMemo(() => [clearButtonStyle, animatedStyle], [clearButtonStyle, animatedStyle]);
3333

3434
const animate = useCallback(() => {
3535
const toValue = hasValue ? VISIBLE_POSITION : NON_VISIBLE_POSITION;
@@ -64,9 +64,6 @@ const ClearButton = ({testID, onClear, onChangeText}: Pick<TextFieldProps, 'onCl
6464
};
6565

6666
const styles = StyleSheet.create({
67-
container: {
68-
marginHorizontal: Spacings.s3
69-
},
7067
clearIcon: {
7168
tintColor: Colors.$textNeutralLight
7269
}

src/components/textField/index.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ const TextField = (props: InternalTextFieldProps) => {
9191
centered,
9292
readonly = false,
9393
showMandatoryIndication,
94+
clearButtonStyle,
9495
...others
9596
} = usePreset(props);
9697

@@ -208,7 +209,12 @@ const TextField = (props: InternalTextFieldProps) => {
208209
</View>
209210
)}
210211
{showClearButton && (
211-
<ClearButton onClear={onClear} testID={`${props.testID}.clearButton`} onChangeText={onChangeText}/>
212+
<ClearButton
213+
onClear={onClear}
214+
testID={`${props.testID}.clearButton`}
215+
onChangeText={onChangeText}
216+
clearButtonStyle={clearButtonStyle}
217+
/>
212218
)}
213219
{trailingAccessory}
214220
{/* </View> */}

src/components/textField/presets/outline.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {BorderRadiuses, Colors, Spacings} from '../../../style';
33
import underline from './underline';
44

55
const styles = StyleSheet.create({
6+
clearButtonStyle: {marginLeft: Spacings.s3},
67
field: {
78
borderWidth: 1,
89
borderColor: Colors.$outlineDisabled,
@@ -14,5 +15,6 @@ const styles = StyleSheet.create({
1415

1516
export default {
1617
...underline,
18+
clearButtonStyle: styles.clearButtonStyle,
1719
fieldStyle: styles.field
1820
};

src/components/textField/presets/underline.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const styles = StyleSheet.create({
2626
lineHeight: undefined,
2727
height: Typography?.text70?.lineHeight
2828
},
29+
clearButtonStyle: {marginHorizontal: Spacings.s3},
2930
floatingPlaceholder: {...Typography.text70}
3031
});
3132

@@ -37,5 +38,6 @@ export default {
3738
labelColor: colorByState,
3839
fieldStyle: styles.field,
3940
style: styles.input,
41+
clearButtonStyle: styles.clearButtonStyle,
4042
floatingPlaceholderStyle: styles.floatingPlaceholder
4143
};

src/components/textField/types.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,17 @@ export interface MandatoryIndication {
6969
showMandatoryIndication?: boolean;
7070
}
7171

72+
export interface ClearButtonProps extends Pick<TextInputProps, 'testID' | 'onChangeText'> {
73+
/**
74+
* On clear button callback
75+
*/
76+
onClear?: () => void;
77+
/**
78+
* The style of the clear button
79+
*/
80+
clearButtonStyle?: StyleProp<ViewStyle>;
81+
}
82+
7283
export interface LabelProps extends MandatoryIndication, Pick<ValidationMessageProps, 'enableErrors'> {
7384
/**
7485
* Field label
@@ -190,6 +201,7 @@ export type TextFieldProps = MarginModifiers &
190201
LabelProps &
191202
Omit<FloatingPlaceholderProps, 'testID'> &
192203
MandatoryIndication &
204+
Omit<ClearButtonProps, 'testID' | 'onChangeText'> &
193205
// We're declaring these props explicitly here for react-docgen (which can't read hooks)
194206
// FieldStateProps &
195207
ValidationMessageProps &
@@ -214,10 +226,6 @@ export type TextFieldProps = MarginModifiers &
214226
* Should show a clear button when there is a value
215227
*/
216228
showClearButton?: boolean;
217-
/**
218-
* On clear button callback
219-
*/
220-
onClear?: () => void;
221229
/**
222230
* Text to display under the input
223231
*/

0 commit comments

Comments
 (0)