-
Notifications
You must be signed in to change notification settings - Fork 223
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
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,7 @@ import {useSelectModel} from './useSelectModel'; | |
*/ | ||
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); | ||
|
||
|
@@ -30,6 +30,7 @@ export const useSelectInput = composeHooks( | |
Object.getPrototypeOf(textInputRef.current), | ||
'value' | ||
); | ||
|
||
if (nativeInputValue && nativeInputValue.set) { | ||
nativeInputValue.set.call(textInputRef.current, value); | ||
} | ||
|
@@ -54,6 +55,7 @@ export const useSelectInput = composeHooks( | |
model.state.selectedIds[0] | ||
) { | ||
const value = model.navigation.getItem(model.state.selectedIds[0], model).id; | ||
|
||
if ( | ||
model.state.selectedIds[0] !== value && | ||
model.state.inputRef.current.value !== value | ||
|
@@ -130,6 +132,11 @@ export const useSelectInput = composeHooks( | |
}, | ||
textInputProps: { | ||
ref: textInputRef, | ||
value: elemProps.value | ||
? model.navigation.getItem(model.state.selectedIds[0], model).textValue | ||
: model.state.selectedIds.length > 0 && model.state.items.length > 0 | ||
? model.navigation.getItem(model.state.selectedIds[0], model).textValue | ||
: undefined, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I need someone to verify this logic. When Nicholas worked on this in this sandbox it worked. However, it broke the dynamic items story because it wasn't setting the value of the visual input. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also this change now throws the following error because we're updating the value ourselves:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The comment splitting the code gave me trouble trying to understand the logic in its entirety: value: elemProps.value
? model.navigation.getItem(model.state.selectedIds[0], model).textValue
: model.state.selectedIds.length > 0 && model.state.items.length > 0
? model.navigation.getItem(model.state.selectedIds[0], model).textValue
: undefined, It looks like this code says the following: "if the developer passed a This doesn't make sense. Are you meaning it to always be a "controlled" input to React? It seems like the following would work better: model.state.selectedIds.length > 0 && model.state.items.length > 0
? model.navigation.getItem(model.state.selectedIds[0], model).textValue
: undefined, This logic says if there's a selected id and items, the |
||
}, | ||
ref: elementRef, | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the failure of the ternary supposed to do? The first part says "if the developer passes a
value
, do a text lookup of that value". The original sandbox had the failure case returnundefined
, meaning "if the developer didn't pass avalue
, don't treat this like a controllable input". But themodel.state.selectedIds.length > 0 && model.state.items.length > 0
will returntrue
orfalse
if avalue
is not provided. What does this do? It doesn't match the original intent, but what is the side effect?