-
Notifications
You must be signed in to change notification settings - Fork 6.8k
feat(slide-toggle): make slide-toggle click and drag actions configurable #11719
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
Conversation
/** | ||
* Injection token that can be used to specify the slide toggle click behavior. | ||
*/ | ||
export const MAT_SLIDE_TOGGLE_CLICK_ACTION = |
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.
Could you combine these two into one token MAT_SLIDE_TOGGLE_DEFAULT_OPTIONS
? We use that pattern on a handful of other components so it would be a bit more consistent (see MAT_AUTOCOMPLETE_DEFAULT_OPTIONS
for an example)
* check: Only toggle checked status. Default behavior | ||
* undefined: Same as `check`. | ||
*/ | ||
export type MatSlideToggleDragAction = 'noop' | 'check' | undefined; |
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.
If someone sets this action to noop
, wouldn't it make sense to just block dragging at all?
Also, does it make sense to call the other value check
? Wouldn't toggle
fit better here?
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.
The use case is when user toggle/drag the slide toggle, a dialog popup and requires user's confirmation. After the user confirmed the value is changed.
I think we still need to enable dragging.
Changed to toggle
6999690
to
e6acc40
Compare
Updated |
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.
Just some small nits. I think it's really necessary to make clear what those two events do.
Shouldn't be mixed up with the change
event.
Also: Asked a few questions on Slack about the use-case
@@ -195,10 +206,13 @@ export class MatSlideToggle extends _MatSlideToggleMixinBase implements OnDestro | |||
// emit its event object to the component's `change` output. | |||
event.stopPropagation(); | |||
|
|||
if (!this._dragging) { | |||
this.toggleChange.emit(); | |||
} |
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.
Can you just move this.toggleChange.emit()
down below the this._emitChangeEvent()
? That's basically the same and removes the unnecessary check.
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.
This this.toggleChange.emit()
also fires when this.defaults.disableToggleValue
is true. When the value is true, the native element's checked status is reverted, and no value change is emitted.
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.
Makes sense.
src/lib/slide-toggle/slide-toggle.ts
Outdated
@@ -153,6 +157,12 @@ export class MatSlideToggle extends _MatSlideToggleMixinBase implements OnDestro | |||
@Output() readonly change: EventEmitter<MatSlideToggleChange> = | |||
new EventEmitter<MatSlideToggleChange>(); | |||
|
|||
/** An event will be dispatched each time the slide-toggle input is toggled. */ | |||
@Output() readonly toggleChange: EventEmitter<void> = new EventEmitter<void>(); |
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.
Just a nit: I think it would be really appropriate to expand this comment further, to mention that this always fires, but does not mean that the slide-toggle value is always being updated. Same for the dragChange
event.
Otherwise, I think there is the risk that people might use toggleChange
over the usual change
event.
src/lib/slide-toggle/slide-toggle.ts
Outdated
// Releasing the pointer over the `<label>` element while dragging triggers another | ||
// click event on the `<label>` element. This means that the checked state of the underlying | ||
// input changed unintentionally and needs to be changed back. | ||
if (this._dragging) { | ||
if (this._dragging || this.defaults.disableToggleValue) { |
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.
Please update the comment to mention that we also revert the value back, if disableToggleValue
is set to true.
e6acc40
to
94525da
Compare
export const MAT_SLIDE_TOGGLE_DEFAULT_OPTIONS = | ||
new InjectionToken<MatSlideToggleDefaultOptions>('mat-slide-toggle-default-options', { | ||
providedIn: 'root', | ||
factory: MAT_SLIDE_TOGGLE_DEFAULT_OPTIONS_FACTORY, |
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.
You should be able to use an inline function here:
factory: () => ({disableToggleValue: false, disableDragValue: false}),
provide: HAMMER_GESTURE_CONFIG, | ||
useFactory: () => gestureConfig = new TestGestureConfig() | ||
}, | ||
{ provide: MAT_SLIDE_TOGGLE_DEFAULT_OPTIONS, useValue: {disableToggleValue: true}}, |
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.
Extra space (here and below)
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.
Should be also able to remove that MutationObserver thing. It's not really needed
94525da
to
9942a6e
Compare
Updated and fixed test. Please take a look. Thanks! |
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.
LGTM
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
No description provided.