From cf4aef5dabeb875e5406d311174503c2d276d8db Mon Sep 17 00:00:00 2001 From: Yoshihiro OKUMURA Date: Mon, 13 May 2019 17:16:16 +0900 Subject: [PATCH] use constructor to initialize component state --- src/components/IndexTreeBlock.tsx | 16 +++++------- src/components/SearchBlock.tsx | 12 ++------- .../database/DatabaseSearchByIndexId.tsx | 8 +++--- .../database/DatabaseSearchByItemType.tsx | 11 +++----- .../database/DatabaseSearchByKeyword.tsx | 8 +----- .../item-type/util/FileDownloadButton.tsx | 23 ++++++----------- .../item-type/util/LicenseAgreementDialog.tsx | 25 +++++++------------ 7 files changed, 33 insertions(+), 70 deletions(-) diff --git a/src/components/IndexTreeBlock.tsx b/src/components/IndexTreeBlock.tsx index ab87baa..30ad35e 100644 --- a/src/components/IndexTreeBlock.tsx +++ b/src/components/IndexTreeBlock.tsx @@ -16,13 +16,6 @@ interface State { class IndexTreeBlock extends Component { - public state: State = { - tree: [], - expandableKeys: [], - expandedKeys: [], - selectedKeys: [], - }; - constructor(props: Props) { super(props); this.handleClickOpenAll = this.handleClickOpenAll.bind(this); @@ -30,9 +23,12 @@ class IndexTreeBlock extends Component { this.handleExpand = this.handleExpand.bind(this); this.handleSelect = this.handleSelect.bind(this); const res = this.load(); - this.state.tree = res.elements; - this.state.expandableKeys = res.keys; - this.state.expandedKeys = res.keys; + this.state = { + tree: res.elements, + expandableKeys: res.keys, + expandedKeys: res.keys, + selectedKeys: [], + }; } load() { diff --git a/src/components/SearchBlock.tsx b/src/components/SearchBlock.tsx index aaba850..fe962a5 100644 --- a/src/components/SearchBlock.tsx +++ b/src/components/SearchBlock.tsx @@ -12,16 +12,10 @@ interface State { class SearchBlock extends Component { - public state: State = { - type: 'all', - keyword: '' - } - constructor(props: Props) { super(props); const { type, keyword } = ItemUtil.getSearchKeywordByQuery(this.props.location.search); - this.state.type = type; - this.state.keyword = keyword; + this.state = { type, keyword }; this.handleChangeType = this.handleChangeType.bind(this); this.handleChangeKeyword = this.handleChangeKeyword.bind(this); this.handleSubmit = this.handleSubmit.bind(this); @@ -45,9 +39,7 @@ class SearchBlock extends Component { handleSubmit(event: FormEvent) { event.preventDefault(); const url = ItemUtil.getSearchByKeywordUrl(this.state.type, this.state.keyword); - if (this.state.keyword.length > 0) { - this.props.history.push(url); - } + this.props.history.push(url); } render() { diff --git a/src/components/database/DatabaseSearchByIndexId.tsx b/src/components/database/DatabaseSearchByIndexId.tsx index 616abdc..06cfabd 100644 --- a/src/components/database/DatabaseSearchByIndexId.tsx +++ b/src/components/database/DatabaseSearchByIndexId.tsx @@ -14,15 +14,13 @@ interface State { class DatabaseSearchByIndexId extends Component { - public state: State = { - index: IndexUtil.get(INDEX_ID_PUBLIC), - }; - constructor(props: Props) { super(props); const { params } = this.props.match; const indexId = params.id ? parseInt(params.id, 10) : INDEX_ID_PUBLIC; - this.state.index = IndexUtil.get(indexId); + this.state = { + index: IndexUtil.get(indexId), + } this.search = this.search.bind(this); } diff --git a/src/components/database/DatabaseSearchByItemType.tsx b/src/components/database/DatabaseSearchByItemType.tsx index ceba201..61216dc 100644 --- a/src/components/database/DatabaseSearchByItemType.tsx +++ b/src/components/database/DatabaseSearchByItemType.tsx @@ -12,16 +12,13 @@ interface State { class DatabaseSearchByItemType extends Component { - public state: State = { - item_type: '', - sub_item_type: '', - }; - constructor(props: Props) { super(props); const { params } = this.props.match; - this.state.item_type = params.itemType ? params.itemType : ''; - this.state.sub_item_type = params.subItemType ? params.subItemType : ''; + this.state = { + item_type: params.itemType ? params.itemType : '', + sub_item_type: params.subItemType ? params.subItemType : '', + }; this.search = this.search.bind(this); } diff --git a/src/components/database/DatabaseSearchByKeyword.tsx b/src/components/database/DatabaseSearchByKeyword.tsx index 34ed000..fbe394e 100644 --- a/src/components/database/DatabaseSearchByKeyword.tsx +++ b/src/components/database/DatabaseSearchByKeyword.tsx @@ -12,16 +12,10 @@ interface State { class DatabaseSearchByKeyword extends Component { - public state: State = { - type: 'all', - keyword: '', - }; - constructor(props: Props) { super(props); const { type, keyword } = ItemUtil.getSearchKeywordByQuery(this.props.location.search); - this.state.type = type; - this.state.keyword = keyword; + this.state = {type, keyword}; this.search = this.search.bind(this); } diff --git a/src/components/database/item-type/util/FileDownloadButton.tsx b/src/components/database/item-type/util/FileDownloadButton.tsx index 6f66fc4..34b7f81 100644 --- a/src/components/database/item-type/util/FileDownloadButton.tsx +++ b/src/components/database/item-type/util/FileDownloadButton.tsx @@ -22,23 +22,16 @@ interface State { class FileDownloadButton extends Component { - public state: State = { - file: null, - rights: '', - useCc: 0, - ccCommercialUse: 0, - ccModification: 0, - show: false, - }; - constructor(props: Props) { super(props); - this.state.file = this.props.file; - this.state.rights = this.props.rights; - this.state.useCc = this.props.useCc; - this.state.ccCommercialUse = this.props.ccCommercialUse; - this.state.ccModification = this.props.ccModification; - this.state.show = false; + this.state = { + file: this.props.file, + rights: this.props.rights, + useCc: this.props.useCc, + ccCommercialUse: this.props.ccCommercialUse, + ccModification: this.props.ccModification, + show: false, + }; this.handleClickDownload = this.handleClickDownload.bind(this); } diff --git a/src/components/database/item-type/util/LicenseAgreementDialog.tsx b/src/components/database/item-type/util/LicenseAgreementDialog.tsx index bf3fcbe..468989a 100644 --- a/src/components/database/item-type/util/LicenseAgreementDialog.tsx +++ b/src/components/database/item-type/util/LicenseAgreementDialog.tsx @@ -28,24 +28,17 @@ interface State { class LicenseAgreementDialog extends Component { - public state: State = { - file: null, - rights: '', - useCc: 0, - ccCommercialUse: 0, - ccModification: 0, - show: false, - disabled: false, - }; - constructor(props: Props) { super(props); - this.state.file = this.props.file; - this.state.rights = this.props.rights; - this.state.useCc = this.props.useCc; - this.state.ccCommercialUse = this.props.ccCommercialUse; - this.state.ccModification = this.props.ccModification; - this.state.show = this.props.show; + this.state = { + file: this.props.file, + rights: this.props.rights, + useCc: this.props.useCc, + ccCommercialUse: this.props.ccCommercialUse, + ccModification: this.props.ccModification, + show: this.props.show, + disabled: false, + }; this.handleChangeCheckbox = this.handleChangeCheckbox.bind(this); this.handleClickDownload = this.handleClickDownload.bind(this); this.handleClickCancel = this.handleClickCancel.bind(this);