Skip to content

Commit

Permalink
NEW ListboxField react field schema
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewandante committed Jun 28, 2023
1 parent 4edc2fc commit 1af345f
Showing 1 changed file with 115 additions and 1 deletion.
116 changes: 115 additions & 1 deletion 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 All @@ -81,14 +86,17 @@ public function Field($properties = [])
*
* @return ArrayList
*/
public function getOptions()
public function getOptions($onlySelected = false)
{
// Loop through and figure out which values were selected.
$options = [];
$selectedValue = $this->getValueArray();
foreach ($this->getSource() as $itemValue => $title) {
$itemSelected = in_array($itemValue, $selectedValue ?? [])
|| in_array($itemValue, $this->getDefaultItems() ?? []);
if ($onlySelected && !$itemSelected) {
continue;
}
$itemDisabled = $this->isDisabled()
|| in_array($itemValue, $this->getDisabledItems() ?? []);
$options[] = new ArrayData([
Expand All @@ -101,6 +109,7 @@ public function getOptions()

$options = new ArrayList($options);
$this->extend('updateGetOptions', $options);

return $options;
}

Expand Down Expand Up @@ -158,4 +167,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(true);
$name = $this->getName();
$schema = array_merge(
parent::getSchemaDataDefaults(),
[
'name' => $name,
'lazyLoad' => false,
'creatable' => false,
'multi' => true,
'value' => $options->count() ? $options->toNestedArray() : null,
'disabled' => $this->isDisabled() || $this->isReadonly(),
]
);

$schema['options'] = array_values($this->getOptions()->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(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 getSchemaDataType(): string
{
return self::SCHEMA_DATA_TYPE_MULTISELECT;
}

public function getValueArray()
{
$value = $this->Value();

// sanity check the values
if (is_array($value) && count($value) > 0 ) {
$first = reset($value);
if (! is_integer($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);
}
}

0 comments on commit 1af345f

Please sign in to comment.