Skip to content

Commit

Permalink
fix: make autoFocus correct for text components
Browse files Browse the repository at this point in the history
This fix includes useStableEventHandler rewriting
to exclude unexpected errors
  • Loading branch information
rrewll committed Apr 2, 2024
1 parent 77697a4 commit 6f63777
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 7 deletions.
8 changes: 2 additions & 6 deletions src/helpers/useStableEventHendler.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import React from 'react';

function error() {
throw new Error('Cannot call an event handler while rendering or after unmount.');
}

export function useStableEventHandler<TFunc extends (...args: unknown[]) => void>(
func: TFunc | undefined,
) {
const ref = React.useRef<TFunc | undefined>(error as any);
const ref = React.useRef<TFunc | undefined>(undefined);

React.useEffect(() => {
ref.current = func;
return () => {
ref.current = error as any;
ref.current = undefined;
};
}, [func]);

Expand Down
2 changes: 1 addition & 1 deletion src/stories/dialog/00_Base_controls.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ function DialogDemo({
</div>
),
}),
makeField('text'),
makeField('text', {autoFocus: true}),
makeField('multi-text'),
makeField('checkbox', {children: 'checkbox'}),
makeField('tumbler', {labelLeft: 'Left label', labelRight: 'Right label'}),
Expand Down

0 comments on commit 6f63777

Please sign in to comment.