Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Pass value to visual input in select #2676

Merged
merged 5 commits into from
Apr 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions modules/react/select/lib/hooks/useSelectInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ import {
} from '@workday/canvas-kit-react/combobox';
import {useSelectModel} from './useSelectModel';

function noop() {
// Do nothing
}

/**
* `useSelectInput` extends {@link useComboboxInput useComboboxInput} and {@link useComboboxKeyboardTypeAhead useComboboxKeyboardTypeAhead} and adds type ahead functionality and Select-specific [keyboard support](https://www.w3.org/WAI/ARIA/apg/patterns/combobox/examples/combobox-select-only/).
*/
export const useSelectInput = composeHooks(
createElemPropsHook(useSelectModel)(
(model, ref, elemProps: {keySofar?: string; placeholder?: string} = {}) => {
(model, ref, elemProps: {keySofar?: string; placeholder?: string; value?: string} = {}) => {
const {elementRef} = useLocalRef<HTMLInputElement>(ref as any);
const textInputRef = React.useRef<HTMLInputElement>(null);

Expand All @@ -30,6 +34,7 @@ export const useSelectInput = composeHooks(
Object.getPrototypeOf(textInputRef.current),
'value'
);

if (nativeInputValue && nativeInputValue.set) {
nativeInputValue.set.call(textInputRef.current, value);
}
Expand All @@ -54,6 +59,19 @@ export const useSelectInput = composeHooks(
model.state.selectedIds[0]
) {
const value = model.navigation.getItem(model.state.selectedIds[0], model).id;

// force the hidden input to have the correct value
if (model.state.inputRef.current.value !== value) {
const nativeInputValue = Object.getOwnPropertyDescriptor(
Object.getPrototypeOf(model.state.inputRef.current),
'value'
);

if (nativeInputValue && nativeInputValue.set) {
nativeInputValue.set.call(model.state.inputRef.current, value);
}
}

if (
model.state.selectedIds[0] !== value &&
model.state.inputRef.current.value !== value
Expand Down Expand Up @@ -130,15 +148,13 @@ export const useSelectInput = composeHooks(
},
textInputProps: {
ref: textInputRef,
onChange: noop,
value:
model.state.selectedIds.length > 0 && model.state.items.length > 0
? model.navigation.getItem(model.state.selectedIds[0], model).textValue
: '',
},
ref: elementRef,

// Account for the case where an initial item is selected when the Select first renders
defaultValue:
model.state.selectedIds.length > 0 && model.state.items.length > 0
? model.navigation.getItem(model.state.selectedIds[0], model).textValue ||
model.state.value
: undefined,
} as const;
}
),
Expand Down
Loading