Skip to content

Commit bfead25

Browse files
committed
1. Add leading and trailing visual
2. Change grid logic 3. Update stories and docs
1 parent 27ee540 commit bfead25

File tree

7 files changed

+553
-299
lines changed

7 files changed

+553
-299
lines changed

src/TextInput.tsx

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import TextInputWrapper from './_TextInputWrapper'
66

77
type NonPassthroughProps = {
88
className?: string
9-
/** @deprecated Use `leadingIcon` or `trailingIcon` prop instead */
9+
/** @deprecated Use `leadingVisual` or `trailingVisual` prop instead */
1010
icon?: React.ComponentType<{className?: string}>
11-
leadingIcon?: React.ComponentType<{className?: string}>
12-
trailingIcon?: React.ComponentType<{className?: string}>
11+
leadingVisual?: string | React.ComponentType<{className?: string}>
12+
trailingVisual?: string | React.ComponentType<{className?: string}>
1313
} & Pick<
1414
ComponentProps<typeof TextInputWrapper>,
1515
'block' | 'contrast' | 'disabled' | 'sx' | 'width' | 'maxWidth' | 'minWidth' | 'variant' | 'size'
@@ -23,8 +23,8 @@ const TextInput = React.forwardRef<HTMLInputElement, TextInputInternalProps>(
2323
(
2424
{
2525
icon: IconComponent,
26-
leadingIcon: LeadingIconComponent,
27-
trailingIcon: TrailingIconComponent,
26+
leadingVisual: LeadingVisual,
27+
trailingVisual: TrailingVisual,
2828
block,
2929
className,
3030
contrast,
@@ -52,18 +52,25 @@ const TextInput = React.forwardRef<HTMLInputElement, TextInputInternalProps>(
5252
validationStatus={validationStatus}
5353
contrast={contrast}
5454
disabled={disabled}
55-
hasIcon={!!IconComponent || !!(LeadingIconComponent || TrailingIconComponent)}
5655
sx={sxProp}
5756
size={sizeProp}
5857
width={widthProp}
5958
minWidth={minWidthProp}
6059
maxWidth={maxWidthProp}
6160
variant={variantProp}
6261
>
63-
{IconComponent && <IconComponent className="TextInput-leading-icon" />}
64-
{LeadingIconComponent && <LeadingIconComponent className="TextInput-leading-icon" />}
62+
{IconComponent && <IconComponent className="TextInput-icon" />}
63+
{LeadingVisual && (
64+
<span data-component="leadingVisual">
65+
{typeof LeadingVisual === 'function' ? <LeadingVisual /> : LeadingVisual}
66+
</span>
67+
)}
6568
<UnstyledTextInput ref={ref} disabled={disabled} {...inputProps} data-component="input" />
66-
{TrailingIconComponent && <TrailingIconComponent className="TextInput-trailing-icon" />}
69+
{TrailingVisual && (
70+
<span data-component="leadingVisual">
71+
{typeof TrailingVisual === 'function' ? <TrailingVisual /> : TrailingVisual}
72+
</span>
73+
)}
6774
</TextInputWrapper>
6875
)
6976
}

src/_TextInputWrapper.tsx

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,24 +69,27 @@ const TextInputWrapper = styled.span<StyledWrapperProps>`
6969
cursor: text;
7070
padding: 6px 12px;
7171
display: grid;
72-
grid-template-areas: 'leadingIcon input trailingIcon';
72+
grid-template-columns: auto 1fr auto;
73+
justify-items: end;
7374
& > :not(:last-child) {
7475
margin-right: ${get('space.2')};
7576
}
76-
[data-component='input'] {
77-
grid-area: input;
77+
78+
[data-component=' leadingVisual '] {
79+
align-self: center;
80+
color: ${get('colors.fg.muted')};
7881
}
7982
80-
.TextInput-leading-icon {
83+
[data-component=' trailingVisual '] {
8184
align-self: center;
8285
color: ${get('colors.fg.muted')};
83-
grid-area: leadingIcon;
8486
}
8587
86-
.TextInput-trailing-icon {
88+
.TextInput-icon {
8789
align-self: center;
8890
color: ${get('colors.fg.muted')};
89-
grid-area: trailingIcon;
91+
margin: 0 ${get('space.2')};
92+
flex-shrink: 0;
9093
}
9194
9295
&:focus-within {
@@ -134,7 +137,7 @@ const TextInputWrapper = styled.span<StyledWrapperProps>`
134137
width: 100%;
135138
`}
136139
137-
// Ensures inputs don't zoom on mobile but are body-font size on desktop
140+
// Ensures inputs don' t zoom on mobile but are body-font size on desktop
138141
@media (min-width: ${get('breakpoints.1')}) {
139142
font-size: ${get('fontSizes.1')};
140143
}

src/__tests__/TextInput.test.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ describe('TextInput', () => {
5353
expect(render(<TextInput name="zipcode" placeholder={'560076'} />)).toMatchSnapshot()
5454
})
5555

56-
it('renders leadingIcon', () => {
57-
expect(render(<TextInput name="search" placeholder={'Search'} leadingIcon={SearchIcon} />)).toMatchSnapshot()
56+
it('renders leadingVisual', () => {
57+
expect(render(<TextInput name="search" placeholder={'Search'} leadingVisual={SearchIcon} />)).toMatchSnapshot()
5858
})
5959

60-
it('renders trailingIcon', () => {
61-
expect(render(<TextInput name="search" placeholder={'Search'} trailingIcon={SearchIcon} />)).toMatchSnapshot()
60+
it('renders trailingVisual', () => {
61+
expect(render(<TextInput name="search" placeholder={'Search'} trailingVisual={SearchIcon} />)).toMatchSnapshot()
6262
})
6363

6464
it('should call onChange prop with input value', () => {

0 commit comments

Comments
 (0)