Skip to content

Commit 33b5d5a

Browse files
fix(Search): merge nested shorthand props for the <input> element (#3785)
* Add failing test * fix(Search): merge nested shorthand props for <input> element * Update test/specs/modules/Search/Search-test.js * Update src/modules/Search/Search.js * Update src/modules/Search/Search.js * Update src/modules/Search/Search.js * Update src/modules/Search/Search.js * Update src/modules/Search/Search.js * Update src/modules/Search/Search.js * Update src/modules/Search/Search.js * Update src/modules/Search/Search.js * Update src/modules/Search/Search.js * Update src/modules/Search/Search.js * Update test/specs/modules/Search/Search-test.js * restore a test Co-authored-by: Oleksandr Fediashov <alexander.mcgarret@gmail.com>
1 parent 6a4ecc8 commit 33b5d5a

2 files changed

Lines changed: 36 additions & 1 deletion

File tree

src/modules/Search/Search.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@ import SearchResults from './SearchResults'
2727

2828
const debug = makeDebugger('search')
2929

30+
const overrideSearchInputProps = (predefinedProps) => {
31+
const { input } = predefinedProps
32+
33+
if (_.isUndefined(input)) {
34+
return { ...predefinedProps, input: { className: 'prompt' } }
35+
}
36+
if (_.isPlainObject(input)) {
37+
return { ...predefinedProps, input: { ...input, className: cx(input.className, 'prompt') } }
38+
}
39+
40+
return predefinedProps
41+
}
42+
3043
/**
3144
* A search module allows a user to query for results from a selection of data
3245
*/
@@ -530,12 +543,15 @@ export default class Search extends Component {
530543
autoGenerateKey: false,
531544
defaultProps: {
532545
...rest,
546+
autoComplete: 'off',
533547
icon,
534-
input: { className: 'prompt', tabIndex: '0', autoComplete: 'off' },
535548
onChange: this.handleSearchChange,
536549
onClick: this.handleInputClick,
550+
tabIndex: '0',
537551
value,
538552
},
553+
// Nested shorthand props need special treatment to survive the shallow merge
554+
overrideProps: overrideSearchInputProps,
539555
})
540556
}
541557

test/specs/modules/Search/Search-test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,25 @@ describe('Search', () => {
774774
})
775775
})
776776

777+
describe('input', () => {
778+
it(`merges nested shorthand props for the <input>`, () => {
779+
wrapperMount(<Search input={{ input: { className: 'foo', tabIndex: '-1' } }} />)
780+
const input = wrapper.find('input')
781+
782+
input.should.have.prop('tabIndex', '-1')
783+
input.should.have.className('foo')
784+
input.should.have.className('prompt')
785+
})
786+
787+
it(`will not merge for a function`, () => {
788+
wrapperMount(<Search input={{ input: (Component, props) => <Component {...props} /> }} />)
789+
const input = wrapper.find('input')
790+
791+
input.should.have.prop('autoComplete', 'off')
792+
input.should.have.not.className('prompt')
793+
})
794+
})
795+
777796
describe('input props', () => {
778797
// Search handles some of html props
779798
const props = _.without(htmlInputAttrs, 'defaultValue', 'type')

0 commit comments

Comments
 (0)