I have a class, lets call Customer...
export default function Customer({ customerId }: Props) {
const [providerId, setProviderId] = useState("");
}
It contains a component imported from another class, which itself is a Select box.
<InputProvider
id="idProviderId"
selectedVal={providerId}
onProviderChange={providerUpdate} />
The InputProvider class looks as follows:
interface Props {
id: string;
selectedVal: string;
//onProviderChange: (providerId: string) => void;
onProviderChange?: ((value: any, action: ActionMeta<any>) => void)
}
export default function InputProvider({id, onProviderChange, selectedVal}: Props) {
//const [customerSaleTypeList, setCustomerSaleTypeList] = useState(new CustomerSaleTypeListDto());
const { data: ListOfCustomerTradeProviders } = useQuery("CustomerTradeProviders", () => getListofCustomerTradeProviders());
//alert(getListofCustomerSaleTypes());
const [value, setValue] = useState()
return (
<InputSelect
name="providerId"
label="Provider"
instanceId={id}
onChange={onProviderChange}
options={ListOfCustomerTradeProviders || []}
/>
);
}
What I want to be able to do is pass in my onChange function, and from there work out the selected value. I am passing in "onProviderChange", in the props I'm really not sure what to set, I've tried a variety of things.
Then back in my Customer class, here's what providerUpdate() looks like:
function providerUpdate(e: any) {
alert(String(e.target.value));
setProviderId(e.target.value); // this line generates the error: TypeError: Cannot read properties of undefined (reading 'value')
//alert("Changing...");
}
InputSelect is as follows, this is a snippet:
export default function InputSelect({
label, name, options, placeholder, message, isClearable, canAddItems, instanceId, onChange,
}: Props) {
const [field, meta, helpers] = useField(name || label || "unknownfield");
const { error } = meta;
const { setValue } = helpers;
function getOptionValue() {
// Select value needs to be the object in the options list not the field value
let foundOption = options.find((item) => item.value === field.value);
if (foundOption) { return foundOption; }
if (canAddItems && field.value) {
// Add the custom value that the user entered so it shows in the control
foundOption = { value: field.value, label: field.value };
options.push(foundOption);
} else {
// Can't add items or value is empty string/null
return null;
}
return foundOption;
}
function handleOnChange(option: InputSelectItem, actionMeta: ActionMeta<any>) {
setValue(option?.value);
if (onChange) { onChange(option, actionMeta); }
}
return (
<>
<Form.Group controlId={name || label}>
<Form.Label>{label || name}</Form.Label>
{canAddItems ? (
<CreatableSelect
name={field.name}
options={options}
value={getOptionValue()}
isClearable={isClearable}
placeholder={placeholder || label}
onChange={handleOnChange}
onBlur={field.onBlur}
instanceId={instanceId}
theme={selectTheme}
styles={selectStyle}
/>
)
: (
<Select
name={field.name}
options={options}
value={getOptionValue()}
isClearable={isClearable}
placeholder={placeholder || label}
onChange={handleOnChange}
onBlur={field.onBlur}
instanceId={instanceId}
theme={selectTheme}
styles={selectStyle}
/>
)}
{(message && !error) && (
<Form.Text>{message}</Form.Text>
)}
{error && (
<Form.Text className="text-error">{error}</Form.Text>
)}
</Form.Group>
</>
);
}
InputSelect.defaultProps = defaultProps;
Where to from here? This seems like such a simple task and I can't work it out.