Browse Source

Merge branch 'master' into feature-merge-master

pull/41847/head
MadCcc 2 years ago
parent
commit
6bea67fb03
  1. 12
      components/cascader/__tests__/__snapshots__/index.test.tsx.snap
  2. 9
      components/cascader/__tests__/index.test.tsx
  3. 5
      components/typography/__tests__/copy.test.tsx
  4. 3
      components/typography/__tests__/index.test.tsx
  5. 105
      docs/react/customize-theme.en-US.md
  6. 105
      docs/react/customize-theme.zh-CN.md

12
components/cascader/__tests__/__snapshots__/index.test.tsx.snap

@ -284,8 +284,8 @@ exports[`Cascader can be selected 2`] = `
exports[`Cascader can be selected 3`] = `
<div
class="ant-select-dropdown ant-slide-up-leave ant-slide-up-leave-start ant-slide-up ant-cascader-dropdown ant-select-dropdown-placement-bottomLeft"
style="left: 0px; top: 0px; box-sizing: border-box; min-width: auto; pointer-events: none;"
class="ant-select-dropdown ant-slide-up-appear ant-slide-up-appear-prepare ant-slide-up ant-cascader-dropdown ant-select-dropdown-placement-bottomLeft"
style="left: -1000vw; top: -1000vh; box-sizing: border-box; min-width: auto;"
>
<div>
<div
@ -566,8 +566,8 @@ exports[`Cascader can be selected in RTL direction 1`] = `
exports[`Cascader can be selected in RTL direction 2`] = `
<div
class="ant-select-dropdown ant-slide-up-leave ant-slide-up-leave-start ant-slide-up ant-cascader-dropdown ant-cascader-dropdown-rtl ant-select-dropdown-placement-bottomRight"
style="left: 0px; top: 0px; box-sizing: border-box; min-width: auto; pointer-events: none;"
class="ant-select-dropdown ant-slide-up-appear ant-slide-up-appear-prepare ant-slide-up ant-cascader-dropdown ant-cascader-dropdown-rtl ant-select-dropdown-placement-bottomRight"
style="left: -1000vw; top: -1000vh; box-sizing: border-box; min-width: auto;"
>
<div>
<div
@ -716,8 +716,8 @@ exports[`Cascader can be selected in RTL direction 2`] = `
exports[`Cascader can be selected in RTL direction 3`] = `
<div
class="ant-select-dropdown ant-slide-up-leave ant-slide-up-leave-start ant-slide-up ant-cascader-dropdown ant-cascader-dropdown-rtl ant-select-dropdown-placement-bottomRight"
style="left: 0px; top: 0px; box-sizing: border-box; min-width: auto; pointer-events: none;"
class="ant-select-dropdown ant-slide-up-appear ant-slide-up-appear-prepare ant-slide-up ant-cascader-dropdown ant-cascader-dropdown-rtl ant-select-dropdown-placement-bottomRight"
style="left: -1000vw; top: -1000vh; box-sizing: border-box; min-width: auto;"
>
<div>
<div

9
components/cascader/__tests__/index.test.tsx

@ -124,10 +124,7 @@ describe('Cascader', () => {
it('can be selected', () => {
const onChange = jest.fn();
const { container } = render(<Cascader options={options} onChange={onChange} />);
toggleOpen(container);
expect(isOpen(container)).toBeTruthy();
const { container } = render(<Cascader open options={options} onChange={onChange} />);
clickOption(container, 0, 0);
expect(getDropdown(container)).toMatchSnapshot();
@ -469,19 +466,17 @@ describe('Cascader', () => {
defaultValue={['zhejiang', 'hangzhou']}
onChange={onChange}
popupPlacement="bottomRight"
open
/>
</ConfigProvider>,
);
toggleOpen(container);
clickOption(container, 0, 0);
expect(getDropdown(container)).toMatchSnapshot();
toggleOpen(container);
clickOption(container, 1, 0);
expect(getDropdown(container)).toMatchSnapshot();
toggleOpen(container);
clickOption(container, 2, 0);
expect(getDropdown(container)).toMatchSnapshot();

5
components/typography/__tests__/copy.test.tsx

@ -74,10 +74,9 @@ describe('Typography copy', () => {
fireEvent.mouseEnter(wrapper.querySelectorAll('.ant-typography-copy')[0]);
if (tooltipTexts[1] !== undefined) {
const expectedInner = tooltipTexts[1] === '' ? tooltipTexts[0] : tooltipTexts[1];
await waitFor(() => {
expect(wrapper.querySelector('.ant-tooltip-inner')?.textContent).toBe(
tooltipTexts[1],
);
expect(wrapper.querySelector('.ant-tooltip-inner')?.textContent).toBe(expectedInner);
});
}

3
components/typography/__tests__/index.test.tsx

@ -173,7 +173,8 @@ describe('Typography', () => {
} else if (tooltips[0] === '' && tooltips[1]) {
expect(container.querySelector('.ant-tooltip-inner')?.textContent).toBe(tooltips[1]);
} else if (tooltips[1] === '' && tooltips[0]) {
expect(container.querySelector('.ant-tooltip-inner')?.textContent).toBe('');
// Tooltip will be hidden in this case, with content memoized
expect(container.querySelector('.ant-tooltip-inner')?.textContent).toBe(tooltips[0]);
} else {
expect(container.querySelector('.ant-tooltip-inner')?.textContent).toBe(tooltips[1]);
}

105
docs/react/customize-theme.en-US.md

@ -292,7 +292,7 @@ If you want to detach a style file into a css file, try the following schemes:
1. Installation dependency
```bash
npm install ts-node tslib --save-dev
npm install ts-node tslib cross-env --save-dev
```
2. Add `tsconfig.node.json`
@ -383,7 +383,7 @@ Take Next.js for example([example](https://github.com/ant-design/create-next-a
"start": "next start",
"lint": "next lint",
"predev": "ts-node --project ./tsconfig.node.json ./scripts/genAntdCss.tsx",
"prebuild": "ts-node --project ./tsconfig.node.json ./scripts/genAntdCss.tsx"
"prebuild": "cross-env NODE_ENV=production ts-node --project ./tsconfig.node.json ./scripts/genAntdCss.tsx"
}
}
```
@ -468,6 +468,107 @@ const cssText = extractStyle((node) => (
More about static-style-extract, see [static-style-extract](https://github.com/ant-design/static-style-extract).
#### Export the css files on demand
```tsx
// scripts/genAntdCss.tsx
import { extractStyle } from '@ant-design/cssinjs';
import type Entity from '@ant-design/cssinjs/lib/Cache';
import { createHash } from 'crypto';
import fs from 'fs';
import path from 'path';
export type DoExtraStyleOptions = {
cache: Entity;
dir?: string;
baseFileName?: string;
};
export function doExtraStyle({
cache,
dir = 'antd-output',
baseFileName = 'antd.min',
}: DoExtraStyleOptions) {
const baseDir = path.resolve(__dirname, '../../static/css');
const outputCssPath = path.join(baseDir, dir);
if (!fs.existsSync(outputCssPath)) {
fs.mkdirSync(outputCssPath, { recursive: true });
}
const css = extractStyle(cache, true);
if (!css) return '';
const md5 = createHash('md5');
const hash = md5.update(css).digest('hex');
const fileName = `${baseFileName}.${hash.substring(0, 8)}.css`;
const fullpath = path.join(outputCssPath, fileName);
const res = `_next/static/css/${dir}/${fileName}`;
if (fs.existsSync(fullpath)) return res;
fs.writeFileSync(fullpath, css);
return res;
}
```
Export on demand using the above tools in `_document.tsx`
```tsx
// _document.tsx
import { StyleProvider, createCache } from '@ant-design/cssinjs';
import Document, { DocumentContext, Head, Html, Main, NextScript } from 'next/document';
import { doExtraStyle } from '../scripts/genAntdCss';
export default class MyDocument extends Document {
static async getInitialProps(ctx: DocumentContext) {
const cache = createCache();
let fileName = '';
const originalRenderPage = ctx.renderPage;
ctx.renderPage = () =>
originalRenderPage({
enhanceApp: (App) => (props) =>
(
<StyleProvider cache={cache}>
<App {...props} />
</StyleProvider>
),
});
const initialProps = await Document.getInitialProps(ctx);
// 1.1 extract style which had been used
fileName = doExtraStyle({
cache,
});
return {
...initialProps,
styles: (
<>
{initialProps.styles}
{/* 1.2 inject css */}
{fileName && <link rel="stylesheet" href={`/${fileName}`} />}
</>
),
};
}
render() {
return (
<Html lang="en">
<Head />
<body>
<Main />
<NextScript />
</body>
</Html>
);
}
}
```
See the demo:[Export the css files on demand demo](https://github.com/ant-design/create-next-app-antd/tree/generate-css-on-demand)
### Shadow DOM Usage
Since `<style />` tag insertion is different from normal DOM in Shadow DOM scenario, you need to use `StyleProvider` of `@ant-design/cssinjs` to configure the `container` property to set the insertion position:

105
docs/react/customize-theme.zh-CN.md

@ -288,7 +288,7 @@ export default () => {
1. 安装依赖
```bash
npm install ts-node tslib --save-dev
npm install ts-node tslib cross-env --save-dev
```
2. 新增 `tsconfig.node.json` 文件
@ -379,7 +379,7 @@ fs.writeFileSync(outputPath, css);
"start": "next start",
"lint": "next lint",
"predev": "ts-node --project ./tsconfig.node.json ./scripts/genAntdCss.tsx",
"prebuild": "ts-node --project ./tsconfig.node.json ./scripts/genAntdCss.tsx"
"prebuild": "cross-env NODE_ENV=production ts-node --project ./tsconfig.node.json ./scripts/genAntdCss.tsx"
}
}
```
@ -464,6 +464,107 @@ const cssText = extractStyle((node) => (
更多`static-style-extract`的实现细节请看:[static-style-extract](https://github.com/ant-design/static-style-extract)。
#### 按需导出 css 样式文件
```tsx
// scripts/genAntdCss.tsx
import { extractStyle } from '@ant-design/cssinjs';
import type Entity from '@ant-design/cssinjs/lib/Cache';
import { createHash } from 'crypto';
import fs from 'fs';
import path from 'path';
export type DoExtraStyleOptions = {
cache: Entity;
dir?: string;
baseFileName?: string;
};
export function doExtraStyle({
cache,
dir = 'antd-output',
baseFileName = 'antd.min',
}: DoExtraStyleOptions) {
const baseDir = path.resolve(__dirname, '../../static/css');
const outputCssPath = path.join(baseDir, dir);
if (!fs.existsSync(outputCssPath)) {
fs.mkdirSync(outputCssPath, { recursive: true });
}
const css = extractStyle(cache, true);
if (!css) return '';
const md5 = createHash('md5');
const hash = md5.update(css).digest('hex');
const fileName = `${baseFileName}.${hash.substring(0, 8)}.css`;
const fullpath = path.join(outputCssPath, fileName);
const res = `_next/static/css/${dir}/${fileName}`;
if (fs.existsSync(fullpath)) return res;
fs.writeFileSync(fullpath, css);
return res;
}
```
`_document.tsx` 中使用上述工具进行按需导出:
```tsx
// _document.tsx
import { StyleProvider, createCache } from '@ant-design/cssinjs';
import Document, { DocumentContext, Head, Html, Main, NextScript } from 'next/document';
import { doExtraStyle } from '../scripts/genAntdCss';
export default class MyDocument extends Document {
static async getInitialProps(ctx: DocumentContext) {
const cache = createCache();
let fileName = '';
const originalRenderPage = ctx.renderPage;
ctx.renderPage = () =>
originalRenderPage({
enhanceApp: (App) => (props) =>
(
<StyleProvider cache={cache}>
<App {...props} />
</StyleProvider>
),
});
const initialProps = await Document.getInitialProps(ctx);
// 1.1 extract style which had been used
fileName = doExtraStyle({
cache,
});
return {
...initialProps,
styles: (
<>
{initialProps.styles}
{/* 1.2 inject css */}
{fileName && <link rel="stylesheet" href={`/${fileName}`} />}
</>
),
};
}
render() {
return (
<Html lang="en">
<Head />
<body>
<Main />
<NextScript />
</body>
</Html>
);
}
}
```
演示示例请看:[按需抽取样式示例](https://github.com/ant-design/create-next-app-antd/tree/generate-css-on-demand)
### 兼容旧版浏览器
请参考文档 [样式兼容](/docs/react/compatible-style-cn)。

Loading…
Cancel
Save