Skip to content

Commit

Permalink
refactor: use change event type
Browse files Browse the repository at this point in the history
  • Loading branch information
juanrgm committed Apr 27, 2022
1 parent 162ad2a commit e7246bc
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 20 deletions.
6 changes: 6 additions & 0 deletions .changeset/four-rules-pretend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@suid/material": patch
"@suid/types": patch
---

Use change event type
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import { RefProp } from "@suid/system/createRef";
import { EventParam } from "@suid/types";
import { ChangeEvent } from "@suid/types";
import { createContext } from "solid-js";

export interface FormControlLabelState {
checked?: boolean;
name?: string;
onChange?: (
event: EventParam<HTMLInputElement, MouseEvent>,
checked: boolean
) => void;
onChange?: (event: ChangeEvent<HTMLInputElement>, checked: boolean) => void;
value?: any;
inputRef?: RefProp;
disabled?: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { TypographyProps } from "../Typography";
import { FormControlLabelClasses } from "./formControlLabelClasses";
import { SxProps } from "@suid/system";
import { RefProp } from "@suid/system/createRef";
import { ElementType, EventParam, OverrideProps } from "@suid/types";
import { ChangeEvent, ElementType, OverrideProps } from "@suid/types";
import { JSXElement } from "solid-js";

export interface FormControlLabelTypeMap<
Expand Down Expand Up @@ -62,13 +62,10 @@ export interface FormControlLabelTypeMap<
/**
* Callback fired when the state is changed.
*
* @param {EventParam<HTMLElement, MouseEvent>} event The event source of the callback.
* @param {ChangeEvent<HTMLElement>} event The event source of the callback.
* You can pull out the new checked state by accessing `event.target.checked` (boolean).
*/
onChange?: (
event: EventParam<HTMLElement, MouseEvent>,
checked: boolean
) => void;
onChange?: (event: ChangeEvent<HTMLElement>, checked: boolean) => void;
/**
* The system prop that allows defining system overrides as well as additional CSS styles.
*/
Expand Down
4 changes: 2 additions & 2 deletions packages/material/src/internal/SwitchBase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,9 @@ const SwitchBase = $.component(function SwitchBase({

if (typeof props.onChange === "function") {
// TODO v6: remove the second argument.
props.onChange(event, newChecked);
props.onChange(event as any, newChecked);
} else {
formControlLabel?.onChange?.(event, newChecked);
formControlLabel?.onChange?.(event as any, newChecked);
}

if (typeof otherProps.onClick === "function")
Expand Down
9 changes: 3 additions & 6 deletions packages/material/src/internal/SwitchBaseProps.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { OverrideProps } from "../OverridableComponent";
import { SwitchBaseClasses } from "./switchBaseClasses";
import { ElementType, EventParam, Ref } from "@suid/types";
import { ElementType, Ref, ChangeEvent } from "@suid/types";
import { JSX, JSXElement } from "solid-js";

export interface SwitchBasePropsVariantOverrides {}
Expand Down Expand Up @@ -61,14 +61,11 @@ export interface SwitchBaseTypeMap<P = {}, D extends ElementType = "div"> {
/**
* Callback fired when the state is changed.
*
* @param {EventParam<HTMLInputElement, MouseEvent>} event The event source of the callback.
* @param {ChangeEvent<HTMLInputElement>} event The event source of the callback.
* You can pull out the new value by accessing `event.target.value` (string).
* You can pull out the new checked state by accessing `event.target.checked` (boolean).
*/
onChange?: (
event: EventParam<HTMLInputElement, MouseEvent>,
checked: boolean
) => void;
onChange?: (event: ChangeEvent<HTMLInputElement>, checked: boolean) => void;
readOnly?: boolean;
/**
* If `true`, the `input` element is required.
Expand Down
6 changes: 5 additions & 1 deletion packages/types/src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ export type EventParam<T = HTMLElement, E extends Event = Event> = E & {

type EH<T, E extends Event> = JSX.EventHandler<T, E>;

export type ChangeEvent<T = Element> = EventParam<T, Event> & {
target: EventTarget & T;
};

export type AnimationEventHandler<T = Element> = EH<T, AnimationEvent>;
export type ChangeEventHandler<T = Element, V = string> = (
event: Parameters<EH<T, KeyboardEvent>>[0],
event: ChangeEvent<T>,
value: V
) => void;
export type ClipboardEventHandler<T = Element> = EH<T, ClipboardEvent>;
Expand Down

0 comments on commit e7246bc

Please sign in to comment.