Skip to content

Commit

Permalink
[MP-672] Support endAdornment properly in NumberInput (#4645)
Browse files Browse the repository at this point in the history
* Support endAdornment in NumberInput

* Support endAdornment in NumberInput

* Remove unnecessary condition
  • Loading branch information
diogolessa authored Feb 5, 2025
1 parent be59400 commit c1a7cbb
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/perfect-dolls-march.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@toptal/picasso-number-input': minor
---

- support custom `endAdornment` in NumberInput
12 changes: 11 additions & 1 deletion packages/base/NumberInput/src/NumberInput/NumberInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export const NumberInput = forwardRef<HTMLInputElement, Props>(
status,
onResetClick,
enableReset,
endAdornment: customEndAdornment = null,
width,
icon,
size,
Expand All @@ -63,7 +64,7 @@ export const NumberInput = forwardRef<HTMLInputElement, Props>(
useRef<HTMLInputElement>(null)
)

const endAdornment = hideControls ? null : (
const defaultEndAdornment = (
<NumberInputEndAdornment
step={step}
min={min}
Expand All @@ -75,6 +76,15 @@ export const NumberInput = forwardRef<HTMLInputElement, Props>(
/>
)

const endAdornment = hideControls ? (
customEndAdornment
) : (
<>
{customEndAdornment}
{defaultEndAdornment}
</>
)

const startAdornment = icon ? (
<InputAdornment position='start' disablePointerEvents>
{icon}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type { ChangeEventHandler } from 'react'
import React, { useState } from 'react'
import { NumberInput, Container, Typography } from '@toptal/picasso'

const WithEndAdornmentExample = () => {
const [value, setValue] = useState('1')

const handleChange: ChangeEventHandler<HTMLInputElement> = event => {
setValue(event.target.value)
}

return (
<Container>
<NumberInput
value={value}
onChange={handleChange}
step='5'
max='100'
min='-100'
endAdornment={<Typography color='dark-grey'>$/hr</Typography>}
/>
</Container>
)
}

export default WithEndAdornmentExample
7 changes: 6 additions & 1 deletion packages/base/NumberInput/src/NumberInput/story/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import PicassoBook from '~/.storybook/components/PicassoBook'
const page = PicassoBook.section('Forms').createPage(
'NumberInput',
`Input component for numbers
${PicassoBook.createSourceLink(__filename)}
`
)
Expand Down Expand Up @@ -35,6 +35,11 @@ page
'With Icon',
'base/NumberInput'
)
.addExample(
'NumberInput/story/WithEndAdornment.example.tsx',
'With End Adornment',
'base/NumberInput'
)
.addExample(
'NumberInput/story/Sizes.example.tsx',
'Sizes',
Expand Down
33 changes: 33 additions & 0 deletions packages/base/NumberInput/src/NumberInput/test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ const NumberInputRenderer = (
value={value}
onChange={handleChange}
status={props.status}
endAdornment={props.endAdornment}
hideControls={props.hideControls}
testIds={props.testIds}
/>
)
Expand Down Expand Up @@ -150,4 +152,35 @@ describe('NumberInput', () => {
expect(validIcon).not.toBeVisible()
})
})

describe('end adornment', () => {
describe('when endAdornment is passed', () => {
it('renders endAdornment with control buttons', () => {
const testProps: NumberInputProps = {
value: '10',
endAdornment: <div data-testid='custom-end-adornment' />,
}

const { getByTestId, getAllByRole } = renderNumberInput(testProps)

expect(getByTestId('custom-end-adornment')).toBeVisible()
expect(getAllByRole('button')).toHaveLength(2)
})
})

describe('when endAdornment is passed and controls are hidden', () => {
it('renders endAdornment without control buttons', () => {
const testProps: NumberInputProps = {
value: '10',
endAdornment: <div data-testid='custom-end-adornment' />,
hideControls: true,
}

const { getByTestId, queryAllByRole } = renderNumberInput(testProps)

expect(getByTestId('custom-end-adornment')).toBeVisible()
expect(queryAllByRole('button')).toHaveLength(0)
})
})
})
})

0 comments on commit c1a7cbb

Please sign in to comment.