Skip to content

Commit 363ce1f

Browse files
committed
Add type prop to Stepper
1 parent 541be39 commit 363ce1f

File tree

4 files changed

+33
-16
lines changed

4 files changed

+33
-16
lines changed

packages/components/src/components/Stepper/Stepper.stories.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export const Default: Story = {
2929
<Stepper {...args}>
3030
<StepperContainer>
3131
<StepperDecreaseButton />
32-
<StepperInput editable={false} />
32+
<StepperInput />
3333
<StepperIncreaseButton />
3434
</StepperContainer>
3535
</Stepper>

packages/components/src/components/Stepper/__tests__/__snapshots__/index.browser.test.tsx.snap

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,15 @@ exports[`Stepper > should render 1`] = `
3737
</div>
3838
</button>
3939
<div
40-
aria-label="Stepper value"
41-
class="width-0-80px--2 height-0-50px--2 text-align-0-center--2 padding-bottom-0-10px--2 padding-top-0-10px--2 padding-right-0-12px--2 padding-left-0-12px--2 border-radius-0-6px--2 display-0-none-1599573265477977541-2 padding-0-0--3 border-0-none--3 width-0-fit-content--3 height-0-fit-content--3"
42-
data-value="0"
43-
readonly=""
44-
type="number"
45-
value="0"
40+
class=" display-0-inline-block--255 position-0-relative--255 box-sizing-0-border-box-1137980104803605758-255"
4641
>
47-
0
42+
<input
43+
aria-label="Stepper value"
44+
class="width-0-80px--2 height-0-50px--2 text-align-0-center--2 padding-bottom-0-10px--2 padding-top-0-10px--2 padding-right-0-12px--2 padding-left-0-12px--2 border-radius-0-6px--2 display-0-none-1599573265477977541-2 color-0-var(--inputDisabledText,light-dark(#D6D7DE,#373737))-7704306251139592248-1 background-0-var(--inputDisabledBg,light-dark(#F0F0F3,#414244))-14172363753176421546-1 border-0-1px solid var(--border,light-dark(#E4E4E4,#434343))-14172363753176421546-1 color-0-var(--inputDisabledText,light-dark(#D6D7DE,#373737))-14172363753176421546-1 background-0-var(--primaryBg,light-dark(#F4F3FA,#F4F3FA0D))-16231282139879731451-1 border-0-1px solid var(--primary,light-dark(#674DC7,#8163E1))-16231282139879731451-1 outline-0-none-16231282139879731451-1 border-0-1px solid var(--primary,light-dark(#674DC7,#8163E1))-8380715471663921674-1 color-0-var(--inputPlaceholder,light-dark(#A9A8AB,#CBCBCB))-15878565022192187906-1 background-0-var(--inputBg,light-dark(#FFF,#2E2E2E))--1 border-radius-0-8px--1 border-style-0-solid--1 border-width-0-1px--1 padding-bottom-0-12px--1 padding-top-0-12px--1 transition-0-all .1s ease-in-out--1 border-color-0-var(--border,light-dark(#E4E4E4,#434343))--1 padding-left-0-12px--1 padding-right-0-12px--1 "
45+
data-value="0"
46+
type="number"
47+
value="0"
48+
/>
4849
</div>
4950
<button
5051
aria-disabled="false"

packages/components/src/components/Stepper/__tests__/index.browser.test.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,17 @@ describe('Stepper', () => {
5151
expect(input).toHaveAttribute('data-value', '10')
5252
})
5353

54+
it('should not change inner value by clicking the number when type is text', () => {
55+
const { container } = render(
56+
<Stepper type="text">
57+
<StepperInput />
58+
</Stepper>,
59+
)
60+
const input = container.querySelector('[aria-label="Stepper value"]')
61+
expect(input?.nodeName).toBe('DIV')
62+
expect(input).toHaveAttribute('data-value', '0')
63+
})
64+
5465
it('should have disabled decrease button when value is at min', () => {
5566
const { container } = render(
5667
<Stepper>

packages/components/src/components/Stepper/index.tsx

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type StepperContextType = {
1414
setValue: (value: number) => void
1515
min: number
1616
max: number
17+
type: 'input' | 'text'
1718
}
1819

1920
const StepperContext = createContext<StepperContextType | null>(null)
@@ -33,6 +34,7 @@ type StepperProps = {
3334
onValueChange?: (value: number) => void
3435
min?: number
3536
max?: number
37+
type?: 'input' | 'text'
3638
}
3739

3840
function Stepper({
@@ -42,6 +44,7 @@ function Stepper({
4244
onValueChange,
4345
min = 0,
4446
max = 100,
47+
type = 'input',
4548
}: StepperProps) {
4649
const [value, setValue] = useState(defaultValue ?? 0)
4750

@@ -56,7 +59,13 @@ function Stepper({
5659

5760
return (
5861
<StepperContext.Provider
59-
value={{ value: valueProp ?? value, setValue: handleChange, min, max }}
62+
value={{
63+
value: valueProp ?? value,
64+
setValue: handleChange,
65+
min,
66+
max,
67+
type,
68+
}}
6069
>
6170
{children}
6271
</StepperContext.Provider>
@@ -126,20 +135,16 @@ interface StepperInputProps extends ComponentProps<typeof Input> {
126135
editable?: boolean
127136
}
128137

129-
function StepperInput({
130-
editable = true,
131-
className,
132-
...props
133-
}: StepperInputProps) {
134-
const { value, setValue } = useStepper()
138+
function StepperInput({ className, ...props }: StepperInputProps) {
139+
const { value, setValue, type } = useStepper()
135140
const notEditableClass = css({
136141
p: '0',
137142
border: 'none',
138143
w: 'fit-content',
139144
h: 'fit-content',
140145
styleOrder: 3,
141146
})
142-
147+
const editable = type === 'input'
143148
const Comp = editable ? Input : 'div'
144149

145150
return (

0 commit comments

Comments
 (0)