Skip to content
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

migrated TextInputLabel/index.native.js to functional component #23941

Merged
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
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';

hayata-suenaga marked this conversation as resolved.
Show resolved Hide resolved
export default TextInputLabel;
Loading