Skip to content

Commit

Permalink
feat(props): Add hideLabel prop option
Browse files Browse the repository at this point in the history
Allow user of the library to not render the label element
  • Loading branch information
hibiken committed Jul 12, 2016
1 parent 29f1bf9 commit 973b77c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ Please see the example above

#### classNames
Type: `Object`,
Require: `false`
Required: `false`

You can give a custom css classes to elements.
Accepted keys are `container`, `label`, `input`, `autocompleteContainer`
Expand Down Expand Up @@ -134,6 +134,13 @@ Default: `"Address"`

You can pass `placeholder` prop to customize input's placeholder text

#### hideLabel
Type: `Boolean`
Required: `false`,
Default: `false`

You can set `hideLabel` to `true` to not render the label element


### `geocodeByAddress` API

Expand Down
17 changes: 12 additions & 5 deletions src/PlacesAutocomplete.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react'

const styles = {
const defaultStyles = {
autocompleteContainer: {
position: 'relative',
paddingBottom: '0px',
Expand Down Expand Up @@ -153,6 +153,11 @@ class PlacesAutocomplete extends React.Component {
}
}

renderLabel() {
if (this.props.hideLabel) { return null }
return (<label className={this.props.classNames.label || ''}>Location</label>)
}

// TODO: Autocomplete item should be customizable.
renderAutocomplete() {
const { autocompleteItems } = this.state
Expand All @@ -161,14 +166,14 @@ class PlacesAutocomplete extends React.Component {
<div
id="PlacesAutocomplete__autocomplete-container"
className={this.props.classNames.autocompleteContainer || ''}
style={styles.autocompleteWrapper}>
style={defaultStyles.autocompleteWrapper}>
{autocompleteItems.map((p, idx) => (
<div
key={p.placeId}
className="autocomplete__item"
onMouseOver={() => this.handleItemMouseOver(p.index)}
onClick={() => this.selectAddress(p.suggestion)}
style={{ ...this.autocompleteItemStyle(p.active), ...styles.autocompleteItem }}>
style={{ ...this.autocompleteItemStyle(p.active), ...defaultStyles.autocompleteItem }}>
<i className="fa fa-map-marker" style={{color: '#b87d4e', marginRight: '5px'}}/> {p.suggestion}
</div>
))}
Expand All @@ -180,10 +185,10 @@ class PlacesAutocomplete extends React.Component {
const { classNames, placeholder, value } = this.props
return (
<fieldset
style={styles.autocompleteContainer}
style={defaultStyles.autocompleteContainer}
className={classNames.container || ''}
>
<label className={classNames.label || ''}>Location</label>
{this.renderLabel()}
<input
type="text"
placeholder={placeholder}
Expand All @@ -202,6 +207,7 @@ PlacesAutocomplete.propTypes = {
value: React.PropTypes.string.isRequired,
setAddress: React.PropTypes.func.isRequired,
placeholder: React.PropTypes.string,
hideLabel: React.PropTypes.bool,
classNames: React.PropTypes.shape({
container: React.PropTypes.string,
label: React.PropTypes.string,
Expand All @@ -212,6 +218,7 @@ PlacesAutocomplete.propTypes = {

PlacesAutocomplete.defaultProps = {
placeholder: 'Address',
hideLabel: false,
classNames: {},
}

Expand Down
7 changes: 7 additions & 0 deletions src/tests/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ describe('custom classNames, placeholder', () => {
})
});

describe('hideLabel prop', () => {
it('lets you hide label element', () => {
const wrapper = shallow(<PlacesAutocomplete value="New York, NY" setAddress={() => {}} hideLabel={true} />)
expect(wrapper.find('label')).to.have.length(0)
})
});


// TODO: test geocodeByAddress function
describe('geocodeByAddress', () => {
Expand Down

0 comments on commit 973b77c

Please sign in to comment.