-
-
Notifications
You must be signed in to change notification settings - Fork 338
feat:add maxTagPlaceholder api #938
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -23,17 +23,37 @@ const sharedLocale = { | |||||||||||||||||||||||||||||||||||||
| style: { width: 300 }, | ||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| const maxTagPlaceholder = (value: any[]) => { | ||||||||||||||||||||||||||||||||||||||
| return ( | ||||||||||||||||||||||||||||||||||||||
| <ul> | ||||||||||||||||||||||||||||||||||||||
| {value?.map((item) => { | ||||||||||||||||||||||||||||||||||||||
| return <li>{item?.format('YYYY-MM-DD')}</li>; | ||||||||||||||||||||||||||||||||||||||
| })} | ||||||||||||||||||||||||||||||||||||||
| </ul> | ||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||
|
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. 🛠️ Refactor suggestion 为可迭代元素添加 key(静态检查已提示) 为 -const maxTagPlaceholder = (value: any[]) => {
+const maxTagPlaceholder = (value: any[]) => {
return (
<ul>
- {value?.map((item) => {
- return <li>{item?.format('YYYY-MM-DD')}</li>;
+ {value?.map((item, idx) => {
+ return <li key={item?.valueOf?.() ?? idx}>{item?.format('YYYY-MM-DD')}</li>;
})}
</ul>
);
};📝 Committable suggestion
Suggested change
🧰 Tools🪛 Biome (2.1.2)[error] 30-30: Missing key property for this element in iterable. The order of the items may change, and having a key can help React identify which item was moved. (lint/correctness/useJsxKeyInIterable) 🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| export default () => { | ||||||||||||||||||||||||||||||||||||||
| const singleRef = React.useRef<PickerRef>(null); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| return ( | ||||||||||||||||||||||||||||||||||||||
| <div> | ||||||||||||||||||||||||||||||||||||||
| <SinglePicker {...sharedLocale} multiple ref={singleRef} onOpenChange={console.error} /> | ||||||||||||||||||||||||||||||||||||||
| <SinglePicker {...sharedLocale} multiple ref={singleRef} needConfirm /> | ||||||||||||||||||||||||||||||||||||||
| <SinglePicker {...sharedLocale} multiple picker="week" defaultValue={[ | ||||||||||||||||||||||||||||||||||||||
| dayjs('2021-01-01'), | ||||||||||||||||||||||||||||||||||||||
| dayjs('2021-01-08'), | ||||||||||||||||||||||||||||||||||||||
| ]} /> | ||||||||||||||||||||||||||||||||||||||
| <SinglePicker | ||||||||||||||||||||||||||||||||||||||
| {...sharedLocale} | ||||||||||||||||||||||||||||||||||||||
| multiple | ||||||||||||||||||||||||||||||||||||||
| picker="week" | ||||||||||||||||||||||||||||||||||||||
| defaultValue={[dayjs('2021-01-01'), dayjs('2021-01-08')]} | ||||||||||||||||||||||||||||||||||||||
| /> | ||||||||||||||||||||||||||||||||||||||
| <SinglePicker | ||||||||||||||||||||||||||||||||||||||
| maxTagCount={10} | ||||||||||||||||||||||||||||||||||||||
| {...sharedLocale} | ||||||||||||||||||||||||||||||||||||||
| multiple | ||||||||||||||||||||||||||||||||||||||
| ref={singleRef} | ||||||||||||||||||||||||||||||||||||||
| maxTagPlaceholder={maxTagPlaceholder} | ||||||||||||||||||||||||||||||||||||||
| defaultValue={[dayjs('2021-01-01'), dayjs('2021-01-08')]} | ||||||||||||||||||||||||||||||||||||||
| /> | ||||||||||||||||||||||||||||||||||||||
| </div> | ||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -4,7 +4,7 @@ import * as React from 'react'; | |||||||||||||||||||||||||||||||||||||
| import type { PickerProps } from '../../SinglePicker'; | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| export interface MultipleDatesProps<DateType extends object = any> | ||||||||||||||||||||||||||||||||||||||
| extends Pick<PickerProps, 'maxTagCount'> { | ||||||||||||||||||||||||||||||||||||||
| extends Pick<PickerProps, 'maxTagCount' | 'maxTagPlaceholder'> { | ||||||||||||||||||||||||||||||||||||||
| prefixCls: string; | ||||||||||||||||||||||||||||||||||||||
| value: DateType[]; | ||||||||||||||||||||||||||||||||||||||
| onRemove: (value: DateType) => void; | ||||||||||||||||||||||||||||||||||||||
|
|
@@ -25,6 +25,7 @@ export default function MultipleDates<DateType extends object = any>( | |||||||||||||||||||||||||||||||||||||
| formatDate, | ||||||||||||||||||||||||||||||||||||||
| disabled, | ||||||||||||||||||||||||||||||||||||||
| maxTagCount, | ||||||||||||||||||||||||||||||||||||||
| maxTagPlaceholder, | ||||||||||||||||||||||||||||||||||||||
| placeholder, | ||||||||||||||||||||||||||||||||||||||
| } = props; | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
|
@@ -68,8 +69,13 @@ export default function MultipleDates<DateType extends object = any>( | |||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| // ========================= Rest ========================= | ||||||||||||||||||||||||||||||||||||||
| function renderRest(omittedValues: DateType[]) { | ||||||||||||||||||||||||||||||||||||||
| const content = `+ ${omittedValues.length} ...`; | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| if (!value.length) { | ||||||||||||||||||||||||||||||||||||||
| return null; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
Contributor
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. |
||||||||||||||||||||||||||||||||||||||
| const content = | ||||||||||||||||||||||||||||||||||||||
| typeof maxTagPlaceholder === 'function' | ||||||||||||||||||||||||||||||||||||||
| ? maxTagPlaceholder(omittedValues) | ||||||||||||||||||||||||||||||||||||||
| : maxTagPlaceholder; | ||||||||||||||||||||||||||||||||||||||
| return renderSelector(content); | ||||||||||||||||||||||||||||||||||||||
|
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. 🛠️ Refactor suggestion 应使用 omittedValues 判空,避免非预期渲染 Rest 占位 这里应判断 - function renderRest(omittedValues: DateType[]) {
- if (!value.length) {
- return null;
- }
+ function renderRest(omittedValues: DateType[]) {
+ if (!omittedValues?.length) {
+ return null;
+ }
const content =
typeof maxTagPlaceholder === 'function'
? maxTagPlaceholder(omittedValues)
- : maxTagPlaceholder;
+ : (maxTagPlaceholder ?? `+ ${omittedValues.length} ...`);
return renderSelector(content);
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -12,7 +12,7 @@ import MultipleDates from './MultipleDates'; | |||||
|
|
||||||
| export interface SingleSelectorProps<DateType extends object = any> | ||||||
| extends SelectorProps<DateType>, | ||||||
| Pick<PickerProps, 'multiple' | 'maxTagCount'> { | ||||||
| Pick<PickerProps, 'multiple' | 'maxTagCount' | 'maxTagPlaceholder'> { | ||||||
| id?: string; | ||||||
|
|
||||||
| value?: DateType[]; | ||||||
|
|
@@ -75,6 +75,7 @@ function SingleSelector<DateType extends object = any>( | |||||
| onInputChange, | ||||||
| multiple, | ||||||
| maxTagCount, | ||||||
| maxTagPlaceholder = (omittedValues: string[]) => `+ ${omittedValues.length} ...`, | ||||||
|
Contributor
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
|
||||||
|
|
||||||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||||||
| // Valid | ||||||
| format, | ||||||
|
|
@@ -170,6 +171,7 @@ function SingleSelector<DateType extends object = any>( | |||||
| onRemove={onMultipleRemove} | ||||||
| formatDate={getText} | ||||||
| maxTagCount={maxTagCount} | ||||||
| maxTagPlaceholder={maxTagPlaceholder} | ||||||
| disabled={disabled} | ||||||
| removeIcon={removeIcon} | ||||||
| placeholder={placeholder} | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -155,6 +155,115 @@ describe('Picker.Multiple', () => { | |
| ).toBeFalsy(); | ||
| }); | ||
| }); | ||
| describe('maxTagPlaceholder', () => { | ||
| it('should not show maxTagPlaceholder when items count is within maxTagCount', () => { | ||
| const maxTagPlaceholder = (omittedValues: any[]) => ( | ||
| <span className="custom-max-tag-placeholder">+{omittedValues.length} more</span> | ||
| ); | ||
|
|
||
| const { container } = render( | ||
| <DayPicker | ||
| multiple | ||
| maxTagCount={3} | ||
| maxTagPlaceholder={maxTagPlaceholder} | ||
| defaultValue={[getDay('2000-01-01'), getDay('2000-01-02')]} | ||
| />, | ||
| ); | ||
|
|
||
| // Should show all items, no placeholder | ||
| expect(container.querySelectorAll('.rc-picker-selection-item')).toHaveLength(2); | ||
| expect(container.querySelector('.custom-max-tag-placeholder')).toBeFalsy(); | ||
| }); | ||
|
|
||
| it('should show maxTagPlaceholder when items count exceeds maxTagCount', () => { | ||
| const maxTagPlaceholder = (omittedValues: any[]) => ( | ||
| <span className="custom-max-tag-placeholder">+{omittedValues.length} items</span> | ||
| ); | ||
|
|
||
| const { container } = render( | ||
| <DayPicker | ||
| multiple | ||
| maxTagCount={2} | ||
| maxTagPlaceholder={maxTagPlaceholder} | ||
| defaultValue={[ | ||
| getDay('2000-01-01'), | ||
| getDay('2000-01-02'), | ||
| getDay('2000-01-03'), | ||
| getDay('2000-01-04'), | ||
| ]} | ||
| />, | ||
| ); | ||
|
|
||
| // Should show maxTagCount items + placeholder | ||
| expect(container.querySelectorAll('.rc-picker-selection-item')).toHaveLength(3); | ||
| expect(container.querySelector('.custom-max-tag-placeholder').textContent).toBe('+2 items'); | ||
| }); | ||
|
|
||
| it('should work with custom maxTagPlaceholder component', () => { | ||
| const CustomPlaceholder = ({ omittedValues }: { omittedValues: any[] }) => ( | ||
| <div className="custom-placeholder-wrapper"> | ||
| <span className="omitted-count">{omittedValues.length}</span> | ||
| <span className="omitted-text">hidden dates</span> | ||
| </div> | ||
| ); | ||
|
|
||
| const { container } = render( | ||
| <DayPicker | ||
| multiple | ||
| maxTagCount={1} | ||
| maxTagPlaceholder={(omittedValues) => <CustomPlaceholder omittedValues={omittedValues} />} | ||
| defaultValue={[getDay('2000-01-01'), getDay('2000-01-02'), getDay('2000-01-03')]} | ||
| />, | ||
| ); | ||
|
|
||
| expect(container.querySelector('.custom-placeholder-wrapper')).toBeTruthy(); | ||
| expect(container.querySelector('.omitted-count').textContent).toBe('2'); | ||
| expect(container.querySelector('.omitted-text').textContent).toBe('hidden dates'); | ||
| }); | ||
|
|
||
| it('should handle maxTagCount edge cases1', () => { | ||
| const maxTagPlaceholder = (omittedValues: any[]) => ( | ||
| <span className="edge-case-placeholder">+{omittedValues.length}</span> | ||
| ); | ||
|
|
||
| // Test maxTagCount = 0 | ||
| const { container, rerender } = render( | ||
|
Contributor
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. |
||
| <DayPicker | ||
| multiple | ||
| maxTagCount={0} | ||
| maxTagPlaceholder={maxTagPlaceholder} | ||
| defaultValue={[getDay('2000-01-01')]} | ||
| />, | ||
| ); | ||
| expect(container.querySelectorAll('.rc-picker-selection-item')).toHaveLength(1); | ||
| expect(container.querySelector('.edge-case-placeholder')).toBeTruthy(); | ||
| expect(container.querySelector('.edge-case-placeholder').textContent).toBe('+1'); | ||
| }); | ||
|
|
||
| it('should pass correct omittedValues to maxTagPlaceholder', () => { | ||
| const maxTagPlaceholder = jest.fn((omittedValues) => ( | ||
| <span className="test-placeholder">+{omittedValues.length}</span> | ||
| )); | ||
|
|
||
| const values = [ | ||
| getDay('2000-01-01'), | ||
| getDay('2000-01-02'), | ||
| getDay('2000-01-03'), | ||
| getDay('2000-01-04'), | ||
| ]; | ||
|
|
||
| render( | ||
| <DayPicker | ||
| multiple | ||
| maxTagCount={2} | ||
| maxTagPlaceholder={maxTagPlaceholder} | ||
| defaultValue={values} | ||
| />, | ||
| ); | ||
|
|
||
| expect(maxTagPlaceholder).toHaveBeenCalledWith([values[2], values[3]]); | ||
| }); | ||
| }); | ||
|
|
||
| it('click year panel should not select', () => { | ||
| const onChange = jest.fn(); | ||
|
|
||
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.
maxTagPlaceholder函数存在两个问题:value参数的类型是any[],这太宽泛了。考虑到这个文件的上下文,它应该被更具体地类型化为dayjs.Dayjs[]。map函数中生成的<li>元素缺少key属性。在 React 中,列表中的每个元素都必须有一个唯一的key,以避免重新渲染时的性能问题和潜在的 bug。