Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-react-warnings-and-style.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tiny-design/react": patch
---

Fix React warnings (missing keys, nested buttons, DOM attribute leaks, setState during render, infinite loop) and Alert title margin
4 changes: 2 additions & 2 deletions packages/react/src/alert/alert.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const Alert = React.forwardRef<HTMLDivElement, AlertProps>((props, forwardedRef)
const closeBtnOnClick = (e: React.MouseEvent<HTMLSpanElement>): void => {
ref.current && setClosedStyle(ref.current as HTMLDivElement);
setShow(false);
onClose && onClose(e);
onClose?.(e);
};

// Setting close text attribute also allows to be closable
Expand Down Expand Up @@ -77,7 +77,7 @@ const Alert = React.forwardRef<HTMLDivElement, AlertProps>((props, forwardedRef)
}}>
{icon && renderIcon()}
<div>
{title && <p className={`${prefixCls}__title`}>{title}</p>}
{title && <p className={classNames(`${prefixCls}__title`, { [`${prefixCls}__title_has-content`]: children })}>{title}</p>}
{children}
</div>
{closeIcon}
Expand Down
8 changes: 6 additions & 2 deletions packages/react/src/alert/style/_index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@

&__title {
box-sizing: border-box;
margin: 0 0 5px;
font-size: 18px;
margin: 0;
font-size: 17px;
font-weight: 500;

&_has-content {
margin-bottom: 5px;
}
}

&__desc {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,34 @@ exports[`<Collapse /> should match the snapshot 1`] = `
<div
class="ty-collapse-item"
>
<button
aria-controls=":r0:"
aria-expanded="false"
<div
class="ty-collapse-item__header"
id=":r1:"
type="button"
>
<svg
class="ty-collapse-item__arrow"
height="10"
viewBox="0 0 1024 1024"
width="10"
>
<path
d="M472.064 751.552 72.832 352.32c-22.08-22.08-22.08-57.792 0-79.872 22.016-22.016 57.792-22.08 79.872 0L512 631.744l359.296-359.296c22.016-22.016 57.792-22.08 79.872 0 22.08 22.08 22.016 57.792 0 79.872l-399.232 399.232C529.856 773.568 494.144 773.568 472.064 751.552z"
fill="currentcolor"
/>
</svg>
<div
class="ty-collapse-item__title"
<button
aria-controls=":r0:"
aria-expanded="false"
class="ty-collapse-item__toggle"
id=":r1:"
type="button"
>
Panel 1
</div>
<div
class="ty-collapse-item__extra"
/>
</button>
<svg
class="ty-collapse-item__arrow"
height="10"
viewBox="0 0 1024 1024"
width="10"
>
<path
d="M472.064 751.552 72.832 352.32c-22.08-22.08-22.08-57.792 0-79.872 22.016-22.016 57.792-22.08 79.872 0L512 631.744l359.296-359.296c22.016-22.016 57.792-22.08 79.872 0 22.08 22.08 22.016 57.792 0 79.872l-399.232 399.232C529.856 773.568 494.144 773.568 472.064 751.552z"
fill="currentcolor"
/>
</svg>
<div
class="ty-collapse-item__title"
>
Panel 1
</div>
</button>
</div>
<div
class="ty-collapse-transition"
style="display: block;"
Expand All @@ -52,33 +53,34 @@ exports[`<Collapse /> should match the snapshot 1`] = `
<div
class="ty-collapse-item"
>
<button
aria-controls=":r2:"
aria-expanded="false"
<div
class="ty-collapse-item__header"
id=":r3:"
type="button"
>
<svg
class="ty-collapse-item__arrow"
height="10"
viewBox="0 0 1024 1024"
width="10"
>
<path
d="M472.064 751.552 72.832 352.32c-22.08-22.08-22.08-57.792 0-79.872 22.016-22.016 57.792-22.08 79.872 0L512 631.744l359.296-359.296c22.016-22.016 57.792-22.08 79.872 0 22.08 22.08 22.016 57.792 0 79.872l-399.232 399.232C529.856 773.568 494.144 773.568 472.064 751.552z"
fill="currentcolor"
/>
</svg>
<div
class="ty-collapse-item__title"
<button
aria-controls=":r2:"
aria-expanded="false"
class="ty-collapse-item__toggle"
id=":r3:"
type="button"
>
Panel 2
</div>
<div
class="ty-collapse-item__extra"
/>
</button>
<svg
class="ty-collapse-item__arrow"
height="10"
viewBox="0 0 1024 1024"
width="10"
>
<path
d="M472.064 751.552 72.832 352.32c-22.08-22.08-22.08-57.792 0-79.872 22.016-22.016 57.792-22.08 79.872 0L512 631.744l359.296-359.296c22.016-22.016 57.792-22.08 79.872 0 22.08 22.08 22.016 57.792 0 79.872l-399.232 399.232C529.856 773.568 494.144 773.568 472.064 751.552z"
fill="currentcolor"
/>
</svg>
<div
class="ty-collapse-item__title"
>
Panel 2
</div>
</button>
</div>
<div
class="ty-collapse-transition"
style="display: block;"
Expand Down
34 changes: 20 additions & 14 deletions packages/react/src/collapse/collapse-panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,27 @@ const CollapsePanel = (props: CollapsePanelProps): React.ReactElement => {
[`${prefixCls}__arrow_active`]: active,
});

const hasExtra = deletable || extra;

return (
<button
type="button"
id={headerId}
className={headerCls}
onClick={headerOnClick}
aria-expanded={active}
aria-controls={panelId}
aria-disabled={disabled || undefined}>
{showArrow && <ArrowDown size={10} className={arrowCls} />}
<div className={`${prefixCls}__title`}>{richNode(header, active)}</div>
<div className={`${prefixCls}__extra`}>
{deletable ? <span onClick={removeItem}>✕</span> : richNode(extra, active)}
</div>
</button>
<div className={headerCls}>
<button
type="button"
id={headerId}
className={`${prefixCls}__toggle`}
onClick={headerOnClick}
aria-expanded={active}
aria-controls={panelId}
aria-disabled={disabled || undefined}>
{showArrow && <ArrowDown size={10} className={arrowCls} />}
<div className={`${prefixCls}__title`}>{richNode(header, active)}</div>
</button>
{hasExtra && (
<div className={`${prefixCls}__extra`}>
{deletable ? <span onClick={removeItem}>✕</span> : richNode(extra, active)}
</div>
)}
</div>
);
};

Expand Down
37 changes: 27 additions & 10 deletions packages/react/src/collapse/style/_index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,9 @@
align-items: center;
box-sizing: border-box;
position: relative;
padding: 12px 16px;
color: var(--ty-color-text);
line-height: 22px;
cursor: pointer;
transition: all 300ms;
background: none;
border: none;
width: 100%;
text-align: left;
font-size: inherit;
font-family: inherit;

&:hover {
background-color: var(--ty-collapse-header-hover-bg);
Expand All @@ -49,11 +41,33 @@
}
}

&__toggle {
display: flex;
align-items: center;
flex: 1;
min-width: 0;
box-sizing: border-box;
padding: 12px 16px;
cursor: pointer;
background: none;
border: none;
width: 100%;
text-align: left;
font-size: inherit;
font-family: inherit;
color: inherit;
line-height: inherit;
}

&__header_disabled &__toggle {
cursor: not-allowed;
}

&__arrow {
margin-right: 10px;
transform: rotate(-90deg);
text-align: center;
color: currentColor;
color: currentcolor;
transition: all 300ms;

&_active {
Expand All @@ -66,9 +80,12 @@
}

&__extra {
display: flex;
align-items: center;
color: inherit;
font-size: 11px;
margin-left: 15px;
padding-right: 16px;
margin-left: auto;
}

&__content {
Expand Down
16 changes: 7 additions & 9 deletions packages/react/src/input-otp/input-otp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,14 @@ const InputOTP = React.forwardRef<InputOTPRef, InputOTPProps>(
// Trigger onChange when value cells change
const triggerValueCellsChange = useCallback(
(nextValueCells: string[]) => {
setValueCells((prev) => {
const prevValue = prev.join('');
const nextValue = nextValueCells.join('');
if (onChange && prevValue !== nextValue) {
onChange(nextValue);
}
return nextValueCells;
});
const prevValue = valueCells.join('');
const nextValue = nextValueCells.join('');
setValueCells(nextValueCells);
if (onChange && prevValue !== nextValue) {
onChange(nextValue);
}
},
[onChange]
[onChange, valueCells]
);

// Patch value at given index
Expand Down
8 changes: 6 additions & 2 deletions packages/react/src/list/list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ const List = React.forwardRef<HTMLDivElement, ListProps>((props, ref) => {
}
if (renderItem) {
const [start, end] = visibleRange;
const visibleItems = dataSource.slice(start, end + 1).map((item, i) => renderItem(item, start + i));
const visibleItems = dataSource.slice(start, end + 1).map((item, i) => (
<React.Fragment key={start + i}>{renderItem(item, start + i)}</React.Fragment>
));
return (
<div style={{ height: totalHeight, position: 'relative' }}>
<div style={{ position: 'absolute', top: 0, left: 0, right: 0, transform: `translateY(${offsetY}px)` }}>
Expand All @@ -106,7 +108,9 @@ const List = React.forwardRef<HTMLDivElement, ListProps>((props, ref) => {
);
}
if (renderItem) {
const rendered = items.map((item, index) => renderItem(item, index));
const rendered = items.map((item, index) => (
<React.Fragment key={index}>{renderItem(item, index)}</React.Fragment>
));
if (grid) {
return (
<div
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/native-select/demo/Disabled.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default function DisabledDemo() {
const { Option } = NativeSelect;

return (
<NativeSelect disabled value="oliver">
<NativeSelect disabled defaultValue="oliver">
<Option value="tom">Tom</Option>
<Option value="oliver">Oliver</Option>
<Option value="jack">Jack</Option>
Expand Down
8 changes: 5 additions & 3 deletions packages/react/src/strength-indicator/strength-indicator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ const StrengthIndicator = React.forwardRef<HTMLDivElement, StrengthIndicatorProp
current = 0,
blocks = 3,
colors = ['#f44336', '#ff9800', '#52c41a'],
labels,
className,
prefixCls: customisedCls,
...otherProps
} = props;
const configContext = useContext(ConfigContext);
const prefixCls = getPrefixCls('strength-indicator', configContext.prefixCls, customisedCls);
const cls = classNames(prefixCls, className);
const displayLabels: React.ReactNode[] = Array.isArray(props.labels)
? props.labels
const showLabels = labels !== undefined;
const displayLabels: React.ReactNode[] = Array.isArray(labels)
? labels
: ['Weak', 'Medium', 'Strong'];

return (
Expand All @@ -31,7 +33,7 @@ const StrengthIndicator = React.forwardRef<HTMLDivElement, StrengthIndicatorProp
return (
<div key={idx} className={itemCls}>
<div className={`${prefixCls}__inner`} style={{ backgroundColor: bgColor }} />
{'labels' in props && (
{showLabels && (
<div className={`${prefixCls}__label`}>{displayLabels[idx]}</div>
)}
</div>
Expand Down
9 changes: 5 additions & 4 deletions packages/react/src/tag/tag.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { PresetColors, TagProps } from './types';
const Tag = React.memo(forwardRef<HTMLDivElement, TagProps>((props, ref) => {
const {
closable = false,
visible: visibleProp,
defaultVisible = true,
prefixCls: customisedCls,
color,
Expand All @@ -18,7 +19,7 @@ const Tag = React.memo(forwardRef<HTMLDivElement, TagProps>((props, ref) => {
...otherProps
} = props;
const [visible, setVisible] = useState<boolean>(
'visible' in props ? (props.visible as boolean) : defaultVisible
visibleProp !== undefined ? visibleProp : defaultVisible
);
const configContext = useContext(ConfigContext);
const prefixCls = getPrefixCls('tag', configContext.prefixCls, customisedCls);
Expand All @@ -37,7 +38,7 @@ const Tag = React.memo(forwardRef<HTMLDivElement, TagProps>((props, ref) => {
if (e.defaultPrevented) {
return;
}
!('visible' in props) && setVisible(false);
visibleProp === undefined && setVisible(false);
};

const tagStyle: React.CSSProperties = {
Expand All @@ -48,8 +49,8 @@ const Tag = React.memo(forwardRef<HTMLDivElement, TagProps>((props, ref) => {
};

useEffect(() => {
'visible' in props && setVisible(props.visible as boolean);
}, [props]);
if (visibleProp !== undefined) setVisible(visibleProp);
}, [visibleProp]);

return (
<div {...otherProps} ref={ref} className={cls} style={tagStyle} onClick={onClick}>
Expand Down
Loading
Loading