Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support htmlTitle for label #19

Merged
merged 5 commits into from
Mar 30, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 3 additions & 0 deletions docs/demo/html-title.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## html-title

<code src="../examples/html-title.tsx">
41 changes: 41 additions & 0 deletions docs/examples/html-title.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import '../../assets/style.less';
import React from 'react';
import Segmented from 'rc-segmented';

export default function App() {
return (
<div>
<div className="wrapper">
<Segmented options={['iOS', 'Android', 'Web']} />
</div>
<div className="wrapper">
<Segmented
options={[
{
label: 'iOS',
value: 'iOS',
},
{
label: 'Android',
value: 'Android',
htmlTitle: 'Android12',
},
{
label: <h3>Web</h3>,
value: 'Web',
},
]}
/>
</div>
<div className="wrapper">
<Segmented
options={[
{ label: 'iOS', value: 'iOS', htmlTitle: 'IOS' },
{ label: 'Android', value: 'Android', htmlTitle: '' },
{ label: <h1>Web</h1>, value: 'Web', htmlTitle: 'WEB' },
]}
/>
</div>
</div>
);
}
42 changes: 38 additions & 4 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export interface SegmentedLabeledOption {
disabled?: boolean;
label: React.ReactNode;
value: SegmentedRawOption;
/**
* html `title` property for label
*/
htmlTitle?: string;
}

type SegmentedOptions = (SegmentedRawOption | SegmentedLabeledOption)[];
Expand All @@ -33,13 +37,31 @@ export interface SegmentedProps extends React.HTMLProps<HTMLDivElement> {
motionName?: string;
}

function getValidHtmlTitle(option: SegmentedLabeledOption) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

感觉还可以再抽一个 getValid 判断 string 和 number 然后直接 return getValid(option.title) ?? getValid(option.label)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

目前看起来这个判断是够用的,后面我考虑下,因为 title 其实要求是 string,而 label 其实在 options 下也应该要求是 string 才好,但是现在 label 是 ReactNode 所以这个判断处理起来有点怪,你可以看看新的写法

if (typeof option.htmlTitle !== 'undefined') {
return option.htmlTitle;
}

// read `label` when htmlTitle is `undefined`
if (typeof option.label !== 'object') {
return option.label?.toString();
}
}

function normalizeOptions(options: SegmentedOptions): SegmentedLabeledOption[] {
return options.map((option) => {
if (typeof option === 'object') {
return option || {};
if (typeof option === 'object' && option !== null) {
const validHtmlTitle = getValidHtmlTitle(option);

return {
...option,
htmlTitle: validHtmlTitle,
};
}

return {
label: option?.toString(),
htmlTitle: option?.toString(),
value: option,
};
});
Expand All @@ -56,12 +78,22 @@ const InternalSegmentedOption: React.FC<{
disabled?: boolean;
checked: boolean;
label: React.ReactNode;
htmlTitle?: string;
value: SegmentedRawOption;
onChange: (
e: React.ChangeEvent<HTMLInputElement>,
value: SegmentedRawOption,
) => void;
}> = ({ prefixCls, className, disabled, checked, label, value, onChange }) => {
}> = ({
prefixCls,
className,
disabled,
checked,
label,
htmlTitle,
value,
onChange,
}) => {
const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
if (disabled) {
return;
Expand All @@ -83,7 +115,9 @@ const InternalSegmentedOption: React.FC<{
checked={checked}
onChange={handleChange}
/>
<div className={`${prefixCls}-item-label`}>{label}</div>
<div className={`${prefixCls}-item-label`} title={htmlTitle}>
{label}
</div>
</label>
);
};
Expand Down
Loading