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

Add select component #52

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft

Add select component #52

wants to merge 7 commits into from

Conversation

Ufooo
Copy link

@Ufooo Ufooo commented Sep 4, 2020

No description provided.

@driesvints
Copy link
Member

I don't have time to look at the PR atm but hopefully soon. In the meantime can you rebase and solve the merge conflicts?

@Ufooo
Copy link
Author

Ufooo commented Sep 10, 2020

it's done

config/blade-ui-kit.php Outdated Show resolved Hide resolved
Copy link
Author

@Ufooo Ufooo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's done

config/blade-ui-kit.php Outdated Show resolved Hide resolved
Copy link
Author

@Ufooo Ufooo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

@freekmurze
Copy link

freekmurze commented Sep 15, 2020

TL;DR: Let's optimise this component for elequent collections instead of arrays, since you'll likely work much more with eloquent collections in a typical Laravel app.

Right now, this component accepts simple arrays. The keys will be used as values in the html, the values in the array will be used as label in the html.

Imagine you want to create a select element with let's say all departments in your app. Departments are stored in the db. In your controller you might retrieve them like this.

Department::all()

In order to pass the models to the view component you'd have to perform a map operation.

$departments = Department::all()->mapWithKey(function(Department $department) {
    return [$department->id => $department->name]
});

Having to do this will grow old pretty fast.

I propose to change this component so that it accepts an iterable instead of an only array, so a Collection gets accepted as well. By default the id attribute could be used as value in the html and the name attribute as the label in the html.

This way you don't need to do any transformation and this would just work.

<x-select :values="$departments" />

Optionally, value-attribute and name-attribute could be accepted to override the id and name defaults.

This is just a rough sketch, I hope you get the idea. Alternatively a second Blade component could be introduced (could be named x-select- collection.

@LonnyX
Copy link

LonnyX commented Sep 23, 2020

@driesvints can we merge this? It sounds like a miss on the package :/

@driesvints
Copy link
Member

@LonnyX sorry I haven't gotten to this yet. I've been a bit swamped the last couple of weeks so I'm running behind on issues/prs on Blade UI Kit. I'm hoping to get through them somewhere soon although it's hard to say when exactly.

I also want to think out this particularly PR a bit better before merging like Freek suggests.

@nasrulhazim
Copy link

@Ufooo , if i make it as multiple selection, i won't be able to set selected values.

@Ufooo
Copy link
Author

Ufooo commented Sep 30, 2020

@freekmurze did you think so? :)

@freekmurze
Copy link

@Ufooo Nice!

@riseno
Copy link

riseno commented Oct 21, 2020

when will this PR be merged?

@danharrin
Copy link

I think we need to make some edits, @Ufooo are you willing to still maintain this PR or would you like me to fork and make the changes on my own?

id="{{ $id }}"
{{ $attributes }}
>
@if(!$slot->isEmpty())

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@if(!$slot->isEmpty())
@if(! $slot->isEmpty())

$this->placeholder = $placeholder;

if ($options instanceof Collection) {
$this->options = $options->mapWithKeys(function ($option) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can refactor this mapWithKeys() expression to use pluck()

Suggested change
$this->options = $options->mapWithKeys(function ($option) {
$this->options = $options->pluck($this->nameAttribute, $this->valueAttribute)

return in_array($value, $selected);
}

return ($value === $selected);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return ($value === $selected);
return $value === $selected;


public function isSelected($value): bool
{
$selected = old($this->name, $this->selected);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the purpose of the old() call here?

/** @var string */
public $placeholder;

public function __construct(string $name, $options, string $placeholder = '', string $nameAttribute = 'name', string $valueAttribute = 'id', $id = null, $selected = null)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that a more appropriate name for $nameAttribute is probably $labelAttribute - each option has a (display) label and value. Thoughts?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$labelAttribute suits better imo

@danharrin danharrin marked this pull request as draft February 2, 2022 13:48
@howdu
Copy link

howdu commented Mar 26, 2022

Until this gets merged laravel-form-components has a complete selection of form components.

@riseno
Copy link

riseno commented May 23, 2022

when will this PR be merged?

well, am gonna leave a footprint for this PR almost 20 months after.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants