Last active
June 17, 2019 16:11
-
-
Save dangreenisrael/5866f568cb00ce7df95dc8c057cdc95b to your computer and use it in GitHub Desktop.
Conditional props
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* This forces you to have at least one of : isReadOnly, isDisabled, or onChange | |
*/ | |
import React from "react"; | |
interface ContainsReadonly { | |
isReadOnly: boolean; | |
} | |
interface ContainsDisabled { | |
isDisabled: boolean; | |
} | |
interface ContainingOnChange { | |
/** Some really cool thing */ | |
onChange: () => void; | |
} | |
type OnChangeUnlessReadonlyOrDisabled = | |
| ContainingOnChange | |
| (ContainsReadonly | ContainsDisabled); | |
interface BaseProps { | |
isDisabled?: boolean; | |
isReadOnly?: boolean; | |
/** This is required unless isDisabled or isReadOnly is passed in */ | |
onChange?: () => void; | |
/** This is a size of things */ | |
size: string; | |
} | |
type IProps = BaseProps & OnChangeUnlessReadonlyOrDisabled; | |
const Input = (props: IProps) => { | |
return <input onChange={props.onChange} />; | |
}; | |
const First = <Input onChange={() => {}} size="small" />; | |
const Third = <Input size="small" isDisabled />; | |
const Second = <Input size="small" />; | |
export default Input; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment