Created
April 17, 2018 15:13
-
-
Save GabiGrin/5e74c631e50f541167695194ac3130a6 to your computer and use it in GitHub Desktop.
example of how to create a type safe partial change function for value comps
This file contains 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
export type Filters = { | |
channels: string[]; | |
spam: boolean; | |
assignedId: string; | |
}; | |
export type FiltersProps = ValueCompProps<Filters>; | |
export class FiltersView extends React.PureComponent<FiltersProps, {}> { | |
partialChange = <T extends keyof Filters>(prop: T) => (newVal: Filters[T]) => { | |
this.props.onChange({...this.props.value, [prop]: newVal}); | |
} | |
render () { | |
const {value} = this.props; | |
return ( | |
<div> | |
This works: | |
Span: <Checkbox value={value.spam} onChange={this.partialChange('spam')}/> | |
this doesn't: it knows channels is not boolean thus not compat with checkbox | |
{/* Span: <Checkbox value={value.spam} onChange={this.partialChange('channels')}/> */} | |
</div> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment