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

Rule to detect JSX text not in a Text component #186

Open
jer-sen opened this issue Jan 31, 2018 · 13 comments
Open

Rule to detect JSX text not in a Text component #186

jer-sen opened this issue Jan 31, 2018 · 13 comments

Comments

@jer-sen
Copy link

jer-sen commented Jan 31, 2018

In some components such as View, text can not be placed directly inside (it must be wrapped with a Text component).

Could you add a rule for this ? Ideally list of standard component forbidding direct use of text could be completed with a rule configuration.

@Intellicode
Copy link
Owner

Excellent idea! I'll see if I can have a go at it

@AleksandrZhukov
Copy link
Contributor

AleksandrZhukov commented Oct 2, 2018

Added this new rule #201

@lefloh
Copy link

lefloh commented Oct 2, 2018

Awesome! I was just searching for a rule like that.
One question: Will this also prevent the following example:

const foo = ''
...
foo && <Component />

React Native does not evaluate foo as falsy but tries to render an empty string here.

@AleksandrZhukov
Copy link
Contributor

AleksandrZhukov commented Oct 2, 2018

no, this one is the only case that can't be handled or I didn't understand how 😄
just use !!foo or foo.lenght > 0 for your case

@lefloh
Copy link

lefloh commented Oct 2, 2018

Yes, I know. Problem is to not forget about that while writing code...
Would be great if eslint could remind you ;)
Thanks anyways!

@dantman
Copy link

dantman commented Oct 12, 2018

Needs the ability to configure it. i.e. So you can add 'CustomStyledText' to valid text nodes if you use a Text wrapper to provide standardized text styles through your app.

Sadly even with that this quickly falls apart for me because I use styled.Text a lot to provide locally styled Text components. And there are a few outliers like react-native-paper providing a <Button> that accepts text.

@AleksandrZhukov
Copy link
Contributor

AleksandrZhukov commented Oct 12, 2018

can you provide a code example?

yeah, seems this rule doesn't support styled components, but need to investigate

@dantman
Copy link

dantman commented Oct 12, 2018

Styled example:

const ContentText = styled.Text`
	${material.body1Object}
	color: ${colors.primaryText};
`;

Example of a "standard" wrapper:
https://github.com/material-native/material-native/blob/master/src/MaterialText.js

<MaterialText body1>Foo</MaterialText>

Or a port of MUI's Typography: https://material-ui.com/style/typography/

@AleksandrZhukov
Copy link
Contributor

@dantman Fixed here #205

PS. Quite funny that you call your library as standard 😄

@dantman
Copy link

dantman commented Oct 13, 2018

Standardized within an app, anyone can make their own with the typography rules for their app. That's just an example of an old one I made to wrap Text with Material Design typography classes.

@asmeikal
Copy link

#205 only works with styled components if the styled Text component is called StyledText, does not work if several StyledText are defined, unless each of them is skipped...

@the0ffh
Copy link

the0ffh commented Jun 9, 2020

@dantman Fixed here #205

PS. Quite funny that you call your library as standard 😄

Quite funny that you call it a fix 😄

@pagkt2
Copy link

pagkt2 commented Jan 12, 2022

Awesome! I was just searching for a rule like that. One question: Will this also prevent the following example:

const foo = ''
...
foo && <Component />

React Native does not evaluate foo as falsy but tries to render an empty string here.

I am using eslint-plugin-jsx-expressions to manage this issue. Cf. jsx-eslint/eslint-plugin-react#2073 (comment)

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

No branches or pull requests

8 participants