diff --git a/docs/assets/screenshots/chip-1.png b/docs/assets/screenshots/chip-1.png new file mode 100644 index 0000000..ff2213d Binary files /dev/null and b/docs/assets/screenshots/chip-1.png differ diff --git a/docs/assets/screenshots/chip-2.png b/docs/assets/screenshots/chip-2.png new file mode 100644 index 0000000..e0b36c2 Binary files /dev/null and b/docs/assets/screenshots/chip-2.png differ diff --git a/example/assets/avatar.jpg b/example/assets/avatar.jpg new file mode 100644 index 0000000..26f00d4 Binary files /dev/null and b/example/assets/avatar.jpg differ diff --git a/example/src/ChipExample.js b/example/src/ChipExample.js new file mode 100644 index 0000000..1306d9e --- /dev/null +++ b/example/src/ChipExample.js @@ -0,0 +1,56 @@ +/* @flow */ + +import * as React from 'react'; +import { View, StyleSheet, Image } from 'react-native'; +import { Chip, withTheme } from 'react-native-paper'; +import type { Theme } from 'react-native-paper/types'; + +type Props = { + theme: Theme, +}; + +class ChipExample extends React.Component { + static title = 'Chip'; + + render() { + const { + theme: { + colors: { background }, + }, + } = this.props; + return ( + + + {}}>Simple Chip + {}}>Chip with delete button + Chip with icon + ( + + )} + onDelete={() => {}} + > + Chip with image + + + + ); + } +} + +const styles = StyleSheet.create({ + container: { + flex: 1, + padding: 4, + }, + + row: { + flexDirection: 'row', + flexWrap: 'wrap', + }, +}); + +export default withTheme(ChipExample); diff --git a/example/src/ExampleList.js b/example/src/ExampleList.js index b8c3884..b322d33 100644 --- a/example/src/ExampleList.js +++ b/example/src/ExampleList.js @@ -7,6 +7,7 @@ import BottomNavigationExample from './BottomNavigationExample'; import ButtonExample from './ButtonExample'; import CardExample from './CardExample'; import CheckboxExample from './CheckboxExample'; +import ChipExample from './ChipExample'; import DialogExample from './DialogExample'; import DividerExample from './DividerExample'; import FABExample from './FABExample'; @@ -35,6 +36,7 @@ export const examples = { button: ButtonExample, card: CardExample, checkbox: CheckboxExample, + chip: ChipExample, dialog: DialogExample, divider: DividerExample, fab: FABExample, diff --git a/src/components/Chip.js b/src/components/Chip.js new file mode 100644 index 0000000..f2051fe --- /dev/null +++ b/src/components/Chip.js @@ -0,0 +1,121 @@ +/* @flow */ + +import * as React from 'react'; +import { View, StyleSheet, TouchableWithoutFeedback } from 'react-native'; +import color from 'color'; +import Icon from './Icon'; +import Text from './Typography/Text'; +import { black, white } from '../styles/colors'; +import withTheme from '../core/withTheme'; +import type { Theme } from '../types'; +import type { IconSource } from './Icon'; + +type Props = { + /** + * Text content of the `Chip`. + */ + children: React.Node, + /** + * Icon to display for the `Chip`. + */ + icon?: IconSource, + /** + * Function to execute on press. + */ + onPress?: () => mixed, + /** + * Function to execute on delete. The delete button appears only when this prop is specified. + */ + onDelete?: () => mixed, + style?: any, + /** + * @optional + */ + theme: Theme, +}; + +/** + * A Chip can be used to display entities in small blocks. + * + *
+ * + * + *
+ * + * ## Usage + * ```js + * import * as React from 'react'; + * import { Chip } from 'react-native-paper'; + * + * const MyComponent = () => ( + * {}}>Example Chip + * ); + * ``` + */ +class Chip extends React.Component { + render() { + const { children, icon, onPress, onDelete, style, theme } = this.props; + const { dark, colors } = theme; + + const backgroundColor = color(dark ? white : black) + .alpha(0.12) + .rgb() + .string(); + const textColor = dark + ? colors.text + : color(colors.text) + .alpha(0.87) + .rgb() + .string(); + const iconColor = color(colors.text) + .alpha(dark ? 0.7 : 0.54) + .rgb() + .string(); + + return ( + + + {icon ? : null} + + {children} + + {onDelete ? ( + + + + + + ) : null} + + + ); + } +} + +const styles = StyleSheet.create({ + content: { + borderRadius: 16, + flexDirection: 'row', + alignItems: 'center', + justifyContent: 'space-around', + margin: 4, + }, + delete: { + padding: 6, + }, + text: { + marginVertical: 8, + }, +}); + +export default withTheme(Chip); diff --git a/src/components/Icon.js b/src/components/Icon.js index 1632d9c..b6622c9 100644 --- a/src/components/Icon.js +++ b/src/components/Icon.js @@ -37,7 +37,7 @@ export type IconSource = type IconProps = { color: string, - size?: number, + size: number, }; type Props = IconProps & { diff --git a/src/components/__tests__/Chip.test.js b/src/components/__tests__/Chip.test.js new file mode 100644 index 0000000..0a1419b --- /dev/null +++ b/src/components/__tests__/Chip.test.js @@ -0,0 +1,31 @@ +/* @flow */ + +import * as React from 'react'; +import renderer from 'react-test-renderer'; +import Chip from '../Chip'; + +it('renders chip with onPress', () => { + const tree = renderer + .create( {}}>Example Chip) + .toJSON(); + + expect(tree).toMatchSnapshot(); +}); + +it('renders chip with icon', () => { + const tree = renderer.create(Example Chip).toJSON(); + + expect(tree).toMatchSnapshot(); +}); + +it('renders deletable chip', () => { + const tree = renderer + .create( + {}}> + Example Chip + + ) + .toJSON(); + + expect(tree).toMatchSnapshot(); +}); diff --git a/src/components/__tests__/__snapshots__/Chip.test.js.snap b/src/components/__tests__/__snapshots__/Chip.test.js.snap new file mode 100644 index 0000000..48ee801 --- /dev/null +++ b/src/components/__tests__/__snapshots__/Chip.test.js.snap @@ -0,0 +1,344 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`renders chip with icon 1`] = ` + + +  + + + Example Chip + + +`; + +exports[`renders chip with onPress 1`] = ` + + + Example Chip + + +`; + +exports[`renders deletable chip 1`] = ` + + +  + + + Example Chip + + + +  + + + +`; diff --git a/src/index.js b/src/index.js index 48e98a9..3171854 100644 --- a/src/index.js +++ b/src/index.js @@ -17,6 +17,7 @@ export { default as CardActions } from './components/Card/CardActions'; export { default as CardContent } from './components/Card/CardContent'; export { default as CardCover } from './components/Card/CardCover'; export { default as Checkbox } from './components/Checkbox'; +export { default as Chip } from './components/Chip'; export { default as Dialog } from './components/Dialog/Dialog'; export { default as DialogActions } from './components/Dialog/DialogActions'; export { default as DialogContent } from './components/Dialog/DialogContent';