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

NEW ListboxField react field schema #10842

Merged
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
111 changes: 111 additions & 0 deletions src/Forms/ListboxField.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ class ListboxField extends MultiSelectField
*/
protected $disabledItems = [];


protected $schemaComponent = 'ListboxField';

/**
* Creates a new dropdown field.
*
Expand All @@ -58,6 +61,8 @@ public function __construct($name, $title = '', $source = [], $value = null, $si
$this->setSize($size);
}

$this->addExtraClass('ss-listbox-field');

parent::__construct($name, $title, $source, $value);
}

Expand Down Expand Up @@ -101,6 +106,7 @@ public function getOptions()

$options = new ArrayList($options);
$this->extend('updateGetOptions', $options);
GuySartorelli marked this conversation as resolved.
Show resolved Hide resolved

return $options;
}

Expand Down Expand Up @@ -158,4 +164,109 @@ public function getDisabledItems()
{
return $this->disabledItems;
}

/**
* Provide ListboxField data to the JSON schema for the frontend component
*
* @return array
*/
public function getSchemaDataDefaults()
{
$options = $this->getOptions();
$selected = $options->filter('Selected', true);
$name = $this->getName();
$schema = array_merge(
parent::getSchemaDataDefaults(),
[
'name' => $name,
'lazyLoad' => false,
'creatable' => false,
'multi' => true,
'value' => $selected->count() ? $selected->toNestedArray() : null,
'disabled' => $this->isDisabled() || $this->isReadonly(),
]
);

$schema['options'] = array_values($options->toNestedArray() ?? []);

return $schema;
}

public function getSchemaStateDefaults()
{
$data = parent::getSchemaStateDefaults();

// Add options to 'data'
$data['lazyLoad'] = false;
$data['multi'] = true;
$data['creatable'] = false;
$options = $this->getOptions()->filter('Selected', true);
$data['value'] = $options->count() ? $options->toNestedArray() : null;

return $data;
}

/**
* Returns array of arrays representing tags.
*
* @param string $term
* @return array
*/
protected function getOptionsArray($term)
{
$source = $this->getSourceList();
if (!$source) {
return [];
}

$titleField = $this->getTitleField();

$query = $source
->filter($titleField . ':PartialMatch:nocase', $term)
->sort($titleField);

// Map into a distinct list
$items = [];
$titleField = $this->getTitleField();

foreach ($query->map('ID', $titleField)->values() as $title) {
$items[$title] = [
'Title' => $title,
'Value' => $title,
];
}

return array_values($items ?? []);
}

public function getValueArray()
{
$value = $this->Value();
$validValues = $this->getValidValues();
if (empty($validValues)) {
return [];
}

$canary = reset($validValues);
if (is_array($value) && count($value) > 0) {
$first = reset($value);
// sanity check the values - make sure strings get strings, ints get ints etc
if (gettype($canary) !== gettype($first)) {
$replaced = [];
foreach ($value as $item) {
if (!is_array($item)) {
$item = json_decode($item, true);
}

if (isset($item['Value'])) {
$replaced[] = $item['Value'];
}
}

$value = $replaced;
}
}

return $this->getListValues($value);
}
}