Skip to content

Commit

Permalink
fix: 🐛 replace defaultProps with useDefaultProps (#2397)
Browse files Browse the repository at this point in the history
* fix: 🐛 🐛 replace defaultProps with useDefaultProps

* fix: 🐛 fix

* fix: 🐛 fix

* fix: 🐛 fix

* fix: 🐛 fix

* fix: 🐛 fix

* fix: 🐛 fix

* fix: fix

* fix: fix

* fix: fix

* fix(fix snap): fix snap
  • Loading branch information
li-jia-nan authored Nov 28, 2023
1 parent db81348 commit be13f67
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 34 deletions.
10 changes: 1 addition & 9 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,5 @@
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
"cSpell.words": [
"activable",
"actived",
"borderless",
"Cascader",
"Popconfirm",
"Swiper",
"tdesign"
]
"cSpell.words": ["activable", "actived", "borderless", "Cascader", "Popconfirm", "Swiper", "tdesign"]
}
14 changes: 7 additions & 7 deletions src/common/Check.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { Ref, forwardRef, useContext, MouseEvent } from 'react';
import React, { forwardRef, useContext, MouseEvent, ChangeEvent } from 'react';
import classNames from 'classnames';
import isBoolean from 'lodash/isBoolean';
import { omit } from '../_util/helper';
Expand Down Expand Up @@ -27,7 +27,7 @@ export interface CheckContextValue {
inject: (props: CheckProps) => CheckProps;
}

const Check = forwardRef((_props: CheckProps, ref: Ref<HTMLLabelElement>) => {
const Check = forwardRef<HTMLLabelElement, CheckProps>((_props, ref) => {
// 支持从 Context 注入
const context = useContext(CheckContext);
const props = context ? context.inject(_props) : _props;
Expand All @@ -53,9 +53,7 @@ const Check = forwardRef((_props: CheckProps, ref: Ref<HTMLLabelElement>) => {

const TOnChange: (
checked: boolean,
context: {
e: React.ChangeEvent<HTMLInputElement> | React.MouseEvent<HTMLInputElement>;
},
context: { e: ChangeEvent<HTMLInputElement> | MouseEvent<HTMLInputElement> },
) => void = onChange;

const [internalChecked, setInternalChecked] = useControlled(props, 'checked', TOnChange);
Expand Down Expand Up @@ -90,9 +88,11 @@ const Check = forwardRef((_props: CheckProps, ref: Ref<HTMLLabelElement>) => {
// Checkbox/ Radio 内容为空则不再渲染 span,不存在 0:Number 的情况
const showLabel = !!(children || label);

const handleLabelClick = (event) => {
const handleLabelClick = (event: MouseEvent<HTMLSpanElement>) => {
// 在tree等组件中使用 阻止label触发checked 与expand冲突
if (props.stopLabelTrigger) event.preventDefault();
if (props.stopLabelTrigger) {
event.preventDefault();
}
};

const onInnerClick = (e: MouseEvent<HTMLLabelElement>) => {
Expand Down
10 changes: 6 additions & 4 deletions src/radio/Radio.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,22 @@ import forwardRefWithStatics from '../_util/forwardRefWithStatics';
import Check, { CheckProps } from '../common/Check';
import RadioGroup from './RadioGroup';
import { radioDefaultProps } from './defaultProps';
import useDefaultProps from '../hooks/useDefaultProps';

export type RadioProps = Omit<CheckProps, 'type'>;

const Radio = forwardRefWithStatics(
(props: RadioProps, ref: Ref<HTMLLabelElement>) => <Check ref={ref} type="radio" {...props} />,
(props: RadioProps, ref: Ref<HTMLLabelElement>) => (
<Check ref={ref} type="radio" {...useDefaultProps<RadioProps>(props, radioDefaultProps)} />
),
{
Group: RadioGroup,
Button: forwardRef((props: RadioProps, ref: Ref<HTMLLabelElement>) => (
<Check ref={ref} type="radio-button" {...props} />
Button: forwardRef<HTMLLabelElement, RadioProps>((props, ref) => (
<Check ref={ref} type="radio-button" {...useDefaultProps<RadioProps>(props, radioDefaultProps)} />
)),
},
);

Radio.displayName = 'Radio';
Radio.defaultProps = radioDefaultProps;

export default Radio;
19 changes: 14 additions & 5 deletions src/radio/RadioGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { CheckContext, CheckContextValue } from '../common/Check';
import Radio from './Radio';
import useMutationObservable from '../_util/useMutationObserver';
import { radioGroupDefaultProps } from './defaultProps';
import useDefaultProps from '../hooks/useDefaultProps';
import useKeyboard from './useKeyboard';

/**
Expand All @@ -21,8 +22,11 @@ export interface RadioGroupProps extends TdRadioGroupProps, StyledProps {
/**
* 单选选项组,里面可以嵌套 <Radio />
*/
const RadioGroup = (props: RadioGroupProps) => {
const RadioGroup: React.FC<RadioGroupProps> = (originalProps) => {
const { classPrefix } = useConfig();

const props = useDefaultProps<RadioGroupProps>(originalProps, radioGroupDefaultProps);

const { disabled, children, onChange, size, variant, options = [], className, style } = props;

const [internalValue, setInternalValue] = useControlled(props, 'value', onChange);
Expand Down Expand Up @@ -61,9 +65,13 @@ const RadioGroup = (props: RadioGroupProps) => {
};

const calcBarStyle = () => {
if (!variant.includes('filled')) return;
if (!variant.includes('filled')) {
return;
}
const checkedRadio = radioGroupRef.current.querySelector?.(checkedRadioCls) as HTMLElement;
if (!checkedRadio) return setBarStyle({ width: 0 });
if (!checkedRadio) {
return setBarStyle({ width: 0 });
}

const { offsetWidth, offsetHeight, offsetLeft, offsetTop } = checkedRadio;
setBarStyle({
Expand All @@ -81,7 +89,9 @@ const RadioGroup = (props: RadioGroupProps) => {
useMutationObservable(radioGroupRef.current, calcBarStyle);

const renderBlock = () => {
if (!variant.includes('filled')) return null;
if (!variant.includes('filled')) {
return null;
}
return <div style={barStyle} className={`${classPrefix}-radio-group__bg-block`}></div>;
};

Expand Down Expand Up @@ -126,6 +136,5 @@ const RadioGroup = (props: RadioGroupProps) => {
};

RadioGroup.displayName = 'RadioGroup';
RadioGroup.defaultProps = radioGroupDefaultProps;

export default RadioGroup;
16 changes: 8 additions & 8 deletions test/snap/__snapshots__/csr.test.jsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -92203,12 +92203,12 @@ exports[`csr snapshot test > csr test src/form/_example/disabled.jsx 1`] = `
class="t-radio-group t-size-m t-radio-group__outline"
>
<label
class="t-radio"
tabindex="0"
class="t-radio t-is-disabled"
>
<input
class="t-radio__former"
data-value="'1'"
disabled=""
tabindex="-1"
type="radio"
value="1"
Expand All @@ -92223,12 +92223,12 @@ exports[`csr snapshot test > csr test src/form/_example/disabled.jsx 1`] = `
</span>
</label>
<label
class="t-radio"
tabindex="0"
class="t-radio t-is-disabled"
>
<input
class="t-radio__former"
data-value="'2'"
disabled=""
tabindex="-1"
type="radio"
value="2"
Expand Down Expand Up @@ -92986,12 +92986,12 @@ exports[`csr snapshot test > csr test src/form/_example/disabled.jsx 1`] = `
class="t-radio-group t-size-m t-radio-group__outline"
>
<label
class="t-radio"
tabindex="0"
class="t-radio t-is-disabled"
>
<input
class="t-radio__former"
data-value="'1'"
disabled=""
tabindex="-1"
type="radio"
value="1"
Expand All @@ -93006,12 +93006,12 @@ exports[`csr snapshot test > csr test src/form/_example/disabled.jsx 1`] = `
</span>
</label>
<label
class="t-radio"
tabindex="0"
class="t-radio t-is-disabled"
>
<input
class="t-radio__former"
data-value="'2'"
disabled=""
tabindex="-1"
type="radio"
value="2"
Expand Down
Loading

0 comments on commit be13f67

Please sign in to comment.