Skip to content

Commit

Permalink
Add ability to remove input text in Select and Relation fields
Browse files Browse the repository at this point in the history
  • Loading branch information
grigoriy-ivanov committed Jan 28, 2024
1 parent d83bab1 commit 0e85da6
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 3 deletions.
14 changes: 11 additions & 3 deletions resources/js/controllers/relation_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,22 @@ export default class extends ApplicationController {
valueField: 'value',
labelField: 'label',
searchField: [],
sortField: [{field:'$order'},{field:'$score'}],
sortField: [{field: '$order'}, {field: '$score'}],
render: {
option_create: (data, escape) => `<div class="create">${this.data.get('message-add')} <strong>${escape(data.input)}</strong>&hellip;</div>`,
no_results: () => `<div class="no-results">${this.data.get('message-notfound')}</div>`,
},
onDelete: () => !! this.data.get('allow-empty'),
onDelete: () => !!this.data.get('allow-empty'),
load: (query, callback) => this.search(query, callback),
});

if (select.hasAttribute('multiple') && this.data.get('clear-input')) {
this.choices.on('item_add', function () {
this.setTextboxValue('');
this.refreshOptions(false);
})
}

}

/**
Expand Down Expand Up @@ -70,7 +78,7 @@ export default class extends ApplicationController {
Object.entries(response.data).forEach((entry) => {
const [value, label] = entry;

options.push({ label, value });
options.push({label, value});
});

this.choices.clearOptions();
Expand Down
7 changes: 7 additions & 0 deletions resources/js/controllers/select_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ export default class extends ApplicationController {
},
onDelete: () => !! this.data.get('allow-empty'),
});

if (select.hasAttribute('multiple') && this.data.get('clear-input')) {
this.choices.on('item_add', function () {
this.setTextboxValue('');
this.refreshOptions(false);
})
}
}

/**
Expand Down
1 change: 1 addition & 0 deletions resources/views/fields/relation.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
data-relation-append="{{ $relationAppend }}"
data-relation-chunk="{{ $chunk }}"
data-relation-allow-empty="{{ $allowEmpty }}"
data-relation-clear-input="{{ $clearInput }}"
data-relation-route="{{ route('platform.systems.relation') }}"
data-relation-message-notfound="{{ __('No results found') }}"
data-relation-message-add="{{ __('Add') }}"
Expand Down
1 change: 1 addition & 0 deletions resources/views/fields/select.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
data-select-placeholder="{{$attributes['placeholder'] ?? ''}}"
data-select-allow-empty="{{ $allowEmpty }}"
data-select-message-notfound="{{ __('No results found') }}"
data-select-clear-input="{{ $clearInput }}"
data-select-allow-add="{{ var_export($allowAdd, true) }}"
data-select-message-add="{{ __('Add') }}"
>
Expand Down
10 changes: 10 additions & 0 deletions src/Screen/Concerns/Multipliable.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,14 @@ public function multiple(): self
$this->set('name', Str::finish($name, '[]'));
});
}

/**
* Remove entered text after add item
*
* @return $this
*/
public function clearInput(bool $value = true): self
{
return $this->set('clearInput', $value);
}
}
11 changes: 11 additions & 0 deletions src/Screen/Fields/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class Relation extends Field
'chunk' => 10,
'allowEmpty' => '',
'allowAdd' => false,
'clearInput' => false,
];

/**
Expand Down Expand Up @@ -245,6 +246,16 @@ public function allowEmpty(bool $value = true)
return $this->set('allowEmpty', $value);
}

/**
* Remove entered text after add item
*
* @return $this
*/
public function clearInput(bool $value = true): self
{
return $this->set('clearInput', $value);
}

/**
* Allow empty value to be set
*
Expand Down
2 changes: 2 additions & 0 deletions src/Screen/Fields/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class Select extends Field implements ComplexFieldConcern
'allowEmpty' => '',
'allowAdd' => false,
'isOptionList' => false,
'clearInput' => false,

];

/**
Expand Down

0 comments on commit 0e85da6

Please sign in to comment.