Skip to content

ht2 #25

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

ht2 #25

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
7 changes: 6 additions & 1 deletion src/components/article-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ class ArticleList extends Component {
}
}
ArticleList.propTypes = {
articles: PropTypes.array.isRequired
articles: PropTypes.array.isRequired,
toggleOpenItem: PropTypes.func.isRequired,
openItemId: PropTypes.string
}
ArticleList.defaultProps = {
openItemId: null
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Зачем?

}

export default accordion(ArticleList)
9 changes: 9 additions & 0 deletions src/components/article.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import React, { Component } from 'react'
import { findDOMNode } from 'react-dom'
import CommentList from './comment-list'
import PropTypes from 'prop-types'

class Article extends Component {
static propTypes = {
article: PropTypes.object.isRequired,
isOpen: PropTypes.bool,
onBtnClick: PropTypes.func.isRequired
}
static defaultProps = {
isOpen: false
}
/*
componentDidCatch(error) {
console.log('---', error)
Expand Down
69 changes: 69 additions & 0 deletions src/components/comment-form/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import './style.css'

export default class CommentForm extends Component {
static propTypes = {
username: PropTypes.string
}
static defaultProps = {
username: ''
}

initState = (username) => {
return {
username,
text: '',
isValidName: !!this.isNameValid(username),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Лучше хранить минимальное состояние, а есть ошибка, или нет - ты можешь всегда при неодходимости посчитать

isValidText: false
}
}

isNameValid = (name) => name.length < 5
isTextValid = (text) => text.length < 10 && text.length % 2 === 1

state = this.initState(this.props.username)

render() {
const { username, isValidName, isValidText, text } = this.state

console.log(this.state)
const nameBorderColor = isValidName ? 'green' : 'red'
const textBorderColor = isValidText ? 'green' : 'red'
const buttonEnabled = isValidText && isValidName
return (
<div>
<p>comment form</p>
<p>Your name validation({this.isNameValid.toString()})</p>
<input value={username} className={nameBorderColor} onChange={this.onUsernameChange} />
<p>Your comment validation({this.isTextValid.toString()}) </p>
<textarea onChange={this.onTextChange} value={text} className={textBorderColor}>
{' '}
</textarea>
<button onClick={this.onCompleteForm} disabled={!buttonEnabled}>
Send
</button>
</div>
)
}

onCompleteForm = () => {
console.log(this.state)
}
onTextChange = (ev) => {
const text = ev.target.value
this.setState({
text,
isValidText: this.isTextValid(text)
})
}

onUsernameChange = (ev) => {
const username = ev.target.value

this.setState({
username,
isValidName: this.isNameValid(username)
})
}
}
8 changes: 8 additions & 0 deletions src/components/comment-form/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.red {
border: 2px solid red;
border-radius: 4px;
}
.green {
border: 2px solid green;
border-radius: 4px;
}
11 changes: 8 additions & 3 deletions src/components/comment-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { Component } from 'react'
import PropTypes from 'prop-types'
import Comment from './comment'
import toggleOpen from '../decorators/toggle-open'
import CommentForm from './comment-form'

class CommentList extends Component {
static propTypes = {
Expand All @@ -10,11 +11,10 @@ class CommentList extends Component {
toggleOpen: PropTypes.func
}

/*
static defaultProps = {
comments: []
comments: [],
isOpen: false
}
*/

render() {
const { isOpen, toggleOpen } = this.props
Expand All @@ -23,10 +23,15 @@ class CommentList extends Component {
<div>
<button onClick={toggleOpen}>{text}</button>
{this.getBody()}
{this.commentForm()}
</div>
)
}

commentForm() {
return this.props.isOpen ? <CommentForm /> : null
}

getBody() {
const { comments, isOpen } = this.props
if (!isOpen) return null
Expand Down
2 changes: 1 addition & 1 deletion src/components/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function Comment({ comment }) {

Comment.propTypes = {
comment: PropTypes.shape({
text: PropTypes.string,
text: PropTypes.string.isRequired,
user: PropTypes.string
})
}
Expand Down
8 changes: 7 additions & 1 deletion src/components/filters/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import React, { Component } from 'react'
import DateRange from './date-range'
import SelectFilter from './select'
import PropTypes from 'prop-types'

class Filters extends Component {
static propTypes = {}
static propTypes = {
articles: PropTypes.array
}
static defaultProps = {
articles: []
}

render() {
return (
Expand Down
8 changes: 8 additions & 0 deletions src/components/filters/select.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { useState } from 'react'
import PropTypes from 'prop-types'
import Select from 'react-select'

function SelectFilter({ articles }) {
Expand All @@ -12,4 +13,11 @@ function SelectFilter({ articles }) {
return <Select options={options} value={selected} onChange={setSelection} isMulti />
}

SelectFilter.propTypes = {
article: PropTypes.shape({
title: PropTypes.string.isRequired,
id: PropTypes.string.isRequired
})
}

export default SelectFilter