I'm starting with react.
I would like the value returned from 'handleClickLoadPIX' to be filled as the value of field 'pj_pix_key'.
I tried to use value={pj_pix_key}
on the <OutlinedInput
but returned value is never possible to be deleted to the field
//** Const
const defaultFields = {
financial: {
fields: {
init: {
pj_pix_key: ``,
},
reset: {
pj_pix_key: ``,
}
},
schema: yup.object().shape({
pj_pix_key: yup.string().optional(),
})
}
}
// ** States
const [pj_pix_key, setPj_pix_key] = React.useState('')
// ** Handle
const handleClickLoadPIX = async value => {
let lookupFieldValue = 'returned-key-from-endpoint'
setPj_pix_key('load-key-from-endpoint')
}
// ** Hooks
const {
reset: financialReset,
control: financialControl,
handleSubmit: handleFinancialSubmit,
formState: { errors: financialErrors }
} = useForm({
defaultValues: defaultFields.financial.fields.init,
mode: 'onBlur',
resolver: yupResolver(defaultFields.financial.schema)
})
//** JSX
<FormControl fullWidth>
<InputLabel id='pj_pix_key-label' htmlFor='pj_pix_key' error={Boolean(financialErrors.pj_pix_key)}>
Chave PIX
</InputLabel>
<Controller
name='pj_pix_key'
control={financialControl}
rules={{ required: false }}
render={({ field: { value, onChange, onBlur } }) => (
<OutlinedInput
value={value}
label='Chave PIX'
onChange={onChange}
onBlur={onBlur}
id='pj_pix_key'
error={Boolean(financialErrors.pj_pix_key)}
type='text'
placeholder='ex.: 12345678'
aria-describedby='pj_pix_key-helper'
labelId='pj_pix_key-label'
endAdornment={
<InputAdornment position='end'>
<Tooltip title='Carregar PIX do CNPJ ou Email comercial'>
<IconButton
edge='end'
onClick={e => handleClickLoadPIX()}
onMouseDown={e => e.preventDefault()}
aria-label='pj_pix_key-helper'
>
<Icon icon='mdi:auto-download' />
</IconButton>
</Tooltip>
</InputAdornment>
}
/>
)}
/>
{financialErrors.pj_pix_key && (
<FormHelperText sx={{ color: 'error.main' }} id='pj_pix_key-helper'>
Campo obrigatório
</FormHelperText>
)}
</FormControl>