Skip to content

Commit d259c96

Browse files
committed
fix: only move dropdown on init
This fixes also the select issue with the label placement. The impact is not tested, but in that case we should offer a relocate function to move the dropdown manually.
1 parent 1127482 commit d259c96

File tree

4 files changed

+27
-37
lines changed

4 files changed

+27
-37
lines changed

sass/components/forms/_input-fields.scss

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,18 @@ textarea.materialize-textarea {
2323
width: 100%;
2424
font-size: $md_sys_typescale_body-large_size; //16px; // => 16 dp
2525
height: 56px; // 56dp
26+
background-color: transparent;
2627
}
2728

2829
%invalid-input-style {
2930
border-bottom: 2px solid var(--md-sys-color-error);
3031
box-shadow: 0 1px 0 0 var(--md-sys-color-error);
3132
}
33+
3234
%hidden-text > span {
3335
display: none
3436
}
37+
3538
%custom-error-message {
3639
content: attr(data-error);
3740
color: var(--md-sys-color-error);
@@ -47,12 +50,9 @@ textarea.materialize-textarea {
4750

4851
input, textarea {
4952
box-sizing: border-box; /* https://stackoverflow.com/questions/1377719/padding-within-inputs-breaks-width-100*/
50-
5153
padding: 0 16px;
5254
padding-top: 20px;
53-
54-
background-color: var(--md-sys-color-surface);
55-
55+
//background-color: transparent; //background-color: var(--md-sys-color-surface);
5656
border: none; // reset
5757
border-radius: 4px; // md.sys.shape.corner.extra-small.top
5858
border-bottom: 1px solid var(--md-sys-color-on-surface-variant);
@@ -173,13 +173,14 @@ textarea.materialize-textarea {
173173

174174
// Outlined
175175

176+
// todo: use this approach for floating labels with outline
177+
// https://dev.to/icyjoseph/use-more-html-elements-floating-from-field-37o9
178+
176179
&.outlined {
177180

178181
input, textarea {
179182
padding-top: 0;
180-
181-
background-color: var(--md-sys-color-background);
182-
183+
background-color: transparent; //(--md-sys-color-background);
183184
border: 1px solid var(--md-sys-color-on-surface-variant);
184185
border-radius: 4px; // md.sys.shape.corner.extra-small
185186

@@ -189,6 +190,7 @@ textarea.materialize-textarea {
189190
margin-left: -1px; // subtract border-width
190191

191192
}
193+
192194
// Label
193195
&:focus:not([readonly]) + label {
194196
color: var(--input-color);
@@ -200,14 +202,13 @@ textarea.materialize-textarea {
200202
left: 16px;
201203
margin-left: -4px;
202204
padding: 0 4px;
203-
background-color: var(--md-sys-color-background);
205+
background-color: transparent; //var(--md-sys-color-background);
204206
}
205207

206208
&:disabled, &[readonly="readonly"] {
207209
color: rgba(var(--md_sys_color_on-surface), 0.38);
208210
border-color: rgba(var(--md_sys_color_on-surface), 0.12);
209211
}
210-
211212
}
212213
}
213214

@@ -238,7 +239,6 @@ textarea.materialize-textarea {
238239
.searchbar {
239240
.prefix {
240241
position: absolute;
241-
//left: 12px;
242242
padding-left: 1rem;
243243
top: 0;
244244
user-select: none;
@@ -307,7 +307,6 @@ textarea {
307307
word-wrap: break-word;
308308
overflow-wrap: break-word; /* future version of deprecated 'word-wrap' */
309309
padding-top: 1.2rem; /* prevents text jump on Enter keypress */
310-
311310
// Reduces repaints
312311
position: absolute;
313312
top: 0;

src/dropdown.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ export class Dropdown extends Component<DropdownOptions> implements Openable {
128128
this.el.ariaExpanded = 'false';
129129

130130
// Move dropdown-content after dropdown-trigger
131-
this._moveDropdown();
131+
this._moveDropdownToElement();
132132
this._makeDropdownFocusable();
133133
this._setupEventHandlers();
134134
}
@@ -216,7 +216,7 @@ export class Dropdown extends Component<DropdownOptions> implements Openable {
216216

217217
_handleClick = (e: MouseEvent) => {
218218
e.preventDefault();
219-
this._moveDropdown((<HTMLElement>e.target).closest('li'));
219+
//this._moveDropdown((<HTMLElement>e.target).closest('li'));
220220
if (this.isOpen) {
221221
this.close();
222222
} else {
@@ -225,7 +225,7 @@ export class Dropdown extends Component<DropdownOptions> implements Openable {
225225
};
226226

227227
_handleMouseEnter = (e) => {
228-
this._moveDropdown((<HTMLElement>e.target).closest('li'));
228+
//this._moveDropdown((<HTMLElement>e.target).closest('li'));
229229
this.open();
230230
};
231231

@@ -388,17 +388,16 @@ export class Dropdown extends Component<DropdownOptions> implements Openable {
388388
this.dropdownEl.style.transformOrigin = '';
389389
}
390390

391-
// Move dropdown after container or trigger
392-
_moveDropdown(containerEl: HTMLElement = null) {
393-
if (!!this.options.container) {
391+
_moveDropdownToElement(containerEl: HTMLElement = null) {
392+
if (this.options.container) {
394393
this.options.container.append(this.dropdownEl);
395-
} else if (containerEl) {
396-
if (!containerEl.contains(this.dropdownEl)) {
397-
containerEl.append(this.dropdownEl);
398-
}
399-
} else {
400-
this.el.after(this.dropdownEl);
394+
return;
395+
}
396+
if (containerEl) {
397+
if (!containerEl.contains(this.dropdownEl)) containerEl.append(this.dropdownEl);
398+
return;
401399
}
400+
this.el.after(this.dropdownEl);
402401
}
403402

404403
_makeDropdownFocusable() {
@@ -584,7 +583,7 @@ export class Dropdown extends Component<DropdownOptions> implements Openable {
584583
if (getComputedStyle(closestOverflowParent).position === 'static')
585584
closestOverflowParent.style.position = 'relative';
586585

587-
this._moveDropdown(closestOverflowParent);
586+
//this._moveDropdown(closestOverflowParent);
588587

589588
// Set width before calculating positionInfo
590589
const idealWidth = this.options.constrainWidth

src/forms.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export class Forms {
106106
// So we set the height to the original one
107107
textarea.style.height = originalHeight + 'px';
108108
}
109-
textarea.setAttribute('previous-length', textarea.value.length.toString());
109+
textarea.setAttribute('previous-length', (textarea.value || '').length.toString());
110110
}
111111

112112
static Init() {
@@ -158,9 +158,8 @@ export class Forms {
158158
static InitTextarea(textarea: HTMLTextAreaElement) {
159159
// Save Data in Element
160160
textarea.setAttribute('original-height', textarea.getBoundingClientRect().height.toString());
161-
textarea.setAttribute('previous-length', textarea.value.length.toString());
161+
textarea.setAttribute('previous-length', (textarea.value || '').length.toString());
162162
Forms.textareaAutoResize(textarea);
163-
164163
textarea.addEventListener('keyup', (e) => Forms.textareaAutoResize(e.target));
165164
textarea.addEventListener('keydown', (e) => Forms.textareaAutoResize(e.target));
166165
}

src/select.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,6 @@ export class FormSelect extends Component<FormSelectOptions> {
180180

181181
_setupDropdown() {
182182
this.labelEl = document.querySelector('[for="' + this.el.id + '"]');
183-
if (this.labelEl) this.labelEl.style.display = 'none';
184183

185184
this.wrapper = document.createElement('div');
186185
this.wrapper.classList.add('select-wrapper', 'input-field');
@@ -291,7 +290,7 @@ export class FormSelect extends Component<FormSelectOptions> {
291290

292291
// Initialize dropdown
293292
if (!this.el.disabled) {
294-
const dropdownOptions = { ...this.options.dropdownOptions }; // TODO:
293+
const dropdownOptions = { ...this.options.dropdownOptions };
295294
dropdownOptions.coverTrigger = false;
296295
const userOnOpenEnd = dropdownOptions.onOpenEnd;
297296
const userOnCloseEnd = dropdownOptions.onCloseEnd;
@@ -333,14 +332,8 @@ export class FormSelect extends Component<FormSelectOptions> {
333332
}
334333
// Add initial selections
335334
this._setSelectedStates();
336-
337-
// Add Label
338-
if (this.labelEl) {
339-
const label = document.createElement('label');
340-
label.htmlFor = this.input.id;
341-
label.innerText = this.labelEl.innerText;
342-
this.input.after(label);
343-
}
335+
// move label
336+
if (this.labelEl) this.input.after(this.labelEl);
344337
}
345338

346339
_addOptionToValues(realOption: HTMLOptionElement, virtualOption: HTMLElement) {

0 commit comments

Comments
 (0)