Skip to content

Commit

Permalink
Merge pull request #23941 from devanshu-josh/issue-16207-migrate-Text…
Browse files Browse the repository at this point in the history
…InputLabel/index.native.js

migrated TextInputLabel/index.native.js to functional component
  • Loading branch information
Hayata Suenaga authored Aug 7, 2023
2 parents 2b49a5f + 30d1e23 commit 11dda38
Showing 1 changed file with 32 additions and 39 deletions.
71 changes: 32 additions & 39 deletions src/components/TextInput/TextInputLabel/index.native.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,44 @@
import React, {PureComponent} from 'react';
import React, {useState} from 'react';
import {Animated} from 'react-native';
import styles from '../../../styles/styles';
import * as TextInputLabelPropTypes from './TextInputLabelPropTypes';
import * as styleConst from '../styleConst';

class TextInputLabel extends PureComponent {
constructor(props) {
super(props);
this.state = {
width: 0,
};
}
function TextInputLabel(props) {
const [width, setWidth] = useState(0);

render() {
return (
<Animated.Text
allowFontScaling={false}
onLayout={({nativeEvent}) => {
this.setState({width: nativeEvent.layout.width});
}}
style={[
styles.textInputLabel,
styles.textInputLabelTransformation(
this.props.labelTranslateY,
this.props.labelScale.interpolate({
inputRange: [styleConst.ACTIVE_LABEL_SCALE, styleConst.INACTIVE_LABEL_SCALE],
outputRange: [-(this.state.width - this.state.width * styleConst.ACTIVE_LABEL_SCALE) / 2, 0],
}),
this.props.labelScale,
),

// If the label is active but the width is not ready yet, the above translateX value will be 0,
// making the label sits at the top center instead of the top left of the input. To solve it
// move the label by a percentage value with left style as translateX doesn't support percentage value.
this.state.width === 0 &&
this.props.isLabelActive && {
left: `${-((1 - styleConst.ACTIVE_LABEL_SCALE) * 100) / 2}%`,
},
]}
>
{this.props.label}
</Animated.Text>
);
}
return (
<Animated.Text
allowFontScaling={false}
onLayout={({nativeEvent}) => {
setWidth(nativeEvent.layout.width);
}}
style={[
styles.textInputLabel,
styles.textInputLabelTransformation(
props.labelTranslateY,
props.labelScale.interpolate({
inputRange: [styleConst.ACTIVE_LABEL_SCALE, styleConst.INACTIVE_LABEL_SCALE],
outputRange: [-(width - width * styleConst.ACTIVE_LABEL_SCALE) / 2, 0],
}),
props.labelScale,
),
// If the label is active but the width is not ready yet, the above translateX value will be 0,
// making the label sits at the top center instead of the top left of the input. To solve it
// move the label by a percentage value with left style as translateX doesn't support percentage value.
width === 0 &&
props.isLabelActive && {
left: `${-((1 - styleConst.ACTIVE_LABEL_SCALE) * 100) / 2}%`,
},
]}
>
{props.label}
</Animated.Text>
);
}

TextInputLabel.propTypes = TextInputLabelPropTypes.propTypes;
TextInputLabel.defaultProps = TextInputLabelPropTypes.defaultProps;
TextInputLabel.displayName = 'TextInputLabel';

export default TextInputLabel;

0 comments on commit 11dda38

Please sign in to comment.