Reactive inputs?

Hi guys, is it possible in my example make material_price reactive and calculate its value when material_cost or material_margin changed? Right now I have afterStateUpdated method on these two inputs, but I have places where many more inputs affect just one.
MoneyInput::make('material_cost')
->label('Cost')
->required()
->lazy()
->afterStateUpdated(function (?float $state, Get $get, Set $set): void {
$margin = (float) ($get('material_margin') ?? 0);

if (! is_numeric($state)) {
$set('material_price', null);

return;
}

$price = $state + ($state * ($margin / 100));
$set('material_price', round($price, 2));
}),

TextInput::make('material_margin')
->label('Margin')
->suffix('%')
->default(50)
->required()
->numeric()
->step(0.1)
->minValue(0)
->lazy()
->afterStateUpdated(function (?float $state, Get $get, Set $set): void {
$cost = (float) ($get('material_cost') ?? 0);

$price = $cost + ($cost * ($state / 100));
$set('material_price', round($price, 2));
}),

MoneyInput::make('material_price')
->label('Price')
->required(),
MoneyInput::make('material_cost')
->label('Cost')
->required()
->lazy()
->afterStateUpdated(function (?float $state, Get $get, Set $set): void {
$margin = (float) ($get('material_margin') ?? 0);

if (! is_numeric($state)) {
$set('material_price', null);

return;
}

$price = $state + ($state * ($margin / 100));
$set('material_price', round($price, 2));
}),

TextInput::make('material_margin')
->label('Margin')
->suffix('%')
->default(50)
->required()
->numeric()
->step(0.1)
->minValue(0)
->lazy()
->afterStateUpdated(function (?float $state, Get $get, Set $set): void {
$cost = (float) ($get('material_cost') ?? 0);

$price = $cost + ($cost * ($state / 100));
$set('material_price', round($price, 2));
}),

MoneyInput::make('material_price')
->label('Price')
->required(),
0 Replies
No replies yetBe the first to reply to this messageJoin

Did you find this page helpful?