Skip to content

Commit b7ab94a

Browse files
committed
use onLayout to calc placeholer width for the floating animation
1 parent bf51076 commit b7ab94a

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

src/components/inputs/TextField.js

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,11 @@ export default class TextField extends BaseInput {
172172
...this.state,
173173
value: props.value, // for floatingPlaceholder functionality
174174
floatingPlaceholderState: new Animated.Value(this.shouldFloatPlaceholder(props.value) ? 1 : 0),
175-
showExpandableModal: false
175+
showExpandableModal: false,
176+
floatingPlaceholderTranslate: 0
176177
};
177178

178179
this.generatePropsWarnings(props);
179-
this.calcPlaceholderLayout();
180180
}
181181

182182
UNSAFE_componentWillReceiveProps(nextProps) {
@@ -191,14 +191,10 @@ export default class TextField extends BaseInput {
191191
}
192192
}
193193

194-
calcPlaceholderLayout = async () => {
195-
if (this.shouldFakePlaceholder()) {
196-
const {placeholder} = this.props;
197-
const typography = this.getTypography();
198-
const {width} = await Typography.measureTextSize(placeholder, typography);
199-
const translate = width / 2 - (width * FLOATING_PLACEHOLDER_SCALE) / 2;
200-
this.setState({floatingPlaceholderTranslate: translate / FLOATING_PLACEHOLDER_SCALE});
201-
}
194+
onPlaceholderLayout = (event) => {
195+
const {width} = event.nativeEvent.layout;
196+
const translate = width / 2 - (width * FLOATING_PLACEHOLDER_SCALE) / 2;
197+
this.setState({floatingPlaceholderTranslate: translate / FLOATING_PLACEHOLDER_SCALE});
202198
};
203199

204200
/** Actions */
@@ -364,14 +360,15 @@ export default class TextField extends BaseInput {
364360
const typography = this.getTypography();
365361
const placeholderColor = this.getStateColor(placeholderTextColor || PLACEHOLDER_COLOR_BY_STATE.default);
366362

367-
if (this.shouldFakePlaceholder() && floatingPlaceholderTranslate) {
363+
if (this.shouldFakePlaceholder()) {
368364
return (
369365
<View absF left>
370366
<Animated.Text
371367
pointerEvents="none"
372368
numberOfLines={1}
373369
suppressHighlighting
374370
accessible={false}
371+
onLayout={this.onPlaceholderLayout}
375372
style={[
376373
this.styles.placeholder,
377374
typography,

0 commit comments

Comments
 (0)