Skip to content

Commit 49163c0

Browse files
authored
site: show the clear element in Select semantic preview (#59245)
* site: show the clear element in Select semantic preview * fix(AutoComplete): pass through clear semantic classNames and styles
1 parent d30c7ad commit 49163c0

9 files changed

Lines changed: 84 additions & 22 deletions

File tree

‎.dumi/theme/common/SelectSemanticTemplate.tsx‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import React from 'react';
1+
import React, { use } from 'react';
22
import { Flex, Segmented } from 'antd';
33

44
import useLocale from '../../hooks/useLocale';
5-
import SemanticPreview from './SemanticPreview';
5+
import SemanticPreview, { SemanticActiveContext } from './SemanticPreview';
66

77
export const locales = {
88
cn: {
@@ -67,14 +67,18 @@ const Block: React.FC<BlockProps> = ({
6767
...props
6868
}) => {
6969
const divRef = React.useRef<HTMLDivElement>(null);
70+
const activeSemantic = use(SemanticActiveContext);
7071
// 多选模式下,优先使用 multipleProps 中的 defaultValue
7172
const multipleDefaultValue = (multipleProps as any)?.defaultValue;
7273
const initialValue = mode === 'single' ? defaultValue : multipleDefaultValue;
7374
const [value, setValue] = React.useState(initialValue);
75+
const [valueMode, setValueMode] = React.useState(mode);
7476

75-
React.useEffect(() => {
76-
setValue(mode === 'single' ? defaultValue : multipleDefaultValue);
77-
}, [mode, defaultValue, multipleDefaultValue]);
77+
// 单选和多选的取值结构不同,切换模式时同步重置,避免把上一个模式的值渲染出去
78+
if (valueMode !== mode) {
79+
setValueMode(mode);
80+
setValue(initialValue);
81+
}
7882

7983
return (
8084
<Flex
@@ -98,12 +102,12 @@ const Block: React.FC<BlockProps> = ({
98102
{...props}
99103
open
100104
placement="bottomLeft"
101-
value={value}
105+
value={activeSemantic === 'placeholder' ? null : value}
102106
onChange={setValue}
103107
getPopupContainer={() => divRef.current}
104108
options={options}
105109
{...(mode === 'multiple' ? multipleProps : {})}
106-
styles={{ popup: { zIndex: 1 } }}
110+
styles={{ popup: { zIndex: 1 }, clear: { opacity: 1 }, suffix: { marginInlineEnd: 24 } }}
107111
maxTagCount={process.env.NODE_ENV === 'test' ? 1 : 'responsive'}
108112
placeholder="Please select"
109113
allowClear

‎.dumi/theme/common/SemanticPreview.tsx‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,14 @@ function HighlightExample(props: {
107107
return Prism.highlight(code, Prism.languages.jsx || Prism.languages.javascript, 'jsx');
108108
}, [componentName, itemsAPI, semanticName]);
109109

110-
return (
111-
<div dangerouslySetInnerHTML={{ __html: highlightCode }} />
112-
);
110+
return <div dangerouslySetInnerHTML={{ __html: highlightCode }} />;
113111
}
114112

115113
const getMarkClassName = (semanticKey: string) =>
116114
`semantic-mark-${semanticKey}`.replace(/\./g, '-');
117115

116+
export const SemanticActiveContext = React.createContext<string | null>(null);
117+
118118
export interface SemanticPreviewProps {
119119
componentName: string;
120120
semantics: { name: string; desc: string; version?: string }[];
@@ -186,7 +186,9 @@ const SemanticPreview: React.FC<SemanticPreviewProps> = (props) => {
186186
className={clsx(styles.colWrap, padding === false && styles.colWrapPaddingLess)}
187187
style={style}
188188
>
189-
<ConfigProvider theme={{ token: { motion } }}>{cloneNode}</ConfigProvider>
189+
<ConfigProvider theme={{ token: { motion } }}>
190+
<SemanticActiveContext value={mergedSemantic}>{cloneNode}</SemanticActiveContext>
191+
</ConfigProvider>
190192
</Col>
191193
<Col span={8}>
192194
<ul className={clsx(styles.listWrap)}>

‎components/auto-complete/AutoComplete.tsx‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export type AutoCompleteSemanticType = {
2727
input?: string;
2828
placeholder?: string;
2929
content?: string;
30+
clear?: string;
3031
popup?: NonNullable<SelectSemanticAllType['classNames']>['popup'];
3132
};
3233
styles?: {
@@ -35,6 +36,7 @@ export type AutoCompleteSemanticType = {
3536
input?: React.CSSProperties;
3637
placeholder?: React.CSSProperties;
3738
content?: React.CSSProperties;
39+
clear?: React.CSSProperties;
3840
popup?: NonNullable<SelectSemanticAllType['styles']>['popup'];
3941
};
4042
};
@@ -235,6 +237,7 @@ const AutoComplete: React.ForwardRefRenderFunction<RefSelectProps, AutoCompleteP
235237
input: mergedClassNames.input,
236238
placeholder: mergedClassNames.placeholder,
237239
content: mergedClassNames.content,
240+
clear: mergedClassNames.clear,
238241
popup: {
239242
root: clsx(popupClassName, dropdownClassName, mergedClassNames.popup.root),
240243
list: mergedClassNames.popup.list,
@@ -251,6 +254,7 @@ const AutoComplete: React.ForwardRefRenderFunction<RefSelectProps, AutoCompleteP
251254
prefix: mergedStyles.prefix,
252255
placeholder: mergedStyles.placeholder,
253256
content: mergedStyles.content,
257+
clear: mergedStyles.clear,
254258
popup: {
255259
root: { ...dropdownStyle, ...mergedStyles.popup.root },
256260
list: mergedStyles.popup.list,

‎components/auto-complete/__tests__/__snapshots__/demo-semantic.test.tsx.snap‎

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ exports[`renders components/auto-complete/demo/_semantic.tsx correctly 1`] = `
1616
style="position: absolute; margin-bottom: 80px;"
1717
>
1818
<div
19-
class="ant-select ant-select-outlined ant-select-auto-complete semantic-mark-root css-var-test-id ant-select-css-var ant-select-single ant-select-open ant-select-show-search"
19+
class="ant-select ant-select-outlined ant-select-auto-complete semantic-mark-root css-var-test-id ant-select-css-var ant-select-single ant-select-allow-clear ant-select-open ant-select-show-search"
2020
style="width: 200px;"
2121
>
2222
<div
@@ -25,15 +25,12 @@ exports[`renders components/auto-complete/demo/_semantic.tsx correctly 1`] = `
2525
prefix
2626
</div>
2727
<div
28-
class="ant-select-content semantic-mark-content"
28+
class="ant-select-content ant-select-content-has-value ant-select-content-has-search-value semantic-mark-content"
29+
title="aojunhao123"
2930
>
30-
<div
31-
class="ant-select-placeholder semantic-mark-placeholder"
32-
>
33-
Please select
34-
</div>
31+
aojunhao123
3532
<input
36-
aria-activedescendant="test-id_list_-1"
33+
aria-activedescendant="test-id_list_0"
3734
aria-autocomplete="list"
3835
aria-controls="test-id_list"
3936
aria-expanded="true"
@@ -44,9 +41,36 @@ exports[`renders components/auto-complete/demo/_semantic.tsx correctly 1`] = `
4441
id="test-id"
4542
role="combobox"
4643
type="text"
47-
value=""
44+
value="aojunhao123"
4845
/>
4946
</div>
47+
<button
48+
aria-label="Clear"
49+
class="ant-select-clear semantic-mark-clear"
50+
style="opacity: 1;"
51+
type="button"
52+
>
53+
<span
54+
aria-label="close-circle"
55+
class="anticon anticon-close-circle"
56+
role="img"
57+
>
58+
<svg
59+
aria-hidden="true"
60+
data-icon="close-circle"
61+
fill="currentColor"
62+
fill-rule="evenodd"
63+
focusable="false"
64+
height="1em"
65+
viewBox="64 64 896 896"
66+
width="1em"
67+
>
68+
<path
69+
d="M512 64c247.4 0 448 200.6 448 448S759.4 960 512 960 64 759.4 64 512 264.6 64 512 64zm127.98 274.82h-.04l-.08.06L512 466.75 384.14 338.88c-.04-.05-.06-.06-.08-.06a.12.12 0 00-.07 0c-.03 0-.05.01-.09.05l-45.02 45.02a.2.2 0 00-.05.09.12.12 0 000 .07v.02a.27.27 0 00.06.06L466.75 512 338.88 639.86c-.05.04-.06.06-.06.08a.12.12 0 000 .07c0 .03.01.05.05.09l45.02 45.02a.2.2 0 00.09.05.12.12 0 00.07 0c.02 0 .04-.01.08-.05L512 557.25l127.86 127.87c.04.04.06.05.08.05a.12.12 0 00.07 0c.03 0 .05-.01.09-.05l45.02-45.02a.2.2 0 00.05-.09.12.12 0 000-.07v-.02a.27.27 0 00-.05-.06L557.25 512l127.87-127.86c.04-.04.05-.06.05-.08a.12.12 0 000-.07c0-.03-.01-.05-.05-.09l-45.02-45.02a.2.2 0 00-.09-.05.12.12 0 00-.07 0z"
70+
/>
71+
</svg>
72+
</span>
73+
</button>
5074
</div>
5175
<div
5276
class="ant-select-dropdown ant-slide-up-appear ant-slide-up-appear-prepare ant-slide-up semantic-mark-popup-root css-var-test-id ant-select-css-var ant-select-dropdown-placement-bottomLeft"
@@ -60,12 +84,20 @@ exports[`renders components/auto-complete/demo/_semantic.tsx correctly 1`] = `
6084
>
6185
<div
6286
aria-label="aojunhao123"
63-
aria-selected="false"
87+
aria-selected="true"
6488
id="test-id_list_0"
6589
role="option"
6690
>
6791
aojunhao123
6892
</div>
93+
<div
94+
aria-label="thinkasany"
95+
aria-selected="false"
96+
id="test-id_list_1"
97+
role="option"
98+
>
99+
thinkasany
100+
</div>
69101
</div>
70102
<div
71103
class="ant-select-dropdown-list semantic-mark-popup-list"
@@ -82,7 +114,7 @@ exports[`renders components/auto-complete/demo/_semantic.tsx correctly 1`] = `
82114
>
83115
<div
84116
aria-disabled="false"
85-
class="ant-select-item ant-select-item-option semantic-mark-popup-listItem"
117+
class="ant-select-item ant-select-item-option semantic-mark-popup-listItem ant-select-item-option-active"
86118
title="aojunhao123"
87119
>
88120
<div

‎components/auto-complete/__tests__/semantic.test.tsx‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,20 @@ describe('AutoComplete.Semantic', () => {
5252
);
5353
});
5454

55+
it('should support clear classNames and styles', () => {
56+
const { container } = render(
57+
<AutoComplete
58+
options={[{ label: '123', value: '123' }]}
59+
classNames={{ clear: 'custom-clear' }}
60+
styles={{ clear: { opacity: 1 } }}
61+
defaultValue="123"
62+
allowClear
63+
/>,
64+
);
65+
66+
expect(container.querySelector('.custom-clear')).toHaveStyle({ opacity: 1 });
67+
});
68+
5569
it('should support function classNames and styles', () => {
5670
const classNamesFn = (info: { props: any }) => {
5771
if (info.props.status === 'error') {

‎components/auto-complete/demo/_semantic.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const App: React.FC = () => {
2323
component={AutoComplete}
2424
componentName="AutoComplete"
2525
prefix="prefix"
26+
defaultValue="aojunhao123"
2627
style={{ width: 200 }}
2728
options={options}
2829
onSearch={(text: string) => setOptions(getPanelValue(text))}

‎components/cascader/__tests__/__snapshots__/demo-semantic.test.tsx.snap‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ exports[`renders components/cascader/demo/_semantic.tsx correctly 1`] = `
135135
</div>
136136
<div
137137
class="ant-select-suffix semantic-mark-suffix"
138+
style="margin-inline-end: 24px;"
138139
>
139140
<span
140141
aria-label="down"
@@ -159,6 +160,7 @@ exports[`renders components/cascader/demo/_semantic.tsx correctly 1`] = `
159160
<button
160161
aria-label="Clear"
161162
class="ant-select-clear semantic-mark-clear"
163+
style="opacity: 1;"
162164
type="button"
163165
>
164166
<span

‎components/select/__tests__/__snapshots__/demo-semantic.test.tsx.snap‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ exports[`renders components/select/demo/_semantic.tsx correctly 1`] = `
135135
</div>
136136
<div
137137
class="ant-select-suffix semantic-mark-suffix"
138+
style="margin-inline-end: 24px;"
138139
>
139140
<span
140141
aria-label="search"
@@ -159,6 +160,7 @@ exports[`renders components/select/demo/_semantic.tsx correctly 1`] = `
159160
<button
160161
aria-label="Clear"
161162
class="ant-select-clear semantic-mark-clear"
163+
style="opacity: 1;"
162164
type="button"
163165
>
164166
<span

‎components/select/demo/_semantic.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const App: React.FC = () => {
99
component={Select}
1010
componentName="Select"
1111
prefix="prefix"
12+
defaultValue="aojunhao123"
1213
style={{ width: 300 }}
1314
multipleProps={{ mode: 'multiple', defaultValue: ['aojunhao123'] }}
1415
options={[

0 commit comments

Comments
 (0)