diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2019-04-30 13:05:06 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-04-30 13:05:06 +0900 |
commit | 457d954e08923b4accd28a919c72d0b61db1bb98 (patch) | |
tree | 9d198f7c7d37893621d4e2a27749070a435854cd /src/settings/components/form/SearchForm.jsx | |
parent | 3d9a251ce293885cda57cd3eebd111f6181132f0 (diff) | |
parent | 037ee5fd40af0117a1eb8d362b4ae97429499f16 (diff) |
Merge pull request #576 from ueokande/move-to-react
Move to React
Diffstat (limited to 'src/settings/components/form/SearchForm.jsx')
-rw-r--r-- | src/settings/components/form/SearchForm.jsx | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/src/settings/components/form/SearchForm.jsx b/src/settings/components/form/SearchForm.jsx new file mode 100644 index 0000000..6b0bd01 --- /dev/null +++ b/src/settings/components/form/SearchForm.jsx @@ -0,0 +1,92 @@ +import './SearchForm.scss'; +import React from 'react'; +import PropTypes from 'prop-types'; +import AddButton from '../ui/AddButton'; +import DeleteButton from '../ui/DeleteButton'; + +class SearchForm extends React.Component { + + render() { + let value = this.props.value; + if (!value.engines) { + value.engines = []; + } + + return <div className='form-search-form'> + <div className='form-search-form-header'> + <div className='column-name'>Name</div> + <div className='column-url'>URL</div> + <div className='column-option'>Default</div> + </div> + { + value.engines.map((engine, index) => { + return <div key={index} className='form-search-form-row'> + <input data-index={index} type='text' name='name' + className='column-name' value={engine[0]} + onChange={this.bindValue.bind(this)} + onBlur={this.props.onBlur} + /> + <input data-index={index} type='text' name='url' + placeholder='http://example.com/?q={}' + className='column-url' value={engine[1]} + onChange={this.bindValue.bind(this)} + onBlur={this.props.onBlur} + /> + <div className='column-option'> + <input data-index={index} type='radio' name='default' + checked={value.default === engine[0]} + onChange={this.bindValue.bind(this)} /> + <DeleteButton data-index={index} name='delete' + onClick={this.bindValue.bind(this)} /> + </div> + </div>; + }) + } + <AddButton name='add' style={{ float: 'right' }} + onClick={this.bindValue.bind(this)} /> + </div>; + } + + bindValue(e) { + let value = this.props.value; + let name = e.target.name; + let index = e.target.getAttribute('data-index'); + let next = { + default: value.default, + engines: value.engines ? value.engines.slice() : [], + }; + + if (name === 'name') { + next.engines[index][0] = e.target.value; + next.default = this.props.value.engines[index][0]; + } else if (name === 'url') { + next.engines[index][1] = e.target.value; + } else if (name === 'default') { + next.default = this.props.value.engines[index][0]; + } else if (name === 'add') { + next.engines.push(['', '']); + } else if (name === 'delete') { + next.engines.splice(index, 1); + } + + this.props.onChange(next); + if (name === 'delete' || name === 'default') { + this.props.onBlur(); + } + } +} + +SearchForm.propTypes = { + value: PropTypes.shape({ + default: PropTypes.string, + engines: PropTypes.arrayOf(PropTypes.arrayOf(PropTypes.string)), + }), + onChange: PropTypes.func, +}; + +SearchForm.defaultProps = { + value: { default: '', engines: []}, + onChange: () => {}, +}; + +export default SearchForm; |