|
| 1 | +[](https://packagist.org/packages/vildanbina/livewire-tabs) |
| 2 | +[](https://packagist.org/packages/vildanbina/livewire-tabs) |
| 3 | +[](https://packagist.org/packages/vildanbina/livewire-tabs) |
| 4 | +[](https://packagist.org/packages/vildanbina/livewire-tabs) |
| 5 | +[](https://packagist.org/packages/vildanbina/livewire-tabs) |
| 6 | + |
| 7 | +A dynamic Laravel Livewire component for tab forms. |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +## Installation |
| 12 | + |
| 13 | +You can install the package via composer: |
| 14 | + |
| 15 | +``` bash |
| 16 | +composer require vildanbina/livewire-tabs |
| 17 | +``` |
| 18 | + |
| 19 | +## TailwindCSS |
| 20 | + |
| 21 | +The base modal is made with TailwindCSS. If you use a different CSS framework I recommend that you publish the modal template and change the markup to include the required classes for your CSS framework. |
| 22 | + |
| 23 | +```shell |
| 24 | +php artisan vendor:publish --tag=livewire-tabs-views |
| 25 | +``` |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +### Creating a Tab Container |
| 30 | + |
| 31 | +You can create livewire component `php artisan make:livewire UserTab` to make the initial Livewire component. Open your component class and make sure it extends the `TabsComponent` class: |
| 32 | + |
| 33 | +```php |
| 34 | +<?php |
| 35 | + |
| 36 | +namespace App\Http\Livewire; |
| 37 | + |
| 38 | +use Vildanbina\LivewireTabs\TabsComponent; |
| 39 | +use App\Models\User; |
| 40 | + |
| 41 | +class UserTab extends TabsComponent |
| 42 | +{ |
| 43 | + // My custom class property |
| 44 | + public $userId; |
| 45 | + |
| 46 | + /* |
| 47 | + * Will return App\Models\User instance or will create empty User (based on $userId parameter) |
| 48 | + */ |
| 49 | + public function model() |
| 50 | + { |
| 51 | + return User::findOrNew($this->userId); |
| 52 | + } |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +When you need to display tabs form, based on above example we need to pass `$userId` value and to display tabs form: |
| 57 | + |
| 58 | +```html |
| 59 | +<livewire:user-tabs user-id="3"/> |
| 60 | +``` |
| 61 | + |
| 62 | +Or when you want to create new user, let blank `user-id` attribute, or don't put that. |
| 63 | + |
| 64 | +When you want to have current tab instance. You can use: |
| 65 | + |
| 66 | +```php |
| 67 | +$tabsFormInstance->getCurrentTab(); |
| 68 | +``` |
| 69 | + |
| 70 | +When you want to go to specific tab. You can use: |
| 71 | + |
| 72 | +```php |
| 73 | +$tabsFormInstance->setTab($tab); |
| 74 | +``` |
| 75 | + |
| 76 | +You can customize tab footer buttons, create some view and put that view to method: |
| 77 | + |
| 78 | +```php |
| 79 | +public function tabFooter() |
| 80 | +{ |
| 81 | + return view('livewire-tabs::tabs-footer'); |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +### Creating a Tab Item |
| 86 | + |
| 87 | +You can create tabs form tab. Open or create your tab class (at `App\Tabs` folder) and make sure it extends the `Tab` class: |
| 88 | + |
| 89 | +```php |
| 90 | +<?php |
| 91 | + |
| 92 | +namespace App\Tabs; |
| 93 | + |
| 94 | +use Vildanbina\LivewireTabs\Components\Tab; |
| 95 | +use Illuminate\Validation\Rule; |
| 96 | + |
| 97 | +class General extends Tab |
| 98 | +{ |
| 99 | + // Tab view located at resources/views/tabs/general.blade.php |
| 100 | + protected string $view = 'tabs.general'; |
| 101 | + |
| 102 | + /* |
| 103 | + * Initialize tab fields |
| 104 | + */ |
| 105 | + public function mount() |
| 106 | + { |
| 107 | + $this->mergeState([ |
| 108 | + 'name' => $this->model->name, |
| 109 | + 'email' => $this->model->email, |
| 110 | + ]); |
| 111 | + } |
| 112 | + |
| 113 | + /* |
| 114 | + * Tab icon |
| 115 | + */ |
| 116 | + public function icon() |
| 117 | + { |
| 118 | + return view('icons.home'); |
| 119 | + } |
| 120 | + |
| 121 | + /* |
| 122 | + * When Tabs Form has submitted |
| 123 | + */ |
| 124 | + public function save($state) |
| 125 | + { |
| 126 | + $user = $this->model; |
| 127 | + |
| 128 | + $user->name = $state['name']; |
| 129 | + $user->email = $state['email']; |
| 130 | + |
| 131 | + $user->save(); |
| 132 | + } |
| 133 | + |
| 134 | + /* |
| 135 | + * Tab Validation |
| 136 | + */ |
| 137 | + public function validate() |
| 138 | + { |
| 139 | + return [ |
| 140 | + [ |
| 141 | + 'state.name' => ['required', Rule::unique('users', 'name')->ignoreModel($this->model)], |
| 142 | + 'state.email' => ['required', Rule::unique('users', 'email')->ignoreModel($this->model)], |
| 143 | + ], |
| 144 | + [ |
| 145 | + 'state.name' => __('Name'), |
| 146 | + 'state.email' => __('Email'), |
| 147 | + ], |
| 148 | + ]; |
| 149 | + } |
| 150 | + |
| 151 | + /* |
| 152 | + * Tab Title |
| 153 | + */ |
| 154 | + public function title(): string |
| 155 | + { |
| 156 | + return __('General'); |
| 157 | + } |
| 158 | +} |
| 159 | +``` |
| 160 | + |
| 161 | +In Tab class, you can use livewire hooks example: |
| 162 | + |
| 163 | +```php |
| 164 | +use Vildanbina\LivewireTabs\Components\Tab; |
| 165 | + |
| 166 | +class General extends Tab |
| 167 | +{ |
| 168 | + public function onTabIn($name, $value) |
| 169 | + { |
| 170 | + // Something you want |
| 171 | + } |
| 172 | + |
| 173 | + public function onTabOut($name, $value) |
| 174 | + { |
| 175 | + // Something you want |
| 176 | + } |
| 177 | + |
| 178 | + public function updating($name, $value) |
| 179 | + { |
| 180 | + // Something you want |
| 181 | + } |
| 182 | + |
| 183 | + public function updatingState($name, $value) |
| 184 | + { |
| 185 | + // Something you want |
| 186 | + } |
| 187 | + |
| 188 | + public function updated($name, $value) |
| 189 | + { |
| 190 | + // Something you want |
| 191 | + } |
| 192 | + |
| 193 | + public function updatedState($name, $value) |
| 194 | + { |
| 195 | + // Something you want |
| 196 | + } |
| 197 | +} |
| 198 | +``` |
| 199 | + |
| 200 | +Each tab need to have view, you can pass view path in `$view` property. |
| 201 | + |
| 202 | +After create tab class, you need to put that tab to tabs form: |
| 203 | + |
| 204 | +```php |
| 205 | +<?php |
| 206 | + |
| 207 | +namespace App\Http\Livewire; |
| 208 | + |
| 209 | +use App\Tabs\General; |
| 210 | +use Vildanbina\LivewireTabs\TabsComponent; |
| 211 | + |
| 212 | +class UserTab extends TabsComponent |
| 213 | +{ |
| 214 | + public array $tabs = [ |
| 215 | + General::class, |
| 216 | + // Other tabs... |
| 217 | + ]; |
| 218 | + |
| 219 | + ... |
| 220 | +} |
| 221 | +``` |
| 222 | + |
| 223 | +## Building Tailwind CSS for production |
| 224 | + |
| 225 | +Because some classes are dynamically build and to compile js you should add some classes to the purge safelist so your `tailwind.config.js` should look something like this: |
| 226 | + |
| 227 | +```js |
| 228 | +module.exports = { |
| 229 | + content: [ |
| 230 | + "./resources/**/*.blade.php", |
| 231 | + "./resources/**/*.js", |
| 232 | + "./resources/**/*.vue", |
| 233 | + |
| 234 | + "./vendor/vildanbina/livewire-tabs/resources/views/*.blade.php", |
| 235 | + ] |
| 236 | +}; |
| 237 | + |
| 238 | +``` |
| 239 | + |
| 240 | +## Contributing |
| 241 | + |
| 242 | +Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details. |
| 243 | + |
| 244 | +## Security Vulnerabilities |
| 245 | + |
| 246 | +Please e-mail [email protected] to report any security vulnerabilities instead of the issue tracker. |
| 247 | + |
| 248 | +## Credits |
| 249 | + |
| 250 | +- [Vildan Bina](https://github.com/vildanbina) |
| 251 | +- [All Contributors](../../contributors) |
| 252 | + |
| 253 | +## License |
| 254 | + |
| 255 | +The MIT License (MIT). Please see [License File](LICENSE.md) for more information. |
0 commit comments