Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Migrate TextInput to TypeScript #1028

Merged
merged 2 commits into from
Feb 8, 2021
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions .changeset/stupid-seals-explode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@primer/components": patch
---

Migrate `TextInput` to TypeScript
1 change: 1 addition & 0 deletions @types/@styled-system/props/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module '@styled-system/props'
72 changes: 45 additions & 27 deletions src/TextInput.js → src/TextInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import classnames from 'classnames'
import systemPropTypes from '@styled-system/prop-types'
import {omit, pick} from '@styled-system/props'
import styled, {css} from 'styled-components'
import {variant, width, minWidth, maxWidth} from 'styled-system'
import {COMMON, get} from './constants'
import {variant, width, minWidth, maxWidth, MaxWidthProps, WidthProps, MinWidthProps} from 'styled-system'
import {COMMON, get, SystemCommonProps} from './constants'
import theme from './theme'
import sx from './sx'
import sx, {SxProp} from './sx'
import {ComponentProps} from './utils/types'

const sizeVariants = variant({
variants: {
Expand All @@ -26,28 +27,6 @@ const sizeVariants = variant({
}
})

// using forwardRef is important so that other components (ex. SelectMenu) can autofocus the input
const TextInput = React.forwardRef(({icon: IconComponent, className, block, disabled, sx, ...rest}, ref) => {
// this class is necessary to style FilterSearch, plz no touchy!
const wrapperClasses = classnames(className, 'TextInput-wrapper')
const wrapperProps = pick(rest)
const inputProps = omit(rest)
return (
<Wrapper
className={wrapperClasses}
hasIcon={!!IconComponent}
block={block}
theme={theme}
disabled={disabled}
sx={sx}
{...wrapperProps}
>
{IconComponent && <IconComponent className="TextInput-icon" />}
<Input ref={ref} disabled={disabled} {...inputProps} />
</Wrapper>
)
})

const Input = styled.input`
border: 0;
font-size: inherit;
Expand All @@ -60,7 +39,18 @@ const Input = styled.input`
}
`

const Wrapper = styled.span`
type StyledWrapperProps = {
disabled?: boolean
hasIcon?: boolean
block?: boolean
variant?: 'small' | 'large'
} & SystemCommonProps &
WidthProps &
MinWidthProps &
MaxWidthProps &
SxProp

const Wrapper = styled.span<StyledWrapperProps>`
display: inline-flex;
align-items: stretch;
min-height: 34px;
Expand Down Expand Up @@ -126,14 +116,41 @@ const Wrapper = styled.span`
${sx};
`

type TextInputInternalProps = {icon?: React.ComponentType<{className?: string}>} & ComponentProps<typeof Wrapper> &
ComponentProps<typeof Input>

// using forwardRef is important so that other components (ex. SelectMenu) can autofocus the input
const TextInput = React.forwardRef<HTMLInputElement, TextInputInternalProps>(
({icon: IconComponent, className, block, disabled, sx, ...rest}, ref) => {
// this class is necessary to style FilterSearch, plz no touchy!
const wrapperClasses = classnames(className, 'TextInput-wrapper')
const wrapperProps = pick(rest)
const inputProps = omit(rest)
return (
<Wrapper
className={wrapperClasses}
hasIcon={!!IconComponent}
block={block}
theme={theme}
disabled={disabled}
sx={sx}
{...wrapperProps}
>
{IconComponent && <IconComponent className="TextInput-icon" />}
<Input ref={ref} disabled={disabled} {...inputProps} />
</Wrapper>
)
}
)

TextInput.defaultProps = {
theme,
type: 'text'
}

TextInput.propTypes = {
block: PropTypes.bool,
icon: PropTypes.elementType,
icon: PropTypes.any,
maxWidth: systemPropTypes.layout.maxWidth,
minWidth: systemPropTypes.layout.minWidth,
variant: PropTypes.oneOf(['small', 'large']),
Expand All @@ -144,4 +161,5 @@ TextInput.propTypes = {

TextInput.displayName = 'TextInput'

export type TextInputProps = ComponentProps<typeof TextInput>
export default TextInput
Comment on lines 163 to 165
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm assuming wrapper props doesn't need it's own export because it's something that's used internally here that would not need to be exposed outside this component?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup 👍 No need to expose implementation details

File renamed without changes.