Skip to content

Commit d2f4e3c

Browse files
daKmoRJoren Broekema
authored andcommitted
fix(field): sync name down instead of delegating
1 parent 07eddb3 commit d2f4e3c

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

packages/field/src/LionField.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ export class LionField extends FormControlMixin(
4747
return {
4848
...super.delegations,
4949
target: () => this.inputElement,
50-
properties: [...super.delegations.properties, 'name', 'type'],
51-
attributes: [...super.delegations.attributes, 'name', 'type'],
50+
properties: [...super.delegations.properties, 'type'],
51+
attributes: [...super.delegations.attributes, 'type'],
5252
};
5353
}
5454

@@ -58,6 +58,10 @@ export class LionField extends FormControlMixin(
5858
// make sure validation can be triggered based on observer
5959
type: Boolean,
6060
},
61+
name: {
62+
type: String,
63+
reflect: true,
64+
},
6165
};
6266
}
6367

@@ -104,6 +108,12 @@ export class LionField extends FormControlMixin(
104108
return (this.inputElement && this.inputElement.value) || '';
105109
}
106110

111+
constructor() {
112+
super();
113+
this.name = '';
114+
this.submitted = false;
115+
}
116+
107117
connectedCallback() {
108118
super.connectedCallback();
109119

@@ -138,6 +148,10 @@ export class LionField extends FormControlMixin(
138148
this.classList.remove('state-disabled'); // eslint-disable-line wc/no-self-class
139149
}
140150
}
151+
152+
if (changedProps.has('name')) {
153+
this.inputElement.name = this.name;
154+
}
141155
}
142156

143157
/**

packages/field/test/lion-field.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,18 @@ describe('<lion-field>', () => {
116116
expect(el.$$slot('input').value).to.equal('one');
117117
});
118118

119+
it('has a name which is reflected to an attribute and is synced down to the native input', async () => {
120+
const el = await fixture(`<${tagString}>${inputSlotString}</${tagString}>`);
121+
expect(el.name).to.equal('');
122+
expect(el.getAttribute('name')).to.equal('');
123+
expect(el.inputElement.getAttribute('name')).to.equal('');
124+
125+
el.name = 'foo';
126+
await el.updateComplete;
127+
expect(el.getAttribute('name')).to.equal('foo');
128+
expect(el.inputElement.getAttribute('name')).to.equal('foo');
129+
});
130+
119131
// TODO: find out if we could put all listeners on this.value (instead of this.inputElement.value)
120132
// and make it act on this.value again
121133
it('has a class "state-filled" if this.value is filled', async () => {

0 commit comments

Comments
 (0)