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

Feature: custom options classes #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "material-input-chips",
"version": "1.3.9",
"version": "1.4.0",
"description": "A chip input field using Material-UI@next.",
"homepage": "https://rodrigonehring.github.io/material-input-chips/styleguide/",
"main": "dist/material-input-chips.js",
Expand Down
32 changes: 31 additions & 1 deletion src/MaterialChips/Index.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,46 @@ const validators = [
```
------

### Custom Chip Component
### Custom Chip Component and Options custom classes

#### Options classes:
* optionsContainer
* menuItemRoot
* menuItemSelected
* optionsContainerOpen

```js
const selected = [{ label: 'CUSTOM CHIP COMPONENT', Email: 'value' }];
const options = [
{ label: 'big field some words', Email: 'Email' },
{ label: 'big field some words 2', Email: 'Email 2' },
{ label: 'big field some words 3', Email: 'Email 3' },
{ label: 'big field some words 4', Email: 'Email 4' },
{ label: 'big field some words 5', Email: 'Email 5' },
{ label: 'big field some words 6', Email: 'Email 6' },
{ label: 'big field some words 7', Email: 'Email 7' },
];
const customOptionsClasses = function() {
return {
optionsContainer: {
border: '3px solid red !important',
},
menuItemRoot: {
color: 'green',
},
menuItemSelected: {
border: '3px solid blue !important',
}
};
}
const chipComponent = function({ chip }) { return ( <span style={{ border: '1px solid red' }}>{chip.label}</span> )};

<Handler>
<MaterialChips
selected={selected}
options={options}
chipComponent={chipComponent}
optionsClasses={customOptionsClasses}
label="Label"
/>
</Handler>
Expand Down
13 changes: 12 additions & 1 deletion src/MaterialChips/Options.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import cx from 'classnames'
import Paper from 'material-ui/Paper'
import { MenuItem } from 'material-ui/Menu'
import { withStyles } from 'material-ui/styles'
import deepmerge from 'deepmerge'

const styles = theme => ({
optionsContainer: {
Expand Down Expand Up @@ -56,6 +57,10 @@ const Options = ({ classes, open, options, onSelect, fields, focus }) => {
<MenuItem
key={option.label + option.Email}
onClick={() => onSelect(option)}
classes={{
root: classes.menuItemRoot,
selected: classes.menuItemSelected,
}}
selected={focus === index}
>
<span
Expand All @@ -77,4 +82,10 @@ Options.propTypes = {
focus: PropTypes.number,
}

export default withStyles(styles)(Options)
export default (customStyles) => {
if (customStyles) {
return withStyles(theme => deepmerge(styles(theme), customStyles(theme)))(Options)
}

return withStyles(styles)(Options)
}
12 changes: 9 additions & 3 deletions src/MaterialChips/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Fuse from 'fuse.js'
import copy from 'copy-to-clipboard'

import Chip from './Chip'
import Options from './Options'
import createOptions from './Options'
import { TYPES, acceptedCharCodes, validate, mimicFuseSearch } from './helpers'
import styles from './styles'

Expand All @@ -29,6 +29,7 @@ class MaterialChips extends Component {
/** clear input text after add an item */
clearAfterAdd: PropTypes.bool,

/** react component to render a chip */
chipComponent: PropTypes.func,

/** Disable input */
Expand Down Expand Up @@ -65,6 +66,9 @@ class MaterialChips extends Component {
/** To show in autocomplete */
options: PropTypes.arrayOf(PropTypes.object),

/** fn(theme) => ({ optionsContainer, optionsContainerOpen }) to override options styles */
optionsClasses: PropTypes.func,

/** Must receive state from props */
selected: PropTypes.arrayOf(PropTypes.object),

Expand All @@ -82,7 +86,6 @@ class MaterialChips extends Component {
openOnFocus: false,
chipsDisabled: false,
inputDisabled: false,
// inputProps: {},
options: [],
submitKeyCodes: [13, 9, 191],
clearAfterAdd: true,
Expand All @@ -103,6 +106,8 @@ class MaterialChips extends Component {
optionsFocus: null,
}

OptionsComponent = createOptions(this.props.optionsClasses)

componentDidMount() {
document.addEventListener('mousedown', this.handleClickOutside)
this.configureFuse()
Expand Down Expand Up @@ -653,12 +658,13 @@ class MaterialChips extends Component {
}
</FormControl>

<Options
<this.OptionsComponent
open={optionsOpen}
options={this.state.options}
focus={this.state.optionsFocus}
onSelect={this.addItemObject}
fields={this.props.fields}
optionsClasses={this.props.optionsClasses}
/>

</div>
Expand Down

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion styleguide/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
</head>
<body>
<div id="app"></div>
<script type="text/javascript" src="build/bundle.c104071a.js"></script></body>
<script type="text/javascript" src="build/bundle.9d6628f6.js"></script></body>
</html>