Skip to content

Commit

Permalink
fix(progress): 🐛 remove value state
Browse files Browse the repository at this point in the history
  • Loading branch information
navin-moorthy committed Feb 3, 2021
1 parent 063eec1 commit b84d759
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 18 deletions.
22 changes: 8 additions & 14 deletions src/progress/ProgressState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,33 +36,27 @@ export interface ProgressState {
percent: number | null;
}

export interface ProgressAction {
/**
* Update the value of the progress indicator
*/
setValue: React.Dispatch<React.SetStateAction<number | null>>;
}

export type ProgressInitialState = Pick<
Partial<ProgressState>,
"value" | "min" | "max"
>;

export type ProgressStateReturn = ProgressState & ProgressAction;
export type ProgressStateReturn = ProgressState;

export function useProgressState(
props: ProgressInitialState = {},
): ProgressStateReturn {
const { value: defaultValue = 0, min = 0, max = 100 } = props;
const [value, setValue] = React.useState(clampValue(defaultValue, min, max));
const percent = isNull(value) ? null : valueToPercent(value, min, max);
const { value = 0, min = 0, max = 100 } = props;
const clampedValue = clampValue(value, min, max);
const percent = isNull(clampedValue)
? null
: valueToPercent(clampedValue, min, max);

return {
value,
setValue,
value: clampedValue,
min,
max,
isIndeterminate: isNull(value),
isIndeterminate: isNull(clampedValue),
percent,
};
}
Expand Down
5 changes: 3 additions & 2 deletions src/progress/stories/CircularProgress.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ export interface AppProps extends ProgressInitialState {

export const App: React.FC<AppProps> = props => {
const { withLabel = false, children, ...rest } = props;
const state = useProgressState(rest);
const { value, setValue, percent, isIndeterminate } = state;
const [value, setValue] = React.useState<number | null>(0);
const state = useProgressState({ value, ...rest });
const { percent, isIndeterminate } = state;

React.useEffect(() => {
const clearId = setInterval(() => {
Expand Down
5 changes: 3 additions & 2 deletions src/progress/stories/LinearProgress.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ export const App: React.FC<AppProps> = props => {
withStripeAnimation = false,
...rest
} = props;
const state = useProgressState(rest);
const { value, setValue, percent, isIndeterminate } = state;
const [value, setValue] = React.useState<number | null>(0);
const state = useProgressState({ value, ...rest });
const { percent, isIndeterminate } = state;

React.useEffect(() => {
const clearId = setInterval(() => {
Expand Down

1 comment on commit b84d759

@vercel
Copy link

@vercel vercel bot commented on b84d759 Feb 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.