Skip to content

Commit

Permalink
Add option to control input reset on selection, close #112
Browse files Browse the repository at this point in the history
  • Loading branch information
mskocik committed May 30, 2022
1 parent c201708 commit e56e704
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ controlItem | Component | `Item` | Item component when item is
dropdownItem | Component | `Item` | Item component in dropdown. See [Custom Items](#custom-items) section for more details.
selectOnTab | bool | `false` | Allow selecting currently active item by <kbd>Tab</kbd> key
resetOnBlur | bool | `true` | Control if input value should be cleared on blur
resetOnSelect | bool | `true` | Control if input value should be cleared on item selection. **Note:** applicable only with `multiple`
clearable | bool | `false` | Display ✖ icon to clear whole selection
multiple | bool | `false` | allow multiselection. Will be set automatically to `true`, if `name` property ends with `[]`, like `tags[]`
max | number | `0` | Maximum allowed items selected, applicable only for multiselect
Expand Down
5 changes: 3 additions & 2 deletions component.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const OPTION_LIST = [
// basic
'value-field', 'label-field', 'disabled-field', 'placeholder',
// UI, UX
'searchable', 'clearable', 'renderer', 'disable-highlight', 'select-on-tab', 'reset-on-blur',
'searchable', 'clearable', 'renderer', 'disable-highlight', 'select-on-tab', 'reset-on-blur', 'reset-on-select',
// multiple
'multiple', 'max', 'collapse-selection',
// creating
Expand Down Expand Up @@ -51,6 +51,7 @@ function formatValue(name, value) {
case 'disable-highlight':
case 'select-on-tab':
case 'reset-on-blur':
case 'reset-on-select':
case 'multiple':
case 'collapse-selection':
case 'creatable':
Expand Down Expand Up @@ -237,7 +238,7 @@ class SvelecteElement extends HTMLElement {
}
}
};
const boolProps = ['searchable','clearable','disable-highlight', 'required', 'select-on-tab','reset-on-blur',
const boolProps = ['searchable','clearable','disable-highlight', 'required', 'select-on-tab','reset-on-blur','reset-on-select',
'multiple','collapse-selection','creatable','allow-editing','keep-created','fetch-reset-on-blur',
'virtual-list','disable-sifter','label-as-value', 'disabled'
].reduce((res, propName) => {
Expand Down
6 changes: 6 additions & 0 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,12 @@ <h4 id="options">Options</h4>
<td><code>true</code></td>
<td>Control if input value should be cleared on blur</td>
</tr>
<tr>
<td>resetOnSelect</td>
<td>bool</td>
<td><code>true</code></td>
<td>Control if input value should be cleared on item selection. <b>Note:</b> Applicable only with <code>multiple</code></td>
</tr>
<tr>
<td>clearable</td>
<td>bool</td>
Expand Down
2 changes: 2 additions & 0 deletions docs/src/09-custom-dependent.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -1376,6 +1376,8 @@ return Nette;
<el-svelecte options={`[{"id":"posts","label":"Posts", "prop": "Posts"},{"id":"users","label":"Users", "prop": "Users"},{"id":"comments","label":"Comments", "prop": "Comment"}]`}
style="margin-bottom: 0"
lazy-dropdown="false"
multiple
reset-on-select="false"
>
<select id="anchored" name="demo" multiple on:change={e => console.log(e.target.selectedOptions)}
data-nette-rules={netteRules}
Expand Down
3 changes: 2 additions & 1 deletion src/Svelecte.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
export let disableHighlight = false;
export let selectOnTab = defaults.selectOnTab;
export let resetOnBlur = defaults.resetOnBlur;
export let resetOnSelect = defaults.resetOnSelect;
export let dndzone = () => ({ noop: true, destroy: () => {}});
export let validatorAction = null;
export let dropdownItem = Item;
Expand Down Expand Up @@ -431,7 +432,7 @@
if (disabled || opt[disabledField] || opt.$isGroupHeader) return;
selectOption(opt);
$inputValue = '';
if ((multiple && resetOnSelect) || !multiple) $inputValue = '';
if (!multiple) {
$hasDropdownOpened = false;
} else {
Expand Down
1 change: 1 addition & 0 deletions src/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const settings = {
clearable: false,
selectOnTab: false,
resetOnBlur: true,
resetOnSelect: true,
fetchResetOnBlur: true,
// multi
multiple: false,
Expand Down

0 comments on commit e56e704

Please sign in to comment.