-
Notifications
You must be signed in to change notification settings - Fork 648
use react-is for checking valid element type, update primer/octicons-react #3437
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
Changes from 1 commit
7416720
1923b6f
02c2e02
492bca9
8bcf91a
340f0a5
4955d42
4ebc57c
16880e8
67f9ea3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| import React, {MouseEventHandler, useCallback, useState} from 'react' | ||
| import {isValidElementType} from 'react-is' | ||
| import {ForwardRefComponent as PolymorphicForwardRefComponent} from '../utils/polymorphic' | ||
| import classnames from 'classnames' | ||
|
|
||
|
|
@@ -24,11 +25,11 @@ export type TextInputNonPassthroughProps = { | |
| /** | ||
| * A visual that renders inside the input before the typing area | ||
| */ | ||
| leadingVisual?: string | React.ComponentType<React.PropsWithChildren<{className?: string}>> | ||
| leadingVisual?: React.ElementType | React.ReactNode | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we don't pass props internally, so we don't need to actually care what the props the element can take are
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This might a be novice ts question but I assumed that only passing
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ooo interesting. I didn't realize ReactNode included that, and i'm suprised it does (since ElementType isn't renderable as children directly). I think i'd prefer this since it's a bit more explicit that this could be a Component, but could also be renderable content? |
||
| /** | ||
| * A visual that renders inside the input after the typing area | ||
| */ | ||
| trailingVisual?: string | React.ComponentType<React.PropsWithChildren<{className?: string}>> | ||
| trailingVisual?: React.ElementType | React.ReactNode | ||
| /** | ||
| * A visual that renders inside the input after the typing area | ||
| */ | ||
|
|
@@ -134,7 +135,7 @@ const TextInput = React.forwardRef<HTMLInputElement, TextInputProps>( | |
| showLoadingIndicator={showLeadingLoadingIndicator} | ||
| hasLoadingIndicator={typeof loading === 'boolean'} | ||
| > | ||
| {typeof LeadingVisual === 'function' ? <LeadingVisual /> : LeadingVisual} | ||
| {typeof LeadingVisual !== 'string' && isValidElementType(LeadingVisual) ? <LeadingVisual /> : LeadingVisual} | ||
| </TextInputInnerVisualSlot> | ||
| <UnstyledTextInput | ||
| ref={inputRef} | ||
|
|
@@ -150,7 +151,11 @@ const TextInput = React.forwardRef<HTMLInputElement, TextInputProps>( | |
| showLoadingIndicator={showTrailingLoadingIndicator} | ||
| hasLoadingIndicator={typeof loading === 'boolean'} | ||
| > | ||
| {typeof TrailingVisual === 'function' ? <TrailingVisual /> : TrailingVisual} | ||
| {typeof TrailingVisual !== 'string' && isValidElementType(TrailingVisual) ? ( | ||
| <TrailingVisual /> | ||
| ) : ( | ||
| TrailingVisual | ||
| )} | ||
| </TextInputInnerVisualSlot> | ||
| {trailingAction} | ||
| </TextInputWrapper> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ import {FocusKeys} from '@primer/behaviors' | |
| import {isFocusable} from '@primer/behaviors/utils' | ||
| import {omit} from '@styled-system/props' | ||
| import React, {FocusEventHandler, KeyboardEventHandler, MouseEventHandler, RefObject, useRef, useState} from 'react' | ||
| import {isValidElementType} from 'react-is' | ||
| import Box from '../Box' | ||
| import {useRefObjectAsForwardedRef} from '../hooks/useRefObjectAsForwardedRef' | ||
| import {useFocusZone} from '../hooks/useFocusZone' | ||
|
|
@@ -296,7 +297,7 @@ function TextInputWithTokensInnerComponent<TokenComponentType extends AnyReactCo | |
| visualPosition="leading" | ||
| showLoadingIndicator={showLeadingLoadingIndicator} | ||
| > | ||
| {typeof LeadingVisual === 'function' ? <LeadingVisual /> : LeadingVisual} | ||
| {typeof LeadingVisual !== 'string' && isValidElementType(LeadingVisual) ? <LeadingVisual /> : LeadingVisual} | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we have to check for We may want to consider whether accepting raw element types makes sense longterm (I don't think there's a good reason to accept a raw Component instead of just React.ReactNode). <Component leadingVisual={SearchIcon} /> // imo this shouldn't be allowed
<Component leadingVisual={<SearchIcon />} /> // imo this should be the waybut that might be a later breaking change at this point?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mattcosta7 I think you're dead-on and I bet we could look to introduce this behavior in v36 and deprecate the raw element type and remove support for it in v37
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh great point! Thanks for the note as well, that was one of my questions until I came to that point! ➕ 1 for using ReactNodes instead of raw components |
||
| </TextInputInnerVisualSlot> | ||
| <Box | ||
| ref={containerRef as RefObject<HTMLDivElement>} | ||
|
|
@@ -362,7 +363,7 @@ function TextInputWithTokensInnerComponent<TokenComponentType extends AnyReactCo | |
| visualPosition="trailing" | ||
| showLoadingIndicator={showTrailingLoadingIndicator} | ||
| > | ||
| {typeof TrailingVisual === 'function' ? <TrailingVisual /> : TrailingVisual} | ||
| {typeof TrailingVisual !== 'string' && isValidElementType(TrailingVisual) ? <TrailingVisual /> : TrailingVisual} | ||
| </TextInputInnerVisualSlot> | ||
| </TextInputWrapper> | ||
| ) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
going to loosen this and types so they can be managed better in dotcom