-
Notifications
You must be signed in to change notification settings - Fork 11
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
Capturing input value and using it as the selected option #5
Comments
Hey, @Mitchell8210 - |
Hey, Firstly thank you for this library - It has been amazing so far :). I would also like to do this - i.e. capture the input value and use it as the selected option: For Example, if I start with 3 options: ['cat', 'dog', 'mouse'] I would like to allow users to type in 'horse' and upon pressing enter, the original options array is expanded to include horse, which would then be selected. (On a page refresh, I would want it to defaults back to the original option selection). This is working nicely. However, I am unable to capture the input value with This is what I have currently: import React, { useState, useCallback, useRef } from 'react';
import { Select } from 'react-functional-select';
const initialOptions = [
{ id: 0, val: 'Cat' },
{ id: 1, val: 'Mouse' },
{ id: 2, val: 'Dog' },
];
function SelectColumnFilter() {
const [currentValue, setCurrentValue] = useState(null);
const [currentOptions, setCurrentOptions] = useState(initialOptions);
const selectRef = useRef(null);
const getOptionValue = useCallback((option) => option.id, []);
const getOptionLabel = useCallback((option) => option.val, []);
const onOptionChange = useCallback((option) => {
if (option != null) {
setCurrentValue(option.val);
} else {
setCurrentValue(null);
}
}, []);
const onKeyDown = useCallback((e) => {
var keycode = e.keyCode ? e.keyCode : e.which;
if (keycode == '13') {
console.log(e);
console.log(selectRef.current);
// I would like to get the current inputted value here.
const newOption = { id: currentOptions.length, val: 'Horse' };
const nextOptions = [...currentOptions, newOption];
setCurrentOptions(nextOptions)
}
}, []);
return (
<Select
ref={selectRef}
options={currentOptions}
onOptionChange={onOptionChange}
getOptionValue={getOptionValue}
getOptionLabel={getOptionLabel}
onKeyDown={onKeyDown}
initialValue={currentValue}
/>
);
}
export default SelectColumnFilter; |
@IsmailM - I just recently published changes for import React, { useState, useCallback, useRef } from "react";
import { Select } from "react-functional-select";
const initialOptions = [
{ id: 0, val: "Cat" },
{ id: 1, val: "Mouse" },
{ id: 2, val: "Dog" },
];
function SelectColumnFilter() {
const [currentValue, setCurrentValue] = useState(null);
const [currentOptions, setCurrentOptions] = useState(initialOptions);
const selectRef = useRef(null);
const getOptionValue = useCallback((option) => option.id, []);
const getOptionLabel = useCallback((option) => option.val, []);
const onOptionChange = useCallback((option) => {
if (option != null) {
setCurrentValue(option.val);
} else {
setCurrentValue(null);
}
}, []);
const onKeyDown = useCallback((e, input, focusedOption) => {
var keycode = e.keyCode ? e.keyCode : e.which;
if (keycode === "13" && input && focusedOption.index === -1) {
e.preventDefault();
setCurrentOptions((prevOptions) => [
...prevOptions,
{ id: prevOptions.length, val: input },
]);
}
}, []);
return (
<Select
ref={selectRef}
options={currentOptions}
onOptionChange={onOptionChange}
getOptionValue={getOptionValue}
getOptionLabel={getOptionLabel}
onKeyDown={onKeyDown}
initialValue={currentValue}
/>
);
}
export default SelectColumnFilter; |
This looks amazing! Thank you very much for taking the time to implement this 👍 |
@IsmailM - no problem, let me know if you find any issues with the recent changes. Thanks for the feedback and insights! |
Hello, I am trying to implement the react-functional-select component into an application that I am working on, I am wondering if there is a way to capture the input from the user and be able to use that value as the selected option. Is this part of the functionality that is built into this component?
I have a list of options that exist already, my goal is to be able to capture the input from the user in the case that what they typed in does not match any of the options that are available in the predefined set.
The text was updated successfully, but these errors were encountered: