i want to get the file name and show it in input element using javascript.
i have a button upload clicking that would open the browser to choose file once chosen i should display the chosen file name in input element.
below is my code,
const kubeConfig = get(values, `${fieldRoot}.kubeConfig`);
const fileName = useMemo(() => {
if (kubeConfig) {
return kubeConfig.name; //cannot read property name of undefined
}
}, [kubeConfig])
<FormField
label="file"
fieldId={`${fieldRoot}.kubeConfig`}
>
{({form}: FieldProps) => (
<>
<input
type="text"
value={fileName ?? 'Upload file'}
/>
<input
type="file"
style={{display: 'none'}}
accept=".pem"
ref={inputRef}
onChange={async() => {
const file = inputRef?.current?.files?.[0];
if (file) {
try {
form.setFieldValue(
`${fieldRoot}.kubeConfig`,
file
);
}
}
}}
/>
<button onClick={handleUploadClick}> Upload </button>
)}
</FormField>
i get error cannot read property name of undefined.
kubeconfig data is like below
I am new to programming learning on the go. could someone help me with this. thanks. how can i access name from kubeConfig data.