Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added attributes, major refactoring #18

Merged
merged 1 commit into from
Jul 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions resources/views/fields/checkbox.blade.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<div>
@if($field->isMultiple())
@if($field->isGroup())
@include('moonshine::fields.multi-checkbox', [
'field' => $field,
'item' => $item,
'resource' => $resource
])
@else
@include('moonshine::fields.shared.checkbox', [
'meta' => $field->meta(),
'attributes' => $field->attributes(),
'id' => $field->id(),
'name' => $field->name(),
'value' => $field->formViewValue($item),
Expand Down
4 changes: 2 additions & 2 deletions resources/views/fields/has-many.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
'field' => $field,
'resource' => $resource,
'item' => $item,
'model' => $field->formViewValue($item)->first() ?? $item->{$field->relation()}()->getRelated(),
'model' => $field->formViewValue($item)->first() ?? $field->getRelated($item),
'level' => $level ?? 0
])
])
4 changes: 2 additions & 2 deletions resources/views/fields/has-one.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
'field' => $field,
'resource' => $resource,
'item' => $item,
'model' => $field->formViewValue($item) ?? $resource->getModel(),
'model' => $field->formViewValue($item) ?? $field->getRelated($item),
'level' => $level ?? 0
])
])
12 changes: 2 additions & 10 deletions resources/views/fields/input.blade.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
<input {!! $field->meta() ?? '' !!}
@if($field->getMask()) x-mask="{{ $field->getMask() }}" @endif
<input {{ $field->attributes()->merge(['class' => 'text-black dark:text-white bg-white dark:bg-darkblue focus:outline-none focus:shadow-outline border border-gray-300 rounded-lg py-2 px-4 block w-full appearance-none leading-normal']) }}
id="{{ $field->id() }}"
aria-label="{{ $field->label() ?? '' }}"
placeholder="{{ $field->label() ?? '' }}"
name="{{ $field->name() }}"
type="{{ $field->type() }}"
min="{{ $field->getAttribute('min') ?? 0 }}"
max="{{ $field->getAttribute('max') ?? 100000 }}"
step="{{ $field->getAttribute('step') ?? 1 }}"
class="{{ $field->getAttribute('class') ?? "text-black dark:text-white bg-white dark:bg-darkblue focus:outline-none focus:shadow-outline border border-gray-300 rounded-lg py-2 px-4 block w-full appearance-none leading-normal" }}"

{{ $field->isRequired() ? "required" : "" }}
{{ $field->isMultiple() ? "multiple" : "" }}
{{ $field->isDisabled() ? "disabled" : "" }}
{{ $field->isReadonly() ? "readonly" : "" }}

@if($field->getAutocomplete()) autocomplete="{{ $field->getAutocomplete() }}" @endif
@if($field->type() !== 'file')
@if(!$field->isFile())
@if(isset($valueKey))
value="{!! is_array($field->formViewValue($item)) ? ($field->formViewValue($item)[$valueKey] ?? '') : '' !!}"
@else
Expand Down
3 changes: 1 addition & 2 deletions resources/views/fields/multi-select.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
<select
x-model="{{ $field->isMultiple() ? 'selectValues' : 'selectValue' }}"
x-ref="multi_select_{{ $field->id() }}"
{!! $field->meta() ?? '' !!}
{{ $field->attributes() }}
id="{{ $field->id() }}"
name="{{ $field->name() }}"
{{ $field->isRequired() ? "required" : "" }}
{{ $field->isMultiple() ? "multiple" : "" }}
style="display: none;"
>
@if(!$field->isMultiple() && $field->isNullable())
Expand Down
6 changes: 2 additions & 4 deletions resources/views/fields/select.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
])
@else
<select
{!! $field->meta() ?? '' !!}
{{ $field->attributes()->merge(['class' => 'text-black dark:text-white bg-white dark:bg-darkblue block appearance-none w-full border border-gray-200 py-3 px-4 pr-8 rounded leading-tight focus:outline-none focus:border-gray-500']) }}
id="{{ $field->id() }}"
name="{{ $field->name() }}"
{{ $field->isRequired() ? "required" : "" }}
{{ $field->isMultiple() ? "multiple" : "" }}
class="text-black dark:text-white bg-white dark:bg-darkblue block appearance-none w-full border border-gray-200 py-3 px-4 pr-8 rounded leading-tight focus:outline-none focus:border-gray-500"
>
@if($field->isNullable())
<option @selected(!$field->formViewValue($item)) value="">-</option>
Expand All @@ -23,4 +21,4 @@ class="text-black dark:text-white bg-white dark:bg-darkblue block appearance-non
</option>
@endforeach
</select>
@endif
@endif
2 changes: 1 addition & 1 deletion resources/views/fields/shared/checkbox.blade.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div>
<input {!! $meta ?? '' !!}
<input {{ $attributes }}
id="{{ $id }}"
type="checkbox"
name="{{ $name }}"
Expand Down
6 changes: 3 additions & 3 deletions resources/views/fields/shared/full-fields.blade.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div x-data="handler_{{ $field->id() }}()" x-init="handler_init_{{ $field->id() }}">
<template x-for="(item, index{{ $level }}) in items" :key="index{{ $level }}">
<div :data-id="item.id" class="full_fields_{{ $field->id() }}">
@if(!method_exists($field, 'isRelationToOne') || !$field->isRelationToOne())
@if(!$field->toOne())
<div class="font-bold text-purple my-4" x-text="index{{ $level }} + 1"></div>
@endif

Expand All @@ -11,7 +11,7 @@
</x-moonshine::field-container>
@endforeach

@if(!method_exists($field, 'isRelationToOne') || !$field->isRelationToOne())
@if(!$field->toOne())
<div class="my-4">
@if($field->isRemovable())
<button @click="removeField(index{{ $level }})" type="button" class="text-pink hover:text-pink inline-block">
Expand All @@ -24,7 +24,7 @@
</template>

<div class="my-4">
@if(!method_exists($field, 'isRelationToOne') || !$field->isRelationToOne())
@if(!$field->toOne())
<button type="button"
class="bg-gradient-to-r from-purple to-pink text-white
text-white font-semibold py-2 px-4 rounded"
Expand Down
12 changes: 6 additions & 6 deletions resources/views/fields/shared/table-fields.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<table class="min-w-full">
<thead class="bg-whiteblue dark:bg-purple">
<tr>
@if(!method_exists($field, 'isRelationToOne') || !$field->isRelationToOne())
@if(!$field->toOne())
<th class="px-6 py-3 text-left text-xs leading-4 font-medium uppercase tracking-wider">#</th>
@endif

Expand All @@ -15,7 +15,7 @@
</th>
@endforeach

@if(!method_exists($field, 'isRelationToOne') || !$field->isRelationToOne())
@if(!$field->toOne())
<th class="px-6 py-3 text-left text-xs leading-4 font-medium uppercase tracking-wider"></th>
@endif
</tr>
Expand All @@ -25,7 +25,7 @@
<template x-for="(item, index{{ $level }}) in items" :key="index{{ $level }}"
>
<tr :data-id="item.id" class="table_fields_{{ $field->id() }}">
@if(!method_exists($field, 'isRelationToOne') || !$field->isRelationToOne())
@if(!$field->toOne())
<td class="px-6 py-4 whitespace-no-wrap" x-text="index{{ $level }} + 1"></td>
@endif

Expand All @@ -35,7 +35,7 @@
</td>
@endforeach

@if(!method_exists($field, 'isRelationToOne') || !$field->isRelationToOne())
@if(!$field->toOne())
<td class="px-6 py-4 whitespace-no-wrap">
@if($field->isRemovable())
<button @click="removeField(index{{ $level }})" type="button" class="text-pink hover:text-pink inline-block">
Expand All @@ -51,7 +51,7 @@
<tfoot class="bg-whiteblue dark:bg-purple">
<tr>
<td colspan="{{ count($field->getFields())+2 }}" class="px-6 py-4 whitespace-no-wrap">
@if(!method_exists($field, 'isRelationToOne') || !$field->isRelationToOne())
@if(!$field->toOne())
<button type="button"
class="bg-gradient-to-r from-purple to-pink text-white
text-white font-semibold py-2 px-4 rounded"
Expand Down Expand Up @@ -80,7 +80,7 @@ class="bg-gradient-to-r from-purple to-pink text-white
<script>
function handler_{{ $field->id() }}() {
return {
handler_init_{{ $field->id() }} () {
handler_init_{{ $field->id() }}() {
this.items = @json($field->jsonValues($item));
},
items: [],
Expand Down
7 changes: 2 additions & 5 deletions resources/views/fields/textarea.blade.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
<textarea {!! $field->meta() ?? '' !!}
<textarea {{ $field->attributes()->merge(['class' => 'text-black dark:text-white bg-white dark:bg-darkblue focus:outline-none focus:shadow-outline border border-gray-300 rounded-lg py-2 px-4 block w-full appearance-none leading-normal']) }}
id="{{ $field->id() }}"
aria-label="{{ $field->label() ?? '' }}"
placeholder="{{ $field->label() ?? '' }}"
name="{{ $field->name() }}"
class="{{ $field->getAttribute('class') ?? "text-black dark:text-white bg-white dark:bg-darkblue focus:outline-none focus:shadow-outline border border-gray-300 rounded-lg py-2 px-4 block w-full appearance-none leading-normal" }}"

{{ $field->isRequired() ? "required" : "" }}
{{ $field->isDisabled() ? "disabled" : "" }}
{{ $field->isReadonly() ? "readonly" : "" }}

@if($field->getAutocomplete()) autocomplete="{{ $field->getAutocomplete() }}" @endif
>{!! $field->formViewValue($item) ?? '' !!}</textarea>
>{!! $field->formViewValue($item) ?? '' !!}</textarea>
10 changes: 0 additions & 10 deletions src/Contracts/Fields/HasRelationshipContract.php

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Leeto\MoonShine\Contracts\Fields\Relationships;

interface BelongsToRelationshipContract
{
}
12 changes: 12 additions & 0 deletions src/Contracts/Fields/Relationships/HasRelationshipContract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Leeto\MoonShine\Contracts\Fields\Relationships;

use Illuminate\Database\Eloquent\Model;

interface HasRelationshipContract
{
public function relatedValues(Model $item): array;

public function values(): array;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Leeto\MoonShine\Contracts\Fields\Relationships;

interface ManyToManyRelationshipContract
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Leeto\MoonShine\Contracts\Fields\Relationships;

interface OneToManyRelationshipContract
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Leeto\MoonShine\Contracts\Fields\Relationships;

interface OneToOneRelationshipContract
{
}
15 changes: 7 additions & 8 deletions src/Fields/BelongsTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
namespace Leeto\MoonShine\Fields;

use Illuminate\Database\Eloquent\Model;
use Leeto\MoonShine\Contracts\Fields\HasRelationshipContract;
use Leeto\MoonShine\Traits\Fields\WithRelationshipsTrait;
use Leeto\MoonShine\Traits\Fields\SearchableTrait;
use Leeto\MoonShine\Contracts\Fields\Relationships\HasRelationshipContract;
use Leeto\MoonShine\Contracts\Fields\Relationships\BelongsToRelationshipContract;
use Leeto\MoonShine\Traits\Fields\WithRelationship;
use Leeto\MoonShine\Traits\Fields\Searchable;

class BelongsTo extends Field implements HasRelationshipContract
class BelongsTo extends Field implements HasRelationshipContract, BelongsToRelationshipContract
{
use WithRelationshipsTrait, SearchableTrait;

protected static bool $toOne = true;
use Searchable, WithRelationship;

protected static string $view = 'select';

Expand All @@ -20,4 +19,4 @@ public function save(Model $item): Model
return $item->{$this->relation()}()
->associate($this->requestValue());
}
}
}
Loading