Skip to content
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

[docs] Add AdaptingHook TypeScript demo #16131

Merged
merged 2 commits into from
Jun 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions docs/src/pages/css-in-js/basics/AdaptingHook.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@ import React from 'react';
import PropTypes from 'prop-types';
import { makeStyles } from '@material-ui/styles';
import Button, { ButtonProps as MuiButtonProps } from '@material-ui/core/Button';

type Omit<T, K> = Pick<T, Exclude<keyof T, K>>;
import { Omit } from '@material-ui/types';

interface Props {
color: 'red' | 'blue';
}

type MyButtonProps = Props & Omit<MuiButtonProps, keyof Props>;

const useStyles = makeStyles({
root: {
background: (props: Props) =>
Expand All @@ -30,7 +27,7 @@ const useStyles = makeStyles({
},
});

function MyButton(props: MyButtonProps) {
function MyButton(props: Props & Omit<MuiButtonProps, keyof Props>) {
const { color, ...other } = props;
const classes = useStyles(props);
return <Button className={classes.root} {...other} />;
Expand Down
4 changes: 1 addition & 3 deletions docs/src/pages/styles/basics/AdaptingHook.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,11 @@ MyButton.propTypes = {
color: PropTypes.oneOf(['red', 'blue']).isRequired,
};

function AdaptingHook() {
export default function AdaptingHook() {
return (
<React.Fragment>
<MyButton color="red">Red</MyButton>
<MyButton color="blue">Blue</MyButton>
</React.Fragment>
);
}

export default AdaptingHook;
49 changes: 49 additions & 0 deletions docs/src/pages/styles/basics/AdaptingHook.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import React from 'react';
import PropTypes from 'prop-types';
import { makeStyles, createStyles } from '@material-ui/styles';
import Button, { ButtonProps as MuiButtonProps } from '@material-ui/core/Button';
import { Omit } from '@material-ui/types';

interface Props {
color: 'red' | 'blue';
}

const useStyles = makeStyles(
createStyles({
root: {
background: (props: Props) =>
props.color === 'red'
? 'linear-gradient(45deg, #FE6B8B 30%, #FF8E53 90%)'
: 'linear-gradient(45deg, #2196F3 30%, #21CBF3 90%)',
border: 0,
borderRadius: 3,
boxShadow: (props: Props) =>
props.color === 'red'
? '0 3px 5px 2px rgba(255, 105, 135, .3)'
: '0 3px 5px 2px rgba(33, 203, 243, .3)',
color: 'white',
height: 48,
padding: '0 30px',
margin: 8,
},
}),
);

function MyButton(props: Props & Omit<MuiButtonProps, keyof Props>) {
const { color, ...other } = props;
const classes = useStyles(props);
return <Button className={classes.root} {...other} />;
}

(MyButton as any).propTypes = {
color: PropTypes.oneOf(['red', 'blue']).isRequired,
};

export default function AdaptingHook() {
return (
<React.Fragment>
<MyButton color="red">Red</MyButton>
<MyButton color="blue">Blue</MyButton>
</React.Fragment>
);
}