Skip to content

Commit 328b979

Browse files
update type
1 parent 80d6f8f commit 328b979

File tree

3 files changed

+75
-62
lines changed

3 files changed

+75
-62
lines changed

packages/react/src/ProgressBar/ProgressBar.features.stories.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react'
22
import type {Meta} from '@storybook/react'
3-
import {ProgressBar, Text} from '..'
3+
import {ProgressBar} from '..'
44

55
export default {
66
title: 'Components/ProgressBar/Features',

packages/react/src/ProgressBar/ProgressBar.stories.tsx

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React, {useEffect} from 'react'
22
import type {Meta} from '@storybook/react'
3-
import {ProgressBar, type ProgressBarProps} from '..'
3+
import {ProgressBar} from '..'
4+
import type {ProgressBarBaseProps} from './ProgressBar'
45

56
const sectionColorsDefault = [
67
'success.emphasis',
@@ -18,7 +19,7 @@ export default {
1819

1920
export const Default = () => <ProgressBar aria-label="Upload test.png" />
2021

21-
export const Playground = ({sections, ...args}: ProgressBarProps & {sections: number}) => {
22+
export const Playground = ({sections, ...args}: ProgressBarBaseProps & {sections: number}) => {
2223
const [sectionColors, setSectionColors] = React.useState(sectionColorsDefault)
2324

2425
useEffect(() => {
@@ -31,9 +32,9 @@ export const Playground = ({sections, ...args}: ProgressBarProps & {sections: nu
3132
return <ProgressBar {...args} sx={args.inline ? {width: '100px'} : {}} aria-label="Upload test.png" />
3233
} else {
3334
return (
34-
<ProgressBar aria-label="Upload test.png">
35+
<ProgressBar>
3536
{[...Array(sections).keys()].map(i => (
36-
<ProgressBar.Item key={i} progress={100 / sections} bg={sectionColors[i]} />
37+
<ProgressBar.Item key={i} progress={100 / sections} bg={sectionColors[i]} aria-label="Upload test.png" />
3738
))}
3839
</ProgressBar>
3940
)

packages/react/src/ProgressBar/ProgressBar.tsx

+69-57
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ const ProgressContainer = toggleStyledComponent(
7373
)
7474

7575
export type ProgressBarItems = React.HTMLAttributes<HTMLSpanElement> & {
76-
'aria-label'?: string
76+
'aria-label': string
7777
className?: string
7878
} & ProgressProp &
7979
SxProp
@@ -127,63 +127,75 @@ export const Item = forwardRef<HTMLSpanElement, ProgressBarItems>(
127127

128128
Item.displayName = 'ProgressBar.Item'
129129

130-
export type ProgressBarProps = React.HTMLAttributes<HTMLSpanElement> & {
131-
bg?: string
132-
className?: string
133-
} & StyledProgressContainerProps &
134-
ProgressProp
135-
136-
export const ProgressBar = forwardRef<HTMLSpanElement, ProgressBarProps>(
137-
(
138-
{
139-
animated,
140-
progress,
141-
bg = 'success.emphasis',
142-
barSize = 'default',
143-
children,
144-
'aria-label': ariaLabel,
145-
'aria-valuenow': ariaValueNow,
146-
'aria-valuetext': ariaValueText,
147-
className,
148-
...rest
149-
}: ProgressBarProps,
150-
forwardRef,
151-
) => {
152-
if (children && progress) {
153-
throw new Error('You should pass `progress` or children, not both.')
154-
}
155-
156-
// Get the number of non-empty nodes passed as children, this will exclude
157-
// booleans, null, and undefined
158-
const validChildren = React.Children.toArray(children).length
159-
const enabled = useFeatureFlag(CSS_MODULES_FEATURE_FLAG)
130+
export type ProgressBarBaseProps = Omit<
131+
React.HTMLAttributes<HTMLSpanElement> & {
132+
bg?: string
133+
className?: string
134+
} & StyledProgressContainerProps &
135+
ProgressProp,
136+
'children' | 'aria-label'
137+
>
138+
139+
export type WithChildren = {
140+
children: React.ReactNode
141+
'aria-label'?: never
142+
}
160143

161-
const cssModulesProps = !enabled
162-
? {barSize}
163-
: {'data-progress-display': rest.inline ? 'inline' : 'block', 'data-progress-bar-size': barSize}
144+
export type WithoutChildren = {
145+
children?: never
146+
'aria-label': string
147+
}
164148

165-
return (
166-
<ProgressContainer
167-
ref={forwardRef}
168-
className={clsx(className, {[classes.ProgressBarContainer]: enabled})}
169-
{...cssModulesProps}
170-
{...rest}
171-
>
172-
{validChildren ? (
173-
children
174-
) : (
175-
<Item
176-
data-animated={animated}
177-
progress={progress}
178-
aria-label={ariaLabel}
179-
aria-valuenow={ariaValueNow}
180-
aria-valuetext={ariaValueText}
181-
bg={bg}
182-
/>
183-
)}
184-
</ProgressContainer>
185-
)
186-
},
187-
)
149+
export type ProgressBarProps = ProgressBarBaseProps & (WithChildren | WithoutChildren)
150+
151+
export const ProgressBar = forwardRef<HTMLSpanElement, ProgressBarProps>((props, forwardRef) => {
152+
const {
153+
animated,
154+
progress,
155+
bg = 'success.emphasis',
156+
barSize = 'default',
157+
children,
158+
'aria-label': ariaLabel,
159+
'aria-valuenow': ariaValueNow,
160+
'aria-valuetext': ariaValueText,
161+
className,
162+
...rest
163+
} = props
164+
165+
if (children && progress) {
166+
throw new Error('You should pass `progress` or children, not both.')
167+
}
168+
169+
// Get the number of non-empty nodes passed as children, this will exclude
170+
// booleans, null, and undefined
171+
const validChildren = React.Children.toArray(children).length
172+
const enabled = useFeatureFlag(CSS_MODULES_FEATURE_FLAG)
173+
174+
const cssModulesProps = !enabled
175+
? {barSize}
176+
: {'data-progress-display': rest.inline ? 'inline' : 'block', 'data-progress-bar-size': barSize}
177+
178+
return (
179+
<ProgressContainer
180+
ref={forwardRef}
181+
className={clsx(className, {[classes.ProgressBarContainer]: enabled})}
182+
{...cssModulesProps}
183+
{...rest}
184+
>
185+
{validChildren ? (
186+
children
187+
) : (
188+
<Item
189+
data-animated={animated}
190+
progress={progress}
191+
aria-label={ariaLabel as string}
192+
aria-valuenow={ariaValueNow}
193+
aria-valuetext={ariaValueText}
194+
bg={bg}
195+
/>
196+
)}
197+
</ProgressContainer>
198+
)
199+
})
188200

189201
ProgressBar.displayName = 'ProgressBar'

0 commit comments

Comments
 (0)