: * a search field whose results are buttons, and a quick add by name. The * page has a form object with a `customer_id` string and may override * customerChosen() to clear fields that depend on the customer. * * @property-read list $customerResults */ trait PicksCustomer { public string $customerSearch = ''; #[Locked] public string $customerName = ''; public string $statusMessage = ''; /** * @return list */ #[Computed] public function customerResults(): array { $term = trim($this->customerSearch); return mb_strlen($term) < 2 ? [] : app(CustomerQueries::class)->search($term, 8); } public function selectCustomer(int $customerId): void { $this->authorize('jobs.manage'); $customer = app(CustomerQueries::class)->findSummary($customerId); if ($customer !== null) { $this->useCustomer($customer->id, $customer->name); } } public function createCustomer(RegisterCustomer $registerCustomer): void { $this->authorize('jobs.manage'); $this->authorize('customers.use'); $name = trim($this->customerSearch); try { $customerId = $registerCustomer->execute(new CustomerDetailsData($name, CustomerType::Company)); } catch (DomainException) { $this->addError('customerSearch', __('customers.errors.invalid_name')); return; } $this->useCustomer($customerId, $name); $this->statusMessage = __('jobs.customer_picker.created', ['name' => $name]); } public function clearCustomer(): void { $this->form->customer_id = ''; $this->customerName = ''; $this->statusMessage = ''; $this->customerChosen(null); $this->js("document.getElementById('customer-search')?.focus()"); } /** * Called when the customer changes; null when it was cleared. */ protected function customerChosen(?int $customerId): void {} private function useCustomer(int $customerId, string $name): void { $changed = (string) $customerId !== $this->form->customer_id; $this->form->customer_id = (string) $customerId; $this->customerName = $name; $this->customerSearch = ''; $this->resetErrorBag(['customerSearch', 'form.customer_id']); if ($changed) { $this->customerChosen($customerId); } $this->js("document.getElementById('change-customer')?.focus()"); } }