Skip to content

Enable TextInputWithTokens to accept leading and trailing visuals #1886

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
merged 6 commits into from
Feb 25, 2022
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/hot-toes-count.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/react': patch
---

Makes it possible to render leading and trailing visuals in TextInputWithTokens just like we do in TextInput
42 changes: 40 additions & 2 deletions docs/content/TextInputWithTokens.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ render(MaxHeightExample)
### With an error validation status

```javascript live noinline
const BasicExample = () => {
const ErrorExample = () => {
const [tokens, setTokens] = React.useState([
{text: 'zero', id: 0},
{text: 'one', id: 1},
Expand All @@ -198,7 +198,45 @@ const BasicExample = () => {
)
}

render(BasicExample)
render(ErrorExample)
```

### With leading and trailing visuals

```javascript live noinline
const LeadingVisualExample = () => {
const [dates, setDates] = React.useState([
{text: '01 Jan', id: 0},
{text: '01 Feb', id: 1},
{text: '01 Mar', id: 2}
])
const [tokens, setTokens] = React.useState([
{text: 'zero', id: 0},
{text: 'one', id: 1},
{text: 'two', id: 2}
])
const onDateRemove = tokenId => {
setDates(dates.filter(token => token.id !== tokenId))
}
const onTokenRemove = tokenId => {
setTokens(tokens.filter(token => token.id !== tokenId))
}

return (
<>
<FormControl>
<FormControl.Label>Dates</FormControl.Label>
<TextInputWithTokens leadingVisual={CalendarIcon} tokens={dates} onTokenRemove={onDateRemove} />
</FormControl>
<FormControl>
<FormControl.Label>Tokens</FormControl.Label>
<TextInputWithTokens trailingVisual={CheckIcon} tokens={tokens} onTokenRemove={onTokenRemove} />
</FormControl>
</>
)
}

render(LeadingVisualExample)
```

## Props
Expand Down
17 changes: 15 additions & 2 deletions src/TextInputWithTokens.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ const overflowCountFontSizeMap: Record<TokenSizeKeys, number> = {
function TextInputWithTokensInnerComponent<TokenComponentType extends AnyReactComponent>(
{
icon: IconComponent,
leadingVisual: LeadingVisual,
trailingVisual: TrailingVisual,
contrast,
className,
block,
Expand Down Expand Up @@ -248,7 +250,8 @@ function TextInputWithTokensInnerComponent<TokenComponentType extends AnyReactCo
className={className}
contrast={contrast}
disabled={disabled}
hasLeadingVisual={Boolean(IconComponent)}
hasLeadingVisual={Boolean(LeadingVisual)}
hasTrailingVisual={Boolean(TrailingVisual)}
theme={theme}
width={widthProp}
minWidth={minWidthProp}
Expand Down Expand Up @@ -283,6 +286,12 @@ function TextInputWithTokensInnerComponent<TokenComponentType extends AnyReactCo
...sxProp
}}
>
{IconComponent && !LeadingVisual && <IconComponent className="TextInput-icon" />}
{LeadingVisual && !IconComponent && (
<span className="TextInput-icon">
{typeof LeadingVisual === 'function' ? <LeadingVisual /> : LeadingVisual}
</span>
)}
<Box
ref={containerRef as RefObject<HTMLDivElement>}
display="flex"
Expand All @@ -300,7 +309,6 @@ function TextInputWithTokensInnerComponent<TokenComponentType extends AnyReactCo
}
}}
>
{IconComponent && <IconComponent className="TextInput-icon" />}
<Box
sx={{
order: 1,
Expand Down Expand Up @@ -344,6 +352,11 @@ function TextInputWithTokensInnerComponent<TokenComponentType extends AnyReactCo
</Text>
) : null}
</Box>
{TrailingVisual && (
<span className="TextInput-icon">
{typeof TrailingVisual === 'function' ? <TrailingVisual /> : TrailingVisual}
</span>
)}
</TextInputWrapper>
)
}
Expand Down
15 changes: 15 additions & 0 deletions src/__tests__/TextInputWithTokens.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'babel-polyfill'
import {TokenSizeKeys, tokenSizes} from '../Token/TokenBase'
import {IssueLabelToken} from '../Token'
import TextInputWithTokens, {TextInputWithTokensProps} from '../TextInputWithTokens'
import {MarkGithubIcon} from '@primer/octicons-react'
expect.extend(toHaveNoViolations)

const mockTokens = [
Expand Down Expand Up @@ -94,6 +95,20 @@ describe('TextInputWithTokens', () => {
).toMatchSnapshot()
})

it('renders a leadingVisual and trailingVisual', () => {
const onRemoveMock = jest.fn()
expect(
render(
<TextInputWithTokens
leadingVisual={MarkGithubIcon}
trailingVisual={MarkGithubIcon}
tokens={mockTokens}
onTokenRemove={onRemoveMock}
/>
)
).toMatchSnapshot()
})

it('focuses the previous token when keying ArrowLeft', () => {
const onRemoveMock = jest.fn()
const {getByLabelText, getByText} = HTMLRender(
Expand Down
Loading