docs: replace class component with hooks (#35519)

* docs(badge): replace class component with hooks

* docs(button): replace class component with hooks

* docs(calendar): replace class component with hooks

* docs(card): replace class component with hooks

* docs(button): replace class component with hooks

* chore(deps): remove webpack devDependencies

* docs(cascader): replace class component with hooks

* docs(checkbox): replace class component with hooks

* docs(collapse): replace class component with hooks

* docs(comment): replace class component with hooks

* docs(descriptions): replace class component with hooks

* docs(config-provider): replace class component with hooks

* docs(date-picker): replace class component with hooks

* docs(drawer): replace class component with hooks

* docs(dropdown): replace class component with hooks

* docs(dropdown): replace class component with hooks

* docs(empty): replace class component with hooks

* docs(grid): replace class component with hooks

* docs(input): replace class component with hooks

* docs(input-number): replace class component with hooks

* docs(demo): fix lint error
This commit is contained in:
dingkang 2022-05-14 16:40:42 +08:00 committed by GitHub
parent e629b39c20
commit 58df74c38e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 621 additions and 737 deletions

View File

@ -19,37 +19,32 @@ import { PlusOutlined } from '@ant-design/icons';
const { Option } = Select; const { Option } = Select;
class DrawerForm extends React.Component { export default () => {
state = { visible: false }; const [visible, setVisible] = React.useState(false);
showDrawer = () => { const showDrawer = () => {
this.setState({ setVisible(true);
visible: true,
});
}; };
onClose = () => { const onClose = () => {
this.setState({ setVisible(false);
visible: false,
});
}; };
render() {
return ( return (
<> <>
<Button type="primary" onClick={this.showDrawer} icon={<PlusOutlined />}> <Button type="primary" onClick={showDrawer} icon={<PlusOutlined />}>
New account New account
</Button> </Button>
<Drawer <Drawer
title="Create a new account" title="Create a new account"
width={720} width={720}
onClose={this.onClose} onClose={onClose}
visible={this.state.visible} visible={visible}
bodyStyle={{ paddingBottom: 80 }} bodyStyle={{ paddingBottom: 80 }}
extra={ extra={
<Space> <Space>
<Button onClick={this.onClose}>Cancel</Button> <Button onClick={onClose}>Cancel</Button>
<Button onClick={this.onClose} type="primary"> <Button onClick={onClose} type="primary">
Submit Submit
</Button> </Button>
</Space> </Space>
@ -153,10 +148,7 @@ class DrawerForm extends React.Component {
</Drawer> </Drawer>
</> </>
); );
} };
}
export default () => <DrawerForm />;
``` ```
```css ```css

View File

@ -16,65 +16,54 @@ Open a new drawer on top of an existing drawer to handle multi branch tasks.
```jsx ```jsx
import { Drawer, Button } from 'antd'; import { Drawer, Button } from 'antd';
class App extends React.Component { export default () => {
state = { visible: false, childrenDrawer: false }; const [visible, setVisible] = React.useState(false);
const [childrenDrawer, setChildrenDrawer] = React.useState(false);
showDrawer = () => { const showDrawer = () => {
this.setState({ setVisible(true);
visible: true,
});
}; };
onClose = () => { const onClose = () => {
this.setState({ setVisible(false);
visible: false,
});
}; };
showChildrenDrawer = () => { const showChildrenDrawer = () => {
this.setState({ setChildrenDrawer(true);
childrenDrawer: true,
});
}; };
onChildrenDrawerClose = () => { const onChildrenDrawerClose = () => {
this.setState({ setChildrenDrawer(false);
childrenDrawer: false,
});
}; };
render() {
return ( return (
<> <>
<Button type="primary" onClick={this.showDrawer}> <Button type="primary" onClick={showDrawer}>
Open drawer Open drawer
</Button> </Button>
<Drawer <Drawer
title="Multi-level drawer" title="Multi-level drawer"
width={520} width={520}
closable={false} closable={false}
onClose={this.onClose} onClose={onClose}
visible={this.state.visible} visible={visible}
> >
<Button type="primary" onClick={this.showChildrenDrawer}> <Button type="primary" onClick={showChildrenDrawer}>
Two-level drawer Two-level drawer
</Button> </Button>
<Drawer <Drawer
title="Two-level Drawer" title="Two-level Drawer"
width={320} width={320}
closable={false} closable={false}
onClose={this.onChildrenDrawerClose} onClose={onChildrenDrawerClose}
visible={this.state.childrenDrawer} visible={childrenDrawer}
> >
This is two-level drawer This is two-level drawer
</Drawer> </Drawer>
</Drawer> </Drawer>
</> </>
); );
} };
}
export default App;
``` ```
<style> <style>

View File

@ -16,39 +16,32 @@ The Drawer can appear from any edge of the screen.
```jsx ```jsx
import { Drawer, Button, Radio, Space } from 'antd'; import { Drawer, Button, Radio, Space } from 'antd';
class App extends React.Component { export default () => {
state = { visible: false, placement: 'left' }; const [visible, setVisible] = React.useState(false);
const [placement, setPlacement] = React.useState('left');
showDrawer = () => { const showDrawer = () => {
this.setState({ setVisible(true);
visible: true,
});
}; };
onClose = () => { const onClose = () => {
this.setState({ setVisible(false);
visible: false,
});
}; };
onChange = e => { const onChange = e => {
this.setState({ setPlacement(e.target.value);
placement: e.target.value,
});
}; };
render() {
const { placement, visible } = this.state;
return ( return (
<> <>
<Space> <Space>
<Radio.Group value={placement} onChange={this.onChange}> <Radio.Group value={placement} onChange={onChange}>
<Radio value="top">top</Radio> <Radio value="top">top</Radio>
<Radio value="right">right</Radio> <Radio value="right">right</Radio>
<Radio value="bottom">bottom</Radio> <Radio value="bottom">bottom</Radio>
<Radio value="left">left</Radio> <Radio value="left">left</Radio>
</Radio.Group> </Radio.Group>
<Button type="primary" onClick={this.showDrawer}> <Button type="primary" onClick={showDrawer}>
Open Open
</Button> </Button>
</Space> </Space>
@ -56,7 +49,7 @@ class App extends React.Component {
title="Basic Drawer" title="Basic Drawer"
placement={placement} placement={placement}
closable={false} closable={false}
onClose={this.onClose} onClose={onClose}
visible={visible} visible={visible}
key={placement} key={placement}
> >
@ -66,8 +59,5 @@ class App extends React.Component {
</Drawer> </Drawer>
</> </>
); );
} };
}
export default App;
``` ```

View File

@ -16,27 +16,22 @@ Render in current dom. custom container, check `getContainer`.
```jsx ```jsx
import { Drawer, Button } from 'antd'; import { Drawer, Button } from 'antd';
class App extends React.Component { export default () => {
state = { visible: false }; const [visible, setVisible] = React.useState(false);
showDrawer = () => { const showDrawer = () => {
this.setState({ setVisible(true);
visible: true,
});
}; };
onClose = () => { const onClose = () => {
this.setState({ setVisible(false);
visible: false,
});
}; };
render() {
return ( return (
<div className="site-drawer-render-in-current-wrapper"> <div className="site-drawer-render-in-current-wrapper">
Render in this Render in this
<div style={{ marginTop: 16 }}> <div style={{ marginTop: 16 }}>
<Button type="primary" onClick={this.showDrawer}> <Button type="primary" onClick={showDrawer}>
Open Open
</Button> </Button>
</div> </div>
@ -44,8 +39,8 @@ class App extends React.Component {
title="Basic Drawer" title="Basic Drawer"
placement="right" placement="right"
closable={false} closable={false}
onClose={this.onClose} onClose={onClose}
visible={this.state.visible} visible={visible}
getContainer={false} getContainer={false}
style={{ position: 'absolute' }} style={{ position: 'absolute' }}
> >
@ -53,10 +48,7 @@ class App extends React.Component {
</Drawer> </Drawer>
</div> </div>
); );
} };
}
export default App;
``` ```
```css ```css

View File

@ -23,22 +23,17 @@ const DescriptionItem = ({ title, content }) => (
</div> </div>
); );
class App extends React.Component { export default () => {
state = { visible: false }; const [visible, setVisible] = React.useState(false);
showDrawer = () => { const showDrawer = () => {
this.setState({ setVisible(true);
visible: true,
});
}; };
onClose = () => { const onClose = () => {
this.setState({ setVisible(false);
visible: false,
});
}; };
render() {
return ( return (
<> <>
<List <List
@ -55,7 +50,7 @@ class App extends React.Component {
<List.Item <List.Item
key={item.id} key={item.id}
actions={[ actions={[
<a onClick={this.showDrawer} key={`a-${item.id}`}> <a onClick={showDrawer} key={`a-${item.id}`}>
View Profile View Profile
</a>, </a>,
]} ]}
@ -70,13 +65,7 @@ class App extends React.Component {
</List.Item> </List.Item>
)} )}
/> />
<Drawer <Drawer width={640} placement="right" closable={false} onClose={onClose} visible={visible}>
width={640}
placement="right"
closable={false}
onClose={this.onClose}
visible={this.state.visible}
>
<p className="site-description-item-profile-p" style={{ marginBottom: 24 }}> <p className="site-description-item-profile-p" style={{ marginBottom: 24 }}>
User Profile User Profile
</p> </p>
@ -164,10 +153,7 @@ class App extends React.Component {
</Drawer> </Drawer>
</> </>
); );
} };
}
export default App;
``` ```
```css ```css

View File

@ -27,25 +27,23 @@ const menu = (
]} ]}
/> />
); );
class App extends React.Component { export default () => {
state = { const [loadings, setLodings] = React.useState([]);
loadings: [],
};
enterLoading = index => { const enterLoading = index => {
const newLoadings = [...this.state.loadings]; const newLoadings = [...loadings];
newLoadings[index] = true; newLoadings[index] = true;
this.setState({ setLodings(newLoadings);
loadings: newLoadings,
});
setTimeout(() => { setTimeout(() => {
newLoadings[index] = false; setLodings(prevLoadings => {
this.setState({ loadings: newLoadings }); const temLoadings = [...prevLoadings];
temLoadings[index] = false;
return temLoadings;
});
}, 6000); }, 6000);
}; };
render() {
const { loadings } = this.state;
return ( return (
<Space direction="vertical"> <Space direction="vertical">
<Dropdown.Button type="primary" loading overlay={menu}> <Dropdown.Button type="primary" loading overlay={menu}>
@ -58,7 +56,7 @@ class App extends React.Component {
type="primary" type="primary"
loading={loadings[0]} loading={loadings[0]}
overlay={menu} overlay={menu}
onClick={() => this.enterLoading(0)} onClick={() => enterLoading(0)}
> >
Submit Submit
</Dropdown.Button> </Dropdown.Button>
@ -66,13 +64,11 @@ class App extends React.Component {
icon={<DownOutlined />} icon={<DownOutlined />}
loading={loadings[1]} loading={loadings[1]}
overlay={menu} overlay={menu}
onClick={() => this.enterLoading(1)} onClick={() => enterLoading(1)}
> >
Submit Submit
</Dropdown.Button> </Dropdown.Button>
</Space> </Space>
); );
} };
}
export default App;
``` ```

View File

@ -17,25 +17,22 @@ The default is to close the menu when you click on menu items, this feature can
import { Menu, Dropdown, Space } from 'antd'; import { Menu, Dropdown, Space } from 'antd';
import { DownOutlined } from '@ant-design/icons'; import { DownOutlined } from '@ant-design/icons';
class OverlayVisible extends React.Component { export default () => {
state = { const [visible, setVisible] = React.useState(false);
visible: false,
};
handleMenuClick = e => { const handleMenuClick = e => {
if (e.key === '3') { if (e.key === '3') {
this.setState({ visible: false }); setVisible(false);
} }
}; };
handleVisibleChange = flag => { const handleVisibleChange = flag => {
this.setState({ visible: flag }); setVisible(flag);
}; };
render() {
const menu = ( const menu = (
<Menu <Menu
onClick={this.handleMenuClick} onClick={handleMenuClick}
items={[ items={[
{ {
label: 'Clicking me will not close the menu.', label: 'Clicking me will not close the menu.',
@ -52,12 +49,9 @@ class OverlayVisible extends React.Component {
]} ]}
/> />
); );
return ( return (
<Dropdown <Dropdown overlay={menu} onVisibleChange={handleVisibleChange} visible={visible}>
overlay={menu}
onVisibleChange={this.handleVisibleChange}
visible={this.state.visible}
>
<a onClick={e => e.preventDefault()}> <a onClick={e => e.preventDefault()}>
<Space> <Space>
Hover me Hover me
@ -66,8 +60,5 @@ class OverlayVisible extends React.Component {
</a> </a>
</Dropdown> </Dropdown>
); );
} };
}
export default () => <OverlayVisible />;
``` ```

View File

@ -36,21 +36,17 @@ const customizeRenderEmpty = () => (
const style = { width: 200 }; const style = { width: 200 };
class Demo extends React.Component { export default () => {
state = { const [customize, setCustomize] = React.useState(false);
customize: false,
};
render() {
const { customize } = this.state;
return ( return (
<div> <div>
<Switch <Switch
unCheckedChildren="default" unCheckedChildren="default"
checkedChildren="customize" checkedChildren="customize"
checked={customize} checked={customize}
onChange={val => { onChange={value => {
this.setState({ customize: val }); setCustomize(value);
}} }}
/> />
@ -93,10 +89,7 @@ class Demo extends React.Component {
</ConfigProvider> </ConfigProvider>
</div> </div>
); );
} };
}
export default Demo;
``` ```
<style> <style>

View File

@ -30,27 +30,26 @@ const colCounts = {};
colCounts[i] = value; colCounts[i] = value;
}); });
class App extends React.Component { export default () => {
state = { const [state, setState] = React.useState({
gutterKey: 1, gutterKey: 1,
vgutterKey: 1, vgutterKey: 1,
colCountKey: 2, colCountKey: 2,
});
const onGutterChange = gutterKey => {
setState({ ...state, gutterKey });
}; };
onGutterChange = gutterKey => { const onVGutterChange = vgutterKey => {
this.setState({ gutterKey }); setState({ ...state, vgutterKey });
}; };
onVGutterChange = vgutterKey => { const onColCountChange = colCountKey => {
this.setState({ vgutterKey }); setState({ ...state, colCountKey });
}; };
onColCountChange = colCountKey => { const { gutterKey, vgutterKey, colCountKey } = state;
this.setState({ colCountKey });
};
render() {
const { gutterKey, vgutterKey, colCountKey } = this.state;
const cols = []; const cols = [];
const colCount = colCounts[colCountKey]; const colCount = colCounts[colCountKey];
let colCode = ''; let colCode = '';
@ -62,6 +61,7 @@ class App extends React.Component {
); );
colCode += ` <Col span={${24 / colCount}} />\n`; colCode += ` <Col span={${24 / colCount}} />\n`;
} }
return ( return (
<> <>
<span>Horizontal Gutter (px): </span> <span>Horizontal Gutter (px): </span>
@ -70,7 +70,7 @@ class App extends React.Component {
min={0} min={0}
max={Object.keys(gutters).length - 1} max={Object.keys(gutters).length - 1}
value={gutterKey} value={gutterKey}
onChange={this.onGutterChange} onChange={onGutterChange}
marks={gutters} marks={gutters}
step={null} step={null}
tipFormatter={value => gutters[value]} tipFormatter={value => gutters[value]}
@ -82,7 +82,7 @@ class App extends React.Component {
min={0} min={0}
max={Object.keys(vgutters).length - 1} max={Object.keys(vgutters).length - 1}
value={vgutterKey} value={vgutterKey}
onChange={this.onVGutterChange} onChange={onVGutterChange}
marks={vgutters} marks={vgutters}
step={null} step={null}
tipFormatter={value => vgutters[value]} tipFormatter={value => vgutters[value]}
@ -94,7 +94,7 @@ class App extends React.Component {
min={0} min={0}
max={Object.keys(colCounts).length - 1} max={Object.keys(colCounts).length - 1}
value={colCountKey} value={colCountKey}
onChange={this.onColCountChange} onChange={onColCountChange}
marks={colCounts} marks={colCounts}
step={null} step={null}
tipFormatter={value => colCounts[value]} tipFormatter={value => colCounts[value]}
@ -110,10 +110,7 @@ class App extends React.Component {
<pre className="demo-code">{`<Row gutter={[${gutters[gutterKey]}, ${vgutters[vgutterKey]}]}>\n${colCode}</Row>`}</pre> <pre className="demo-code">{`<Row gutter={[${gutters[gutterKey]}, ${vgutters[vgutterKey]}]}>\n${colCode}</Row>`}</pre>
</> </>
); );
} };
}
export default App;
``` ```
```css ```css

View File

@ -16,30 +16,22 @@ Click the button to toggle between available and disabled states.
```jsx ```jsx
import { InputNumber, Button } from 'antd'; import { InputNumber, Button } from 'antd';
class App extends React.Component { export default () => {
state = { const [disabled, setDisabled] = React.useState(true);
disabled: true,
const toggle = () => {
setDisabled(!disabled);
}; };
toggle = () => {
this.setState({
disabled: !this.state.disabled,
});
};
render() {
return ( return (
<> <>
<InputNumber min={1} max={10} disabled={this.state.disabled} defaultValue={3} /> <InputNumber min={1} max={10} disabled={disabled} defaultValue={3} />
<div style={{ marginTop: 20 }}> <div style={{ marginTop: 20 }}>
<Button onClick={this.toggle} type="primary"> <Button onClick={toggle} type="primary">
Toggle disabled Toggle disabled
</Button> </Button>
</div> </div>
</> </>
); );
} };
}
export default App;
``` ```

View File

@ -18,18 +18,14 @@ import { Input } from 'antd';
const { TextArea } = Input; const { TextArea } = Input;
class Demo extends React.Component { export default () => {
state = { const [value, setValue] = React.useState();
value: '',
};
onChange = ({ target: { value } }) => { // eslint-disable-next-line @typescript-eslint/no-shadow
this.setState({ value }); const onChange = ({ target: { value } }) => {
setValue(value);
}; };
render() {
const { value } = this.state;
return ( return (
<> <>
<TextArea placeholder="Autosize height based on content lines" autoSize /> <TextArea placeholder="Autosize height based on content lines" autoSize />
@ -41,14 +37,11 @@ class Demo extends React.Component {
<div style={{ margin: '24px 0' }} /> <div style={{ margin: '24px 0' }} />
<TextArea <TextArea
value={value} value={value}
onChange={this.onChange} onChange={onChange}
placeholder="Controlled autosize" placeholder="Controlled autosize"
autoSize={{ minRows: 3, maxRows: 5 }} autoSize={{ minRows: 3, maxRows: 5 }}
/> />
</> </>
); );
} };
}
export default Demo;
``` ```

View File

@ -22,28 +22,17 @@ const { TextArea } = Input;
const defaultValue = const defaultValue =
'The autoSize property applies to textarea nodes, and only the height changes automatically. In addition, autoSize can be set to an object, specifying the minimum number of rows and the maximum number of rows. The autoSize property applies to textarea nodes, and only the height changes automatically. In addition, autoSize can be set to an object, specifying the minimum number of rows and the maximum number of rows.'; 'The autoSize property applies to textarea nodes, and only the height changes automatically. In addition, autoSize can be set to an object, specifying the minimum number of rows and the maximum number of rows. The autoSize property applies to textarea nodes, and only the height changes automatically. In addition, autoSize can be set to an object, specifying the minimum number of rows and the maximum number of rows.';
class Demo extends React.Component { export default () => {
state = { const [autoResize, setAutoResize] = React.useState(false);
autoResize: false,
};
render() {
const { autoResize } = this.state;
return ( return (
<> <>
<Button <Button onClick={() => setAutoResize(!autoResize)} style={{ marginBottom: 16 }}>
onClick={() => this.setState({ autoResize: !autoResize })}
style={{ marginBottom: 16 }}
>
Auto Resize: {String(autoResize)} Auto Resize: {String(autoResize)}
</Button> </Button>
<TextArea rows={4} autoSize={autoResize} defaultValue={defaultValue} /> <TextArea rows={4} autoSize={autoResize} defaultValue={defaultValue} />
<TextArea allowClear style={{ width: 93 }} /> <TextArea allowClear style={{ width: 93 }} />
</> </>
); );
} };
}
export default Demo;
``` ```

View File

@ -20,18 +20,19 @@ function formatNumber(value) {
return new Intl.NumberFormat().format(value); return new Intl.NumberFormat().format(value);
} }
class NumericInput extends React.Component { const NumericInput = props => {
onChange = e => { const { value, onBlur, onChange } = props;
const { value } = e.target;
const handleChange = e => {
const inputValue = e.target.value;
const reg = /^-?\d*(\.\d*)?$/; const reg = /^-?\d*(\.\d*)?$/;
if ((!isNaN(value) && reg.test(value)) || value === '' || value === '-') { if ((!isNaN(inputValue) && reg.test(inputValue)) || inputValue === '' || inputValue === '-') {
this.props.onChange(value); onChange(inputValue);
} }
}; };
// '.' at the end or only '-' in the input box. // '.' at the end or only '-' in the input box.
onBlur = () => { const handleBlur = () => {
const { value, onBlur, onChange } = this.props;
let valueTemp = value; let valueTemp = value;
if (value.charAt(value.length - 1) === '.' || value === '-') { if (value.charAt(value.length - 1) === '.' || value === '-') {
valueTemp = value.slice(0, -1); valueTemp = value.slice(0, -1);
@ -42,50 +43,33 @@ class NumericInput extends React.Component {
} }
}; };
render() {
const { value } = this.props;
const title = value ? ( const title = value ? (
<span className="numeric-input-title">{value !== '-' ? formatNumber(value) : '-'}</span> <span className="numeric-input-title">{value !== '-' ? formatNumber(value) : '-'}</span>
) : ( ) : (
'Input a number' 'Input a number'
); );
return ( return (
<Tooltip <Tooltip trigger={['focus']} title={title} placement="topLeft" overlayClassName="numeric-input">
trigger={['focus']}
title={title}
placement="topLeft"
overlayClassName="numeric-input"
>
<Input <Input
{...this.props} {...props}
onChange={this.onChange} onChange={handleChange}
onBlur={this.onBlur} onBlur={handleBlur}
placeholder="Input a number" placeholder="Input a number"
maxLength={25} maxLength={25}
/> />
</Tooltip> </Tooltip>
); );
}
}
class NumericInputDemo extends React.Component {
constructor(props) {
super(props);
this.state = { value: '' };
}
onChange = value => {
this.setState({ value });
}; };
render() { export default () => {
return ( const [value, setValue] = React.useState();
<NumericInput style={{ width: 120 }} value={this.state.value} onChange={this.onChange} />
);
}
}
export default () => <NumericInputDemo />; const onChange = val => {
setValue(val);
};
return <NumericInput style={{ width: 120 }} value={value} onChange={onChange} />;
};
``` ```
```css ```css