|
1 | 1 | /* |
2 | | - * Copyright 2017 Palantir Technologies, Inc. All rights reserved. |
| 2 | + * Copyright 2024 Palantir Technologies, Inc. All rights reserved. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
|
16 | 16 |
|
17 | 17 | import * as React from "react"; |
18 | 18 |
|
19 | | -import { Button, Card, Classes, type Elevation, FormGroup, H5, Slider, Switch } from "@blueprintjs/core"; |
20 | | -import { Example, type ExampleProps } from "@blueprintjs/docs-theme"; |
| 19 | +import { Button, Card, Classes, Elevation, FormGroup, H5, Slider, Switch } from "@blueprintjs/core"; |
| 20 | +import { Example, type ExampleProps, handleBooleanChange } from "@blueprintjs/docs-theme"; |
21 | 21 |
|
22 | | -export interface CardExampleState { |
23 | | - compact: boolean; |
24 | | - elevation: Elevation; |
25 | | - interactive: boolean; |
26 | | - selected: boolean; |
27 | | -} |
| 22 | +const MAX_ELEVATION = 4; |
28 | 23 |
|
29 | | -export class CardExample extends React.PureComponent<ExampleProps, CardExampleState> { |
30 | | - public state: CardExampleState = { |
31 | | - compact: false, |
32 | | - elevation: 0, |
33 | | - interactive: false, |
34 | | - selected: false, |
35 | | - }; |
| 24 | +export const CardExample: React.FC<ExampleProps> = props => { |
| 25 | + const [compact, setCompact] = React.useState(false); |
| 26 | + const [elevation, setElevation] = React.useState<Elevation>(Elevation.ZERO); |
| 27 | + const [interactive, setInteractive] = React.useState(false); |
| 28 | + const [selected, setSelected] = React.useState(false); |
36 | 29 |
|
37 | | - public render() { |
38 | | - return ( |
39 | | - <Example options={this.renderOptions()} {...this.props}> |
40 | | - <Card {...this.state}> |
41 | | - <H5>Analytical applications</H5> |
42 | | - <p> |
43 | | - User interfaces that enable people to interact smoothly with data, ask better questions, and |
44 | | - make better decisions. |
45 | | - </p> |
46 | | - <Button text="Explore products" className={Classes.BUTTON} /> |
47 | | - </Card> |
48 | | - </Example> |
49 | | - ); |
50 | | - } |
| 30 | + const handleElevationChange = React.useCallback((value: number) => setElevation(value as Elevation), []); |
51 | 31 |
|
52 | | - private renderOptions = () => ( |
| 32 | + const options = ( |
53 | 33 | <> |
54 | 34 | <H5>Props</H5> |
55 | | - <Switch checked={this.state.interactive} label="Interactive" onChange={this.handleInteractiveChange} /> |
56 | | - {this.state.interactive && ( |
57 | | - <Switch checked={this.state.selected} label="Selected" onChange={this.handleSelectedChange} /> |
58 | | - )} |
59 | | - <Switch checked={this.state.compact} label="Compact" onChange={this.handleCompactChange} /> |
| 35 | + <Switch checked={interactive} label="Interactive" onChange={handleBooleanChange(setInteractive)} /> |
| 36 | + <Switch |
| 37 | + checked={interactive && selected} |
| 38 | + disabled={!interactive} |
| 39 | + label="Selected" |
| 40 | + onChange={handleBooleanChange(setSelected)} |
| 41 | + /> |
| 42 | + <Switch checked={compact} label="Compact" onChange={handleBooleanChange(setCompact)} /> |
60 | 43 | <FormGroup label="Elevation"> |
61 | 44 | <Slider |
62 | | - max={4} |
63 | | - showTrackFill={false} |
64 | | - value={this.state.elevation} |
65 | | - onChange={this.handleElevationChange} |
66 | 45 | handleHtmlProps={{ "aria-label": "card elevation" }} |
| 46 | + max={MAX_ELEVATION} |
| 47 | + onChange={handleElevationChange} |
| 48 | + showTrackFill={false} |
| 49 | + value={elevation} |
67 | 50 | /> |
68 | 51 | </FormGroup> |
69 | 52 | </> |
70 | 53 | ); |
71 | 54 |
|
72 | | - private handleElevationChange = (elevation: Elevation) => this.setState({ elevation }); |
73 | | - |
74 | | - private handleInteractiveChange = () => this.setState({ interactive: !this.state.interactive }); |
75 | | - |
76 | | - private handleCompactChange = () => this.setState({ compact: !this.state.compact }); |
77 | | - |
78 | | - private handleSelectedChange = () => this.setState({ selected: !this.state.selected }); |
79 | | -} |
| 55 | + return ( |
| 56 | + <Example options={options} {...props}> |
| 57 | + <Card compact={compact} elevation={elevation} interactive={interactive} selected={selected}> |
| 58 | + <H5>Analytical applications</H5> |
| 59 | + <p> |
| 60 | + User interfaces that enable people to interact smoothly with data, ask better questions, and make |
| 61 | + better decisions. |
| 62 | + </p> |
| 63 | + <Button text="Explore products" className={Classes.BUTTON} /> |
| 64 | + </Card> |
| 65 | + </Example> |
| 66 | + ); |
| 67 | +}; |
0 commit comments