-
Notifications
You must be signed in to change notification settings - Fork 35
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
junior course react #26
base: master
Are you sure you want to change the base?
Changes from 5 commits
6b9778e
ed7c32d
bb3ea64
20335f0
0395e08
c4c4d46
ebb1431
0bd0f02
3113e6c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
body { | ||
margin: 0; | ||
padding: 0; | ||
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", | ||
"Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue", | ||
sans-serif; | ||
-webkit-font-smoothing: antialiased; | ||
-moz-osx-font-smoothing: grayscale; | ||
} | ||
|
||
margin: 0; | ||
padding: 0; | ||
color: #323c48; | ||
font-family: Open Sans, -apple-system, BlinkMacSystemFont, "Segoe UI", | ||
"Roboto", "Oxygen", "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", | ||
"Helvetica Neue", sans-serif; | ||
-webkit-font-smoothing: antialiased; | ||
-moz-osx-font-smoothing: grayscale; | ||
} | ||
|
||
code { | ||
font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New", | ||
font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New", | ||
monospace; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import React from 'react'; | ||
import shallowCompare from 'react-addons-shallow-compare'; | ||
|
||
import { logger } from 'csssr-school-utils'; | ||
|
||
class BaseComponent extends React.Component { | ||
shouldComponentUpdate(nextProps, nextState) { | ||
if (shallowCompare(this, nextProps, nextState)) { | ||
logger.call(this, this.constructor.name, nextProps, nextState); | ||
|
||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
|
||
export default BaseComponent; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import React from 'react'; | ||
|
||
const FiltersContext = React.createContext({}); | ||
|
||
export default FiltersContext; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import PropTypes from 'prop-types'; | ||
import React from 'react'; | ||
|
||
import BaseComponent from '../BaseComponent/index'; | ||
import FiltersContext from '../FilterContext'; | ||
|
||
const filterTitleStyle = { | ||
marginBottom: '8px', | ||
fontSize: '20px', | ||
fontWeight: 700 | ||
} | ||
|
||
const labelStyle = { | ||
marginRight: '16px' | ||
} | ||
|
||
const labelTitleStyle = { | ||
marginRight: '8px' | ||
} | ||
|
||
const inuputStyle ={ | ||
width: '6em' | ||
} | ||
|
||
class PriceFilter extends BaseComponent { | ||
|
||
handleControlChange(filterName, value) { | ||
setTimeout(() => this.props.onChangeFilter({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Вся асинхронщина должнв быть в componentDidMount There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Это задержка пользовательского ввода |
||
filterName, | ||
value | ||
}), 500); | ||
} | ||
|
||
render() { | ||
return ( | ||
<div> | ||
<div style={ filterTitleStyle }> | ||
<span>Цена</span> | ||
</div> | ||
<FiltersContext.Consumer> | ||
{ | ||
({ minPrice, maxPrice }) => | ||
<div> | ||
<label style={ labelStyle }> | ||
<span style={ labelTitleStyle }>От:</span> | ||
<input style={ inuputStyle } | ||
type="text" | ||
defaultValue={ minPrice || 0 } | ||
onChange={(event)=> this.handleControlChange('minPrice', parseInt(event.target.value, 10))}/> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. В целом в этом случае стрелки ок. Но это часто приводит в проблемам "лишних" рендеров тк функция анонимная каждый раз. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. обратил внимание, для реакта это повод перерисовать компонент, ввиду того, что ссылка изменилась |
||
</label> | ||
|
||
<label style={ labelStyle }> | ||
<span style={ labelTitleStyle }>До:</span> | ||
<input style={ inuputStyle } | ||
type="text" | ||
defaultValue={ maxPrice || 0 } | ||
onChange={(event)=> this.handleControlChange('maxPrice', parseInt(event.target.value, 10))}/> | ||
</label> | ||
</div> | ||
} | ||
</FiltersContext.Consumer> | ||
</div> | ||
) | ||
} | ||
} | ||
|
||
PriceFilter.propTypes = { | ||
onChangeFilter: PropTypes.func | ||
} | ||
export default PriceFilter |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
import BaseComponent from '../BaseComponent/index'; | ||
import PriceFilter from '../PriceFilter/index'; | ||
|
||
const filtersContainerStyle = { | ||
padding: '4px 16px' | ||
} | ||
|
||
class UserFilters extends BaseComponent { | ||
constructor(props) { | ||
super(props); | ||
this.handleFilterChange = this.handleFilterChange.bind(this); | ||
} | ||
|
||
handleFilterChange(changes) { | ||
const { filterName, value } = changes; | ||
|
||
setTimeout(() => this.props.onChangeFilter({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Вообще не очень понятно зачем тут таймаут There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. нужна задержка по вводу пользователя There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. уберу ка я это от сюда |
||
[filterName]: value | ||
}), 500) | ||
} | ||
|
||
render() { | ||
return ( | ||
<div style={ filtersContainerStyle }> | ||
<PriceFilter onChangeFilter={ this.handleFilterChange }/> | ||
</div> | ||
) | ||
} | ||
} | ||
|
||
UserFilters.propTypes = { | ||
onChangeFilter: PropTypes.func | ||
} | ||
export default UserFilters; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Думаю уже нет смысла наследоваться от BaseComponent, эта часть была для понимания как sCU реализуется. Наследуйся от React.Component
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Мне самому инетресно пока, когда был рерндер
Давай оставим :)