Fix recalculate in input

Hey guys, I have 3 inputs in my form and a 4th for the sum of these 3, it works well for me but when I recalculate or modify some of the inputs after the sum I cannot do the recalculation... I have my code like this:
Forms\Components\TextInput::make('alicuotadepto')
->label('% Alicuota Departamento')
->placeholder('Ingrese la alicuota en %, ejemplo: 0.02')
->required()
->numeric()
->mask(RawJs::make(<<<'JS'
/^(\d{1,4})(\d{0,2})?$/.test($input) ? $input.replace(/^(\d{1})(\d{0,2})?$/, '$1.$2') : ''
JS)),
Forms\Components\TextInput::make('alicuotadepto')
->label('% Alicuota Departamento')
->placeholder('Ingrese la alicuota en %, ejemplo: 0.02')
->required()
->numeric()
->mask(RawJs::make(<<<'JS'
/^(\d{1,4})(\d{0,2})?$/.test($input) ? $input.replace(/^(\d{1})(\d{0,2})?$/, '$1.$2') : ''
JS)),
Forms\Components\TextInput::make('alicuotabodega')
->label('% Alicuota Bodega')
->placeholder('Ingrese la alicuota en %, ejemplo: 0.02')
->required()
->numeric()
->mask(RawJs::make(<<<'JS'
/^(\d{1,4})(\d{0,2})?$/.test($input) ? $input.replace(/^(\d{1})(\d{0,2})?$/, '$1.$2') : ''
JS)),
Forms\Components\TextInput::make('alicuotabodega')
->label('% Alicuota Bodega')
->placeholder('Ingrese la alicuota en %, ejemplo: 0.02')
->required()
->numeric()
->mask(RawJs::make(<<<'JS'
/^(\d{1,4})(\d{0,2})?$/.test($input) ? $input.replace(/^(\d{1})(\d{0,2})?$/, '$1.$2') : ''
JS)),
Forms\Components\TextInput::make('alicuotaestacionamiento')
->label('% Alicuota Estacionamiento')
->placeholder('Ingrese la alicuota en %, ejemplo: 0.02')
->required()
->numeric()
->live(debounce:1500)
->mask(RawJs::make(<<<'JS'
/^(\d{1,4})(\d{0,2})?$/.test($input) ? $input.replace(/^(\d{1})(\d{0,2})?$/, '$1.$2') : ''
JS))
->afterStateUpdated(function (Set $set, Get $get) {
$alicuotadepto = $get('alicuotadepto');
$alicuotabodega = $get('alicuotabodega');
$alicuotaestacionamiento = $get('alicuotaestacionamiento');
// Verificar si ambos campos tienen valores
if ($alicuotadepto !== '' && $alicuotabodega !== '' && $alicuotaestacionamiento != '') {
$alicuotatotal = $alicuotadepto + $alicuotabodega + $alicuotaestacionamiento;
$set('alicuotatotal', $alicuotatotal);
}
}),
Forms\Components\TextInput::make('alicuotaestacionamiento')
->label('% Alicuota Estacionamiento')
->placeholder('Ingrese la alicuota en %, ejemplo: 0.02')
->required()
->numeric()
->live(debounce:1500)
->mask(RawJs::make(<<<'JS'
/^(\d{1,4})(\d{0,2})?$/.test($input) ? $input.replace(/^(\d{1})(\d{0,2})?$/, '$1.$2') : ''
JS))
->afterStateUpdated(function (Set $set, Get $get) {
$alicuotadepto = $get('alicuotadepto');
$alicuotabodega = $get('alicuotabodega');
$alicuotaestacionamiento = $get('alicuotaestacionamiento');
// Verificar si ambos campos tienen valores
if ($alicuotadepto !== '' && $alicuotabodega !== '' && $alicuotaestacionamiento != '') {
$alicuotatotal = $alicuotadepto + $alicuotabodega + $alicuotaestacionamiento;
$set('alicuotatotal', $alicuotatotal);
}
}),
Forms\Components\TextInput::make('alicuotatotal')
->label('Alicuota Total')
->placeholder('La alicuota total se calculara automaticamente')
->readOnly()
->required(),
Forms\Components\TextInput::make('alicuotatotal')
->label('Alicuota Total')
->placeholder('La alicuota total se calculara automaticamente')
->readOnly()
->required(),
How can I do this recalculation?
2 Replies
Majid Al Zariey
Majid Al Zariey8mo ago
The afterStateUpdated should be in each input and all inputs should be live() Unless you are always changing the value of the last input.
TranceCode
TranceCodeOP8mo ago
Yeah it's working, thank you @Majid Al Zariey
Want results from more Discord servers?
Add your server