Skip to content

Incubator.TextField - should float with defaultValue #2344

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

Merged

Conversation

M-i-k-e-l
Copy link
Collaborator

@M-i-k-e-l M-i-k-e-l commented Nov 24, 2022

Description

Incubator.TextField - FloatingPlaceholder should float with defaultValue
Adding shouldFloat and fixing its logic

WOAUILIB-3123

Handling edge cases, using the below PlaygroundScreen:

  1. Switching between TextFields should:
    a. float\un-float if both are empty
    b. float\un-float on an empty one and remain un-floated on one with a value \ defaultValue
  2. Changing defaultValue should affect the floating status
  3. Deleting the defaultValue (to an empty value) should un-float
import React, {Component} from 'react';
import {View, Text, Card, Incubator, Button} from 'react-native-ui-lib';

export default class PlaygroundScreen extends Component {
  state = {
    defaultValue: 'initial value'
  };
  render() {
    return (
      <View bg-grey80 flex padding-20>
        <View marginT-20>
          <Incubator.TextField
            migrate
            marginH-s5
            placeholder={'placeholder'}
            defaultValue={this.state.defaultValue}
            floatingPlaceholder
            floatOnFocus
          />
          <Incubator.TextField migrate marginH-s5 placeholder={'placeholder'} floatingPlaceholder floatOnFocus/>
        </View>
        <Card height={100} center padding-20>
          <Text text50>Playground Screen</Text>
        </Card>
        <View flex center>
          <Button
            marginV-20
            label="Button"
            onPress={() => {
              this.setState({defaultValue: undefined});
            }}
          />
        </View>
      </View>
    );
  }
}

Changelog

Incubator.TextField - FloatingPlaceholder should float with defaultValue

testID
}: FloatingPlaceholderProps) => {
const context = useContext(FieldContext);
const [placeholderOffset, setPlaceholderOffset] = useState({
top: 0,
left: 0
});
const animation = useRef(new Animated.Value(Number((floatOnFocus && context.isFocused) || context.hasValue))).current;

const shouldFloat = useMemo(() => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no need to memoize primitive values (booleans, strings, etc...) 😬

Copy link
Collaborator Author

@M-i-k-e-l M-i-k-e-l Nov 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't that break the useDidUpdate dependencies? Unless I put the dependencies from here, which is a little weird.
I think we tested it once, but I can test again.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you're right but I found a bug :/

@M-i-k-e-l M-i-k-e-l marked this pull request as draft November 24, 2022 10:01
@M-i-k-e-l M-i-k-e-l marked this pull request as ready for review November 24, 2022 15:07
@M-i-k-e-l M-i-k-e-l requested a review from ethanshar November 24, 2022 15:07
@ethanshar ethanshar removed their request for review November 29, 2022 11:44
@@ -86,6 +86,7 @@ export interface FloatingPlaceholderProps {
floatOnFocus?: boolean;
validationMessagePosition?: ValidationMessagePosition;
extraOffset?: number;
defaultValue?: TextInputProps['defaultValue'];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'TextInputProps' are the OLD TextField's prop... why use them in the incubator?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it's RN's props

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok...

@@ -125,6 +125,7 @@ const TextField = (props: InternalTextFieldProps) => {
<View flexG /* flex row */>
{floatingPlaceholder && (
<FloatingPlaceholder
defaultValue={others.defaultValue}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not pass the input's 'value'? Why do we need a separate prop?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a prop from RN which we inherit and the user that opened the bug is using

@@ -18,24 +19,27 @@ const FloatingPlaceholder = ({
floatOnFocus,
validationMessagePosition,
extraOffset = 0,
defaultValue,
Copy link
Collaborator

@Inbal-Tish Inbal-Tish Nov 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The FlotingPlaceholder only needs to know only if to float or not, why doesn't the TextField component controls it with the 'hasValue' passed in context?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're saying that shouldFloat should be coming from somewhere else? Maybe the Presenter like in ValidationMessage?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I think the floating placeholder should only get a boolean and the logic will be in the component that uses it

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@Inbal-Tish Inbal-Tish merged commit 8f2a148 into master Dec 13, 2022
@M-i-k-e-l M-i-k-e-l deleted the fix/incubator-text-field-should-float-with-default-value branch December 13, 2022 08:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants