Skip to content

Commit e95bfdb

Browse files
mannycarrera4manuel.carrera
and
manuel.carrera
authored
fix: Define stable ref reference in useComboboxInputConstrained (Workday#3145)
Create a stable reference to the form element ref in `useComboboxInputConstrained`. React during the lifecycle will sometimes set the ref to `null`. By setting it to a variable we create a stable reference. [category:Components] Co-authored-by: manuel.carrera <[email protected]>
1 parent 921fb68 commit e95bfdb

File tree

4 files changed

+36
-8
lines changed

4 files changed

+36
-8
lines changed

cypress/component/Examples.spec.tsx

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import * as React from 'react';
2+
3+
import {TextInputWithReactHookForm} from '@workday/canvas-kit-preview-react/_examples/stories/mdx/examples/TextInputWithReactHookForm';
4+
5+
describe('TextInputWithReactHookForm', () => {
6+
context('given inputs using React Hook Form', () => {
7+
beforeEach(() => {
8+
cy.mount(<TextInputWithReactHookForm />);
9+
});
10+
11+
it('should not have any axe errors', () => {
12+
cy.checkA11y();
13+
});
14+
});
15+
});

modules/preview-react/_examples/stories/mdx/Forms.mdx modules/preview-react/_examples/stories/mdx/FormLibraryExample.mdx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import {ExampleCodeBlock} from '@workday/canvas-kit-docs';
22
import {TextInputWithReactHookForm} from './examples/TextInputWithReactHookForm';
3+
import * as FormStories from './FormLibraryExample.stories';
34

4-
<Meta title="Examples/Preview/Forms" />
5+
<Meta of={FormStories} />
56

6-
# Canvas Kit Forms Examples
77

88
## Uncontrolled Text Inputs Using React Hook Form
99

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import {Meta} from '@storybook/react';
2+
3+
import {TextInputWithReactHookForm as TextInputWithReactHookFormExample} from './examples/TextInputWithReactHookForm';
4+
5+
export default {
6+
title: 'Examples/Forms',
7+
parameters: {},
8+
} as Meta;
9+
10+
export const TextInputWithReactHookForm = {
11+
render: TextInputWithReactHookFormExample,
12+
};

modules/react/combobox/lib/hooks/useComboboxInputConstrained.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,20 @@ export const useComboboxInputConstrained = createElemPropsHook(useComboboxModel)
6363
formElementRef,
6464
() => {
6565
if (formLocalRef.current) {
66+
const formElementRefStable = formLocalRef.current;
6667
// Hook into the DOM `value` property of the form input element and update the model
6768
// accordingly
68-
Object.defineProperty(formLocalRef.current, 'value', {
69+
Object.defineProperty(formElementRefStable, 'value', {
6970
get() {
7071
const value = Object.getOwnPropertyDescriptor(
71-
Object.getPrototypeOf(formLocalRef.current),
72+
Object.getPrototypeOf(formElementRefStable),
7273
'value'
73-
)?.get?.call(formLocalRef.current);
74+
)?.get?.call(formElementRefStable);
7475
return value;
7576
},
7677
set(value: string) {
7778
if (
78-
formLocalRef.current &&
79+
formElementRefStable &&
7980
value !==
8081
(modelStateRef.current.selectedIds === 'all'
8182
? []
@@ -88,10 +89,10 @@ export const useComboboxInputConstrained = createElemPropsHook(useComboboxModel)
8889
});
8990

9091
// forward calls to `.focus()` and `.blur()` to the user input
91-
formLocalRef.current.focus = (options?: FocusOptions) => {
92+
formElementRefStable.focus = (options?: FocusOptions) => {
9293
userLocalRef.current!.focus(options);
9394
};
94-
formLocalRef.current.blur = () => {
95+
formElementRefStable.blur = () => {
9596
userLocalRef.current!.blur();
9697
};
9798
}

0 commit comments

Comments
 (0)