generated from react-component/footer
-
Notifications
You must be signed in to change notification settings - Fork 24
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(type): value supports generic definition #180
Closed
Closed
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d2ee55c
feat(ts): value supports generic definition
Wxh16144 792c6dc
docs: update demo
Wxh16144 a4809cb
fix: pins types-ramda to 0.29.4
Wxh16144 50a01a9
chore: rename
Wxh16144 6fd3169
feat: options Value support generic
Wxh16144 6e8022a
chore: update demo
Wxh16144 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,8 +1,8 @@ | ||||||
import * as React from 'react'; | ||||||
import classNames from 'classnames'; | ||||||
import useMergedState from 'rc-util/lib/hooks/useMergedState'; | ||||||
import { composeRef } from 'rc-util/lib/ref'; | ||||||
import omit from 'rc-util/lib/omit'; | ||||||
import { composeRef } from 'rc-util/lib/ref'; | ||||||
import * as React from 'react'; | ||||||
|
||||||
import MotionThumb from './MotionThumb'; | ||||||
|
||||||
|
@@ -23,12 +23,15 @@ export interface SegmentedLabeledOption { | |||||
|
||||||
type SegmentedOptions = (SegmentedRawOption | SegmentedLabeledOption)[]; | ||||||
|
||||||
export interface SegmentedProps | ||||||
/** | ||||||
* Generic parameter `Value` is supported since 2.3.0 | ||||||
*/ | ||||||
export interface SegmentedProps<Value extends SegmentedValue = SegmentedValue> | ||||||
extends Omit<React.HTMLProps<HTMLDivElement>, 'onChange'> { | ||||||
options: SegmentedOptions; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. options 也得接受泛型吧 |
||||||
defaultValue?: SegmentedValue; | ||||||
value?: SegmentedValue; | ||||||
onChange?: (value: SegmentedValue) => void; | ||||||
defaultValue?: Value; | ||||||
value?: Value; | ||||||
onChange?: (value: Value) => void; | ||||||
disabled?: boolean; | ||||||
prefixCls?: string; | ||||||
direction?: 'ltr' | 'rtl'; | ||||||
|
@@ -65,7 +68,7 @@ function normalizeOptions(options: SegmentedOptions): SegmentedLabeledOption[] { | |||||
}); | ||||||
} | ||||||
|
||||||
const InternalSegmentedOption: React.FC<{ | ||||||
interface _InternalSegmentedOptionProps { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
prefixCls: string; | ||||||
className?: string; | ||||||
disabled?: boolean; | ||||||
|
@@ -77,16 +80,20 @@ const InternalSegmentedOption: React.FC<{ | |||||
e: React.ChangeEvent<HTMLInputElement>, | ||||||
value: SegmentedRawOption, | ||||||
) => void; | ||||||
}> = ({ | ||||||
prefixCls, | ||||||
className, | ||||||
disabled, | ||||||
checked, | ||||||
label, | ||||||
title, | ||||||
value, | ||||||
onChange, | ||||||
}) => { | ||||||
} | ||||||
|
||||||
const InternalSegmentedOption = (props: _InternalSegmentedOptionProps) => { | ||||||
const { | ||||||
prefixCls, | ||||||
className, | ||||||
disabled, | ||||||
checked, | ||||||
label, | ||||||
title, | ||||||
value, | ||||||
onChange, | ||||||
} = props; | ||||||
|
||||||
const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => { | ||||||
if (disabled) { | ||||||
return; | ||||||
|
@@ -115,110 +122,114 @@ const InternalSegmentedOption: React.FC<{ | |||||
); | ||||||
}; | ||||||
|
||||||
const Segmented = React.forwardRef<HTMLDivElement, SegmentedProps>( | ||||||
(props, ref) => { | ||||||
const { | ||||||
prefixCls = 'rc-segmented', | ||||||
direction, | ||||||
options = [], | ||||||
disabled, | ||||||
defaultValue, | ||||||
value, | ||||||
onChange, | ||||||
className = '', | ||||||
motionName = 'thumb-motion', | ||||||
...restProps | ||||||
} = props; | ||||||
|
||||||
const containerRef = React.useRef<HTMLDivElement>(null); | ||||||
const mergedRef = React.useMemo( | ||||||
() => composeRef<HTMLDivElement>(containerRef, ref), | ||||||
[containerRef, ref], | ||||||
); | ||||||
|
||||||
const segmentedOptions = React.useMemo(() => { | ||||||
return normalizeOptions(options); | ||||||
}, [options]); | ||||||
|
||||||
// Note: We should not auto switch value when value not exist in options | ||||||
// which may break single source of truth. | ||||||
const [rawValue, setRawValue] = useMergedState(segmentedOptions[0]?.value, { | ||||||
value, | ||||||
defaultValue, | ||||||
}); | ||||||
|
||||||
// ======================= Change ======================== | ||||||
const [thumbShow, setThumbShow] = React.useState(false); | ||||||
|
||||||
const handleChange = ( | ||||||
event: React.ChangeEvent<HTMLInputElement>, | ||||||
val: SegmentedRawOption, | ||||||
) => { | ||||||
if (disabled) { | ||||||
return; | ||||||
} | ||||||
|
||||||
setRawValue(val); | ||||||
|
||||||
onChange?.(val); | ||||||
}; | ||||||
const Segmented = ( | ||||||
props: SegmentedProps, | ||||||
ref: React.ForwardedRef<HTMLDivElement>, | ||||||
) => { | ||||||
const { | ||||||
prefixCls = 'rc-segmented', | ||||||
direction, | ||||||
options = [], | ||||||
disabled, | ||||||
defaultValue, | ||||||
value, | ||||||
onChange, | ||||||
className = '', | ||||||
motionName = 'thumb-motion', | ||||||
...restProps | ||||||
} = props; | ||||||
|
||||||
const containerRef = React.useRef<HTMLDivElement>(null); | ||||||
const mergedRef = React.useMemo( | ||||||
() => composeRef<HTMLDivElement>(containerRef, ref), | ||||||
[containerRef, ref], | ||||||
); | ||||||
|
||||||
const segmentedOptions = React.useMemo(() => { | ||||||
return normalizeOptions(options); | ||||||
}, [options]); | ||||||
|
||||||
// Note: We should not auto switch value when value not exist in options | ||||||
// which may break single source of truth. | ||||||
const [rawValue, setRawValue] = useMergedState(segmentedOptions[0]?.value, { | ||||||
value, | ||||||
defaultValue, | ||||||
}); | ||||||
|
||||||
// ======================= Change ======================== | ||||||
const [thumbShow, setThumbShow] = React.useState(false); | ||||||
|
||||||
const divProps = omit(restProps, ['children']); | ||||||
|
||||||
return ( | ||||||
<div | ||||||
{...divProps} | ||||||
className={classNames( | ||||||
prefixCls, | ||||||
{ | ||||||
[`${prefixCls}-rtl`]: direction === 'rtl', | ||||||
[`${prefixCls}-disabled`]: disabled, | ||||||
}, | ||||||
className, | ||||||
)} | ||||||
ref={mergedRef} | ||||||
> | ||||||
<div className={`${prefixCls}-group`}> | ||||||
<MotionThumb | ||||||
const handleChange = ( | ||||||
event: React.ChangeEvent<HTMLInputElement>, | ||||||
val: SegmentedRawOption, | ||||||
) => { | ||||||
if (disabled) { | ||||||
return; | ||||||
} | ||||||
|
||||||
setRawValue(val); | ||||||
|
||||||
onChange?.(val); | ||||||
}; | ||||||
|
||||||
const divProps = omit(restProps, ['children']); | ||||||
|
||||||
return ( | ||||||
<div | ||||||
{...divProps} | ||||||
className={classNames( | ||||||
prefixCls, | ||||||
{ | ||||||
[`${prefixCls}-rtl`]: direction === 'rtl', | ||||||
[`${prefixCls}-disabled`]: disabled, | ||||||
}, | ||||||
className, | ||||||
)} | ||||||
ref={mergedRef} | ||||||
> | ||||||
<div className={`${prefixCls}-group`}> | ||||||
<MotionThumb | ||||||
prefixCls={prefixCls} | ||||||
value={rawValue} | ||||||
containerRef={containerRef} | ||||||
motionName={`${prefixCls}-${motionName}`} | ||||||
direction={direction} | ||||||
getValueIndex={(val) => | ||||||
segmentedOptions.findIndex((n) => n.value === val) | ||||||
} | ||||||
onMotionStart={() => { | ||||||
setThumbShow(true); | ||||||
}} | ||||||
onMotionEnd={() => { | ||||||
setThumbShow(false); | ||||||
}} | ||||||
/> | ||||||
{segmentedOptions.map((segmentedOption) => ( | ||||||
<InternalSegmentedOption | ||||||
{...segmentedOption} | ||||||
key={segmentedOption.value} | ||||||
prefixCls={prefixCls} | ||||||
value={rawValue} | ||||||
containerRef={containerRef} | ||||||
motionName={`${prefixCls}-${motionName}`} | ||||||
direction={direction} | ||||||
getValueIndex={(val) => | ||||||
segmentedOptions.findIndex((n) => n.value === val) | ||||||
} | ||||||
onMotionStart={() => { | ||||||
setThumbShow(true); | ||||||
}} | ||||||
onMotionEnd={() => { | ||||||
setThumbShow(false); | ||||||
}} | ||||||
className={classNames( | ||||||
segmentedOption.className, | ||||||
`${prefixCls}-item`, | ||||||
{ | ||||||
[`${prefixCls}-item-selected`]: | ||||||
segmentedOption.value === rawValue && !thumbShow, | ||||||
}, | ||||||
)} | ||||||
checked={segmentedOption.value === rawValue} | ||||||
onChange={handleChange} | ||||||
disabled={!!disabled || !!segmentedOption.disabled} | ||||||
/> | ||||||
{segmentedOptions.map((segmentedOption) => ( | ||||||
<InternalSegmentedOption | ||||||
{...segmentedOption} | ||||||
key={segmentedOption.value} | ||||||
prefixCls={prefixCls} | ||||||
className={classNames( | ||||||
segmentedOption.className, | ||||||
`${prefixCls}-item`, | ||||||
{ | ||||||
[`${prefixCls}-item-selected`]: | ||||||
segmentedOption.value === rawValue && !thumbShow, | ||||||
}, | ||||||
)} | ||||||
checked={segmentedOption.value === rawValue} | ||||||
onChange={handleChange} | ||||||
disabled={!!disabled || !!segmentedOption.disabled} | ||||||
/> | ||||||
))} | ||||||
</div> | ||||||
))} | ||||||
</div> | ||||||
); | ||||||
}, | ||||||
); | ||||||
</div> | ||||||
); | ||||||
}; | ||||||
|
||||||
Segmented.displayName = 'Segmented'; | ||||||
const RefSegmented = React.forwardRef(Segmented); | ||||||
RefSegmented.displayName = 'Segmented'; | ||||||
|
||||||
export default Segmented; | ||||||
export default RefSegmented as <Value extends SegmentedValue = SegmentedValue>( | ||||||
props: SegmentedProps<Value> & { ref?: React.Ref<HTMLDivElement> }, | ||||||
) => React.ReactElement; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个是什么原因
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
貌似跑 tsc 会挂,但是今天看 181 pr 又是好的,估计是 tsconfig include有点问题吧