-
Notifications
You must be signed in to change notification settings - Fork 3
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
Use custom events for inputs #42
Changes from 5 commits
8cd17b4
2969a78
616e816
dcc095c
1cbc48e
ef5db1a
044e85d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,32 @@ | ||
import { Component, Prop } from '@stencil/core'; | ||
import { Option, Value } from 'types/Select'; | ||
import { Component, Prop, Event, EventEmitter, Watch } from '@stencil/core'; | ||
import { Option } from 'types/Select'; | ||
|
||
@Component({ | ||
tag: 'mf-select', | ||
styleUrl: 'mf-select.css', | ||
scoped: true, | ||
}) | ||
export class MfSelect { | ||
@Prop() options: Option[] = []; | ||
@Prop() selectedValue?: Value; | ||
@Prop() defaultValue?: string; | ||
@Prop() name: string; | ||
@Prop() options: Option[] = []; | ||
@Prop() required?: boolean; | ||
@Prop() onChange: (e: UIEvent) => void; | ||
@Event() updateValue: EventEmitter; | ||
@Watch('defaultValue') watchHandler(newVal: string) { | ||
this.updateValue.emit({ name: this.name, value: newVal }); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was totally wondering if |
||
|
||
onChangeHandler = (e: Event) => { | ||
if (!e.target) return; | ||
const { value } = e.target as HTMLSelectElement; | ||
this.updateValue.emit({ name: this.name, value }); | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the event details, we’re submitting the |
||
|
||
render() { | ||
return ( | ||
<select name={this.name} required={this.required} onChange={this.onChange}> | ||
<select name={this.name} required={this.required} onChange={this.onChangeHandler}> | ||
{this.options.map(({ label, value }) => ( | ||
<option value={value} selected={this.selectedValue === value}> | ||
<option value={value} selected={value === this.defaultValue}> | ||
{label} | ||
</option> | ||
))} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Borrowed the
defaultValue
from React. Technically, this works likevalue
, and can overwrite the user’s changes. But the way it’s passed down, it’ll never change so it should allow the user to modify inputs freely (kinda like how HTML works—value
only determines the default. Actually there have been discussions to change this in React)