Skip to content

feat: some naming issues #10

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

Merged
merged 1 commit into from
Mar 1, 2022
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
29 changes: 15 additions & 14 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,23 @@ import omit from 'rc-util/lib/omit';

export type SegmentedValue = string | number;

type RawOption = SegmentedValue;
type SegmentedRawOption = SegmentedValue;

interface LabeledOption {
interface SegmentedLabeledOption {
className?: string;
disabled?: boolean;
label: React.ReactNode;
value: RawOption;
value: SegmentedRawOption;
}

type Option = RawOption | LabeledOption;

type Options = Option[];
type SegmentedOptions = (SegmentedRawOption | SegmentedLabeledOption)[];

type ExtendedHTMLInputElement = Omit<HTMLInputElement, 'value'> & {
value: SegmentedValue;
};

export interface SegmentedProps extends React.HTMLProps<HTMLDivElement> {
options: Options;
options: SegmentedOptions;
defaultValue?: SegmentedValue;
value?: SegmentedValue;
onChange?: (e: React.ChangeEvent<ExtendedHTMLInputElement>) => void;
Expand All @@ -35,7 +33,7 @@ export interface SegmentedProps extends React.HTMLProps<HTMLDivElement> {
motionName?: string;
}

function normalizeOptions(options: Options): LabeledOption[] {
function normalizeOptions(options: SegmentedOptions): SegmentedLabeledOption[] {
return options.map((option) => {
if (typeof option === 'object') {
return option || {};
Expand All @@ -52,14 +50,17 @@ const calcThumbStyle = (targetElement: HTMLElement): React.CSSProperties => ({
width: targetElement.clientWidth,
});

const SegmentedOption: React.FC<{
const InternalSegmentedOption: React.FC<{
prefixCls: string;
className?: string;
disabled?: boolean;
checked: boolean;
label: React.ReactNode;
value: RawOption;
onChange: (e: React.ChangeEvent<HTMLInputElement>, value: RawOption) => void;
value: SegmentedRawOption;
onChange: (
e: React.ChangeEvent<HTMLInputElement>,
value: SegmentedRawOption,
) => void;
}> = ({ prefixCls, className, disabled, checked, label, value, onChange }) => {
const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
if (disabled) {
Expand Down Expand Up @@ -119,7 +120,7 @@ const Segmented = React.forwardRef<HTMLDivElement, SegmentedProps>(
);

const [visualSelected, setVisualSelected] = React.useState<
RawOption | undefined
SegmentedRawOption | undefined
>(selected);

const [thumbShow, setThumbShow] = React.useState(false);
Expand All @@ -143,7 +144,7 @@ const Segmented = React.forwardRef<HTMLDivElement, SegmentedProps>(

const handleChange = (
event: React.ChangeEvent<HTMLInputElement>,
value: RawOption,
value: SegmentedRawOption,
) => {
if (disabled) {
return;
Expand Down Expand Up @@ -231,7 +232,7 @@ const Segmented = React.forwardRef<HTMLDivElement, SegmentedProps>(
}}
</CSSMotion>
{segmentedOptions.map((segmentedOption) => (
<SegmentedOption
<InternalSegmentedOption
key={segmentedOption.value}
prefixCls={prefixCls}
className={classNames(segmentedOption.className, {
Expand Down
2 changes: 1 addition & 1 deletion tests/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ describe('rc-segmented', () => {
).toEqual([true, false, false]);
});

it('render segmented with className and other html attribues', () => {
it('render segmented with className and other html attributes', () => {
const wrapper = mount(
<Segmented
options={['iOS', 'Android', 'Web']}
Expand Down