Skip to content

Commit

Permalink
added file element
Browse files Browse the repository at this point in the history
  • Loading branch information
lukesnowden committed Oct 29, 2018
1 parent b8d533d commit 56b550e
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 1 deletion.
35 changes: 35 additions & 0 deletions app/Libs/Elements/File.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Laraview\Libs\Elements;

use Laraview\Console\Commands\LaraviewGenerateElement;
use Laraview\Libs\Blueprints\ElementBlueprint;
use Laraview\Libs\Elements\Generate\InputGeneration;
use Laraview\Libs\Elements\Traits\Formats\FileBootstrap;

abstract class File extends Text implements ElementBlueprint
{

use FileBootstrap;

/**
* @return mixed|null|string
*/
public static function humanReadableName()
{
return 'File Element';
}

/**
* @param $region
* @param LaraviewGenerateElement $console
* @return mixed
*/
public static function generate( $region, LaraviewGenerateElement $console )
{
$generator = new InputGeneration( $region, $console );
$generator->setStubPath( __DIR__ . '/../../../stubs/elements/file.stub' );
return $generator->create();
}

}
29 changes: 29 additions & 0 deletions app/Libs/Elements/Traits/Formats/FileBootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Laraview\Libs\Elements\Traits\Formats;

trait FileBootstrap
{

/**
* @return string
*/
protected function element()
{
return sprintf( '<input type="file" name="%s" %s value="%s" />',
$this->name,
$this->attributes(),
"{{ \${$this->valueKeyName()} }}"
);
}

/**
* @return void
*/
protected function preRender()
{
parent::preRender();
$this->attributes[ 'class' ] = trim( ( isset( $this->attributes[ 'class' ] ) ? $this->attributes[ 'class' ] : '' ) . ' form-control' );
}

}
4 changes: 3 additions & 1 deletion app/Libs/Register.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Laraview\Libs\Blueprints\ViewBlueprint;
use Laraview\Libs\Blueprints\RegisterBlueprint;
use Laraview\Libs\Elements\Checkbox;
use Laraview\Libs\Elements\File;
use Laraview\Libs\Elements\Number;
use Laraview\Libs\Elements\Time;
use Laraview\Libs\Elements\Date;
Expand Down Expand Up @@ -55,6 +56,7 @@ class Register implements RegisterBlueprint
Textarea::class,
Date::class,
Time::class,
File::class,
];

/**
Expand All @@ -63,7 +65,7 @@ class Register implements RegisterBlueprint
protected $registeredLayouts = [
Tabs::class,
Table::class,
Modal::class
Modal::class,
];

/**
Expand Down
48 changes: 48 additions & 0 deletions stubs/elements/file.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace [NAMESPACE];

use Laraview\Libs\Blueprints\ElementBlueprint;
use Laraview\Libs\Elements\File;

class [CLASS_NAME] extends File implements ElementBlueprint
{

/**
* Name of input
* @var string
*/
protected $name = '[NAME]';

/**
* Label text
* @var string
*/
protected $label = '[LABEL]';

/**
* Input elements HTML attributes
* @var array
*/
protected $attributes = [
[ATTRIBUTES]
];

/**
* @return mixed
*/
public function value()
{
return old( $this->name ) ?: $this->data( "model.{$this->name}" );
}

/**
* @param $model
* @param $request
*/
public function receivePayload( $model, $request )
{
$model->{$this->name} = $request->input( $this->name );
}

}

0 comments on commit 56b550e

Please sign in to comment.