Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | 32x 32x 177x 177x 177x 177x 157x 177x 177x 177x 177x 125x 177x 177x 4x 177x 4x 177x 5x 177x 32x | import React, { useContext, useEffect, forwardRef, Ref, LabelHTMLAttributes, useLayoutEffect, } from "react"; import classNames from "classnames"; import { useRefAndForward } from "../_util/use-ref-and-forward"; import { Combine, WithoutMethod, Omit } from "../_type"; import { ControlledProps, ChangeContext, useDefaultValue, } from "../form/controlled"; import { Tooltip } from "../tooltip"; import { callBoth } from "../_util/call-both"; import { useConfig } from "../_util/config-context"; /** * `CheckInput` 组件所接收的参数 */ export interface CheckProps extends Combine< Omit<LabelHTMLAttributes<HTMLLabelElement>, "onChange" | "defaultValue">, ControlledProps<boolean> > { /** * 获取内部 `input` 的引用 */ inputRef?: React.Ref<HTMLInputElement>; /** * `Checkbox` 的名称,对于 `CheckGroup` 等管理时必传 */ name?: string; /** * 选项类型 */ type: "radio" | "checkbox"; /** * 是否已经勾选 */ value?: boolean; /** * 勾选状态发生变更时回调 */ onChange?: (value: boolean, context: CheckChangeContext) => void; /** * 是否半选状态(仅对 `checkbox` 生效) * @default false */ indeterminate?: boolean; /** * 是否可用 * @default false */ disabled?: boolean; /** * 渲染方式,设置为 `block` 会独占一行 * @default "inline" */ display?: "inline" | "block"; /** 文本解释内容,可用于解释选项的作用 */ tooltip?: React.ReactNode; /** 点击时触发 */ onClick?: (event: React.MouseEvent) => void; } /** * 为变更事件提供额外的内容 */ export interface CheckChangeContext extends ChangeContext { /** * 发生变化的选项组件实例 */ check: ReadonlyCheckProps; } /** * 组件实例只读属性集合 */ export interface ReadonlyCheckProps extends Readonly<WithoutMethod<CheckProps>> {} /** * Check 组件支持使用 CheckContext 进行状态托管 */ export const CheckContext = React.createContext<CheckContextValue>(null); /** * 托管 Check 组件的状态,请提供 inject() 方法注入托管好的 props */ export interface CheckContextValue { inject: (props: CheckProps) => CheckProps; } export const Check = forwardRef( (_props: CheckProps, ref: Ref<HTMLLabelElement>) => { const { classPrefix } = useConfig(); // 支持从 Context 注入 const context = useContext(CheckContext); let props = _props; if (context) { props = context.inject(_props); } const { value, onChange, type, indeterminate, children, className, disabled = false, onClick, display, style, inputRef, tooltip, tabIndex, ...htmlProps } = useDefaultValue(props, false); // handle indeterminate const [checkInputRef, forwardInputRef] = useRefAndForward(null, inputRef); useLayoutEffect(() => { if (type === "checkbox" && checkInputRef.current) { checkInputRef.current.indeterminate = indeterminate; } }); const labelClassName = classNames( `${classPrefix}-form-check`, { [`${classPrefix}-form-check--block`]: display === "block", }, className ); const input = ( <input readOnly ref={forwardInputRef} type={type} className={`${classPrefix}-${type}`} checked={value} disabled={disabled} tabIndex={tabIndex} onClick={callBoth( onClick, (event: React.MouseEvent<HTMLInputElement>) => onChange(event.currentTarget.checked, { event, check: { ...props, value: event.currentTarget.checked }, }) )} /> ); let check = ( <label ref={ref} className={labelClassName} style={style} {...htmlProps} onClick={event => event.stopPropagation()} > {type === "radio" ? ( <span className={`${classPrefix}-radio-wrap`}> {input} <i className={`${classPrefix}-radio__dot`} /> </span> ) : ( input )} <span key="label" className={`${classPrefix}-form-check__label`}> {children} </span> </label> ); if (tooltip) { check = <Tooltip title={tooltip}>{check}</Tooltip>; } return check; } ); Check.displayName = "Check"; |