|
| 1 | +import React, { |
| 2 | + CSSProperties, |
| 3 | + forwardRef, |
| 4 | + HTMLAttributes, |
| 5 | + InputHTMLAttributes, |
| 6 | +} from "react"; |
| 7 | +import cn from "classnames"; |
| 8 | +import { bem, PropsWithRef } from "@react-md/utils"; |
| 9 | + |
| 10 | +const styles = bem("rmd-switch"); |
| 11 | + |
| 12 | +/** @remarks \@since 2.8.0 */ |
| 13 | +export interface SwitchTrackProps |
| 14 | + extends InputHTMLAttributes<HTMLInputElement> { |
| 15 | + /** |
| 16 | + * If an `id` is provided, the track will contain a checkbox input element and |
| 17 | + * render the "ball" as a `<label>`. If the `id` is omitted, no input element |
| 18 | + * will be rendered and the "ball" will be rendered as a `<span>`. |
| 19 | + * |
| 20 | + * Basically only omit the `id` if this is used in another accessible widget |
| 21 | + * like `menuitemcheckbox`. |
| 22 | + */ |
| 23 | + id?: string; |
| 24 | + |
| 25 | + /** |
| 26 | + * An optional style object to provide to the ball. |
| 27 | + */ |
| 28 | + ballStyle?: CSSProperties; |
| 29 | + |
| 30 | + /** |
| 31 | + * An optional class name to provide to the ball. |
| 32 | + */ |
| 33 | + ballClassName?: string; |
| 34 | + |
| 35 | + /** |
| 36 | + * Any additional props and optional ref to provide to the track itself since |
| 37 | + * all the props are normally provided to the `<input>` element instead. |
| 38 | + */ |
| 39 | + containerProps?: PropsWithRef< |
| 40 | + HTMLAttributes<HTMLSpanElement>, |
| 41 | + HTMLSpanElement |
| 42 | + >; |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * This is most likely an internal only component that is used to render the |
| 47 | + * switch element either as a checkbox or in the `MenuItemSwitch` component. |
| 48 | + * |
| 49 | + * @remarks \@since 2.8.0 |
| 50 | + */ |
| 51 | +export const SwitchTrack = forwardRef<HTMLInputElement, SwitchTrackProps>( |
| 52 | + function SwitchTrack( |
| 53 | + { |
| 54 | + id, |
| 55 | + disabled = false, |
| 56 | + checked = false, |
| 57 | + className, |
| 58 | + ballStyle, |
| 59 | + ballClassName, |
| 60 | + containerProps, |
| 61 | + children, |
| 62 | + ...props |
| 63 | + }, |
| 64 | + ref |
| 65 | + ) { |
| 66 | + return ( |
| 67 | + <span |
| 68 | + {...containerProps} |
| 69 | + className={cn(styles(), containerProps?.className)} |
| 70 | + > |
| 71 | + {id && ( |
| 72 | + <> |
| 73 | + <input |
| 74 | + {...props} |
| 75 | + id={id} |
| 76 | + ref={ref} |
| 77 | + type="checkbox" |
| 78 | + className={cn(styles("input"), className)} |
| 79 | + disabled={disabled} |
| 80 | + /> |
| 81 | + <label |
| 82 | + htmlFor={id} |
| 83 | + aria-hidden |
| 84 | + style={ballStyle} |
| 85 | + className={cn(styles("ball"), ballClassName)} |
| 86 | + > |
| 87 | + {children} |
| 88 | + </label> |
| 89 | + </> |
| 90 | + )} |
| 91 | + {!id && ( |
| 92 | + <span |
| 93 | + style={ballStyle} |
| 94 | + className={cn(styles("ball", { checked }), ballClassName)} |
| 95 | + /> |
| 96 | + )} |
| 97 | + </span> |
| 98 | + ); |
| 99 | + } |
| 100 | +); |
0 commit comments