Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Makes .fill work with selects and other modifiers #3495

Merged
merged 1 commit into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 4 additions & 5 deletions packages/alpinejs/src/directives/x-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ directive('model', (el, { modifiers, expression }, { effect, cleanup }) => {
})
}
}

if (modifiers.includes('fill') && el.hasAttribute('value') && (getValue() === null || getValue() === '')) {
setValue(el.value)
}

if (typeof expression === 'string' && el.type === 'radio') {
// Radio buttons only work properly when they share a name attribute.
Expand All @@ -73,7 +69,10 @@ directive('model', (el, { modifiers, expression }, { effect, cleanup }) => {
let removeListener = isCloning ? () => {} : on(el, event, modifiers, (e) => {
setValue(getInputValue(el, modifiers, e, getValue()))
})


if (modifiers.includes('fill') && [null, ''].includes(getValue())) {
el.dispatchEvent(new Event(event, {}));
}
// Register the listener removal callback on the element, so that
// in addition to the cleanup function, x-modelable may call it.
// Also, make this a keyed object if we decide to reintroduce
Expand Down
35 changes: 35 additions & 0 deletions tests/cypress/integration/directives/x-model.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,5 +151,40 @@ test('x-model with fill modifier takes input value on null or empty string',
}
)

test('x-model with fill modifier works with select/radio elements',
html`
<div x-data="{ a: null, b: null, c: null, d: null }">
<select x-model.fill="a">
<option value="123">123</option>
<option value="456" selected>456</option>
</select>
<select x-model.fill="b" multiple>
<option value="123" selected>123</option>
<option value="456" selected>456</option>
</select>
</div>
`,
({ get }) => {
get('[x-data]').should(haveData('a', '456'));
get('[x-data]').should(haveData('b', ['123', '456']));
}
);

test('x-model with fill modifier respects number modifier',
html`
<div x-data="{ a: null, b: null, c: null, d: null }">
<input type="text" x-model.fill.number="a" value="456" / >
<select x-model.fill.number="b" multiple>
<option value="123" selected>123</option>
<option value="456" selected>456</option>
</select>
</div>
`,
({ get }) => {
get('[x-data]').should(haveData('a', 456));
get('[x-data]').should(haveData('b', [123,456]));
}
);



5 changes: 4 additions & 1 deletion tests/cypress/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ function injectHtmlAndBootAlpine(cy, templateAndPotentiallyScripts, callback, pa
})
}

export let haveData = (key, value) => ([ el ]) => expect(root(el)._x_dataStack[0][key]).to.equal(value)
export let haveData =
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution, just a heads up, making non-code-changing, styling changes (especially to styles that diverge from the rest of the codebase), makes PR's harder for me to review and merge without putting extra work in.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On this note, would you be able to add the workspace settings/formatter settings you use to the repository?

Then it becomes automatic that anyone cloning the repo would have the same formatter.

(key, value) =>
([el]) =>
expect(root(el)._x_dataStack[0][key]).to.deep.equal(value);

export let haveFocus = () => el => expect(el).to.have.focus

Expand Down