Skip to content

Commit

Permalink
fix: fix config typing
Browse files Browse the repository at this point in the history
  • Loading branch information
crimx committed Jan 21, 2019
1 parent 4073f41 commit d164efb
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 83 deletions.
49 changes: 28 additions & 21 deletions src/app-config/dicts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export function getALlDicts () {
related: true,
sentence: 4
}
} as DictItem,
},
cambridge: {
/**
* Supported language: en, zh-CN, zh-TW, ja, kor, fr, de, es
Expand Down Expand Up @@ -93,7 +93,7 @@ export function getALlDicts () {
chs: false,
minor: false,
},
} as DictItem,
},
cobuild: {
/**
* Supported language: en, zh-CN, zh-TW, ja, kor, fr, de, es
Expand Down Expand Up @@ -131,7 +131,7 @@ export function getALlDicts () {
options: {
sentence: 4
}
} as DictItem,
},
etymonline: {
/**
* Supported language: en, zh-CN, zh-TW, ja, kor, fr, de, es
Expand Down Expand Up @@ -170,7 +170,7 @@ export function getALlDicts () {
resultnum: 4,
chart: true,
}
} as DictItem,
},
eudic: {
/**
* Supported language: en, zh-CN, zh-TW, ja, kor, fr, de, es
Expand Down Expand Up @@ -208,7 +208,7 @@ export function getALlDicts () {
options: {
resultnum: 10
}
} as DictItem,
},
google: {
/**
* Supported language: en, zh-CN, zh-TW, ja, kor, fr, de, es
Expand Down Expand Up @@ -252,7 +252,7 @@ export function getALlDicts () {
options_sel: {
tl: ['default', 'zh-CN', 'zh-TW', 'en'],
},
} as DictItem,
},
googledict: {
/**
* Supported language: en, zh-CN, zh-TW, ja, kor, fr, de, es
Expand Down Expand Up @@ -290,7 +290,7 @@ export function getALlDicts () {
options: {
enresult: true
}
} as DictItem,
},
guoyu: {
/**
* Supported language: en, zh-CN, zh-TW, ja, kor, fr, de, es
Expand Down Expand Up @@ -321,7 +321,7 @@ export function getALlDicts () {
chs: true,
minor: false,
}
} as DictItem,
},
hjdict: {
/**
* Supported language: en, zh-CN, zh-TW, ja, kor, fr, de, es
Expand Down Expand Up @@ -371,7 +371,7 @@ export function getALlDicts () {
aas: ['fr', 'de'],
eas: ['fr', 'es'],
},
} as DictItem,
},
liangan: {
/**
* Supported language: en, zh-CN, zh-TW, ja, kor, fr, de, es
Expand Down Expand Up @@ -402,7 +402,7 @@ export function getALlDicts () {
chs: true,
minor: false,
}
} as DictItem,
},
longman: {
/**
* Supported language: en, zh-CN, zh-TW, ja, kor, fr, de, es
Expand Down Expand Up @@ -446,7 +446,7 @@ export function getALlDicts () {
bussinessFirst: true,
related: true,
}
} as DictItem,
},
macmillan: {
/**
* Supported language: en, zh-CN, zh-TW, ja, kor, fr, de, es
Expand Down Expand Up @@ -484,7 +484,7 @@ export function getALlDicts () {
options: {
related: true,
}
} as DictItem,
},
oald: {
/**
* Supported language: en, zh-CN, zh-TW, ja, kor, fr, de, es
Expand Down Expand Up @@ -522,7 +522,7 @@ export function getALlDicts () {
options: {
related: true,
},
} as DictItem,
},
sogou: {
/**
* Supported language: en, zh-CN, zh-TW, ja, kor, fr, de, es
Expand Down Expand Up @@ -565,7 +565,7 @@ export function getALlDicts () {
options_sel: {
tl: ['default', 'zh-CHS', 'zh-CHT', 'en'],
},
} as DictItem,
},
urban: {
/**
* Supported language: en, zh-CN, zh-TW, ja, kor, fr, de, es
Expand Down Expand Up @@ -603,7 +603,7 @@ export function getALlDicts () {
options: {
resultnum: 4
}
} as DictItem,
},
vocabulary: {
/**
* Supported language: en, zh-CN, zh-TW, ja, kor, fr, de, es
Expand Down Expand Up @@ -634,7 +634,7 @@ export function getALlDicts () {
chs: false,
minor: false,
}
} as DictItem,
},
weblio: {
/**
* Supported language: en, zh-CN, zh-TW, ja, kor, fr, de, es
Expand Down Expand Up @@ -665,7 +665,7 @@ export function getALlDicts () {
chs: true,
minor: true,
},
} as DictItem,
},
websterlearner: {
/**
* Supported language: en, zh-CN, zh-TW, ja, kor, fr, de, es
Expand Down Expand Up @@ -707,7 +707,7 @@ export function getALlDicts () {
arts: true,
related: true,
},
} as DictItem,
},
wikipedia: {
/**
* Supported language: en, zh-CN, zh-TW, ja, kor, fr, de, es
Expand Down Expand Up @@ -748,7 +748,7 @@ export function getALlDicts () {
options_sel: {
lang: ['auto', 'zh', 'zh-cn', 'zh-tw', 'zh-hk', 'en', 'ja', 'fr', 'de'],
},
} as DictItem,
},
youdao: {
/**
* Supported language: en, zh-CN, zh-TW, ja, kor, fr, de, es
Expand Down Expand Up @@ -791,7 +791,7 @@ export function getALlDicts () {
translation: true,
related: true,
}
} as DictItem,
},
zdic: {
/**
* Supported language: en, zh-CN, zh-TW, ja, kor, fr, de, es
Expand Down Expand Up @@ -822,7 +822,14 @@ export function getALlDicts () {
chs: true,
minor: false,
}
} as DictItem,
},
}

// This is simply for type checking. The returned types are useful.
// tslint:disable-next-line: no-unused-expression
allDicts as {
[index: string]: DictItem
}

return allDicts
}
138 changes: 76 additions & 62 deletions src/options/components/options/Dictionaries/EditDictModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import React from 'react'
import { DictID } from '@/app-config'
import { Props } from '../typings'
import { updateConfigOrProfile, formItemModalLayout } from '../helpers'
import { DictItem } from '@/app-config/dicts'

import { FormComponentProps } from 'antd/lib/form'
import { Form, Modal, InputNumber, Select, Switch, Checkbox } from 'antd'
import { Form, Modal, InputNumber, Select, Switch, Checkbox, Radio } from 'antd'

const { Option } = Select

Expand All @@ -14,6 +15,79 @@ export type EditDictModalProps = Props & FormComponentProps & {
}

export class EditDictModal extends React.Component<EditDictModalProps> {
renderMoreOptions = (dictID: DictID) => {
const { t, profile } = this.props
const { getFieldDecorator } = this.props.form
const dict = profile.dicts.all[dictID] as DictItem
const { options } = dict
if (!options) { return }

const optionPath = `profile#dicts#all#${dictID}#options#`

return (
<>
<p>{t('dict_more_options')}</p>
{Object.keys(options).map(optKey => {
// can be number | boolean | string(select)
const value = options[optKey]
switch (typeof value) {
case 'number':
return (
<Form.Item
{...formItemModalLayout}
key={optKey}
label={t(`dict:${dictID}_${optKey}`)}
>{
getFieldDecorator(optionPath + optKey, {
initialValue: value,
rules: [{ type: 'number' }],
})(
<InputNumber formatter={v => `${v} ${t(`dict:${dictID}_${optKey}_unit`)}`} />
)
}</Form.Item>
)
case 'boolean':
return (
<Form.Item
{...formItemModalLayout}
key={optKey}
label={t(`dict:${dictID}_${optKey}`)}
>{
getFieldDecorator(optionPath + optKey, {
initialValue: value,
valuePropName: 'checked',
})(
<Switch />
)
}</Form.Item>
)
case 'string':
return (
<Form.Item
{...formItemModalLayout}
key={optKey}
label={t(`dict:${dictID}_${optKey}`)}
style={{ marginBottom: 0 }}
>{
getFieldDecorator(optionPath + optKey, {
initialValue: value,
})(
<Radio.Group>
{dict.options_sel![optKey].map(option => (
<Radio value={option} key={option}>{
t(`dict:${dictID}_${optKey}-${option}`)
}</Radio>
))}
</Radio.Group>
)
}</Form.Item>
)
}
})}
</>
)
}

render () {
const { onClose, dictID, t, profile } = this.props
const { getFieldDecorator } = this.props.form
Expand Down Expand Up @@ -93,67 +167,7 @@ export class EditDictModal extends React.Component<EditDictModalProps> {
<InputNumber formatter={v => `${v} px`} />
)
}</Form.Item>
{allDict[dictID].options &&
<>
<p>{t('dict_more_options')}</p>
{Object.keys(allDict[dictID].options!).map(optKey => {
// can be number | boolean | string(select)
const value = allDict[dictID].options![optKey]
switch (typeof value) {
case 'number':
return (
<Form.Item
{...formItemModalLayout}
key={optKey}
label={t(`dict:${dictID}_${optKey}`)}
>{
getFieldDecorator(`${dictPath}#options#${optKey}`, {
initialValue: value,
rules: [{ type: 'number' }],
})(
<InputNumber formatter={v => `${v} ${t(`dict:${dictID}_${optKey}_unit`)}`} />
)
}</Form.Item>
)
case 'boolean':
return (
<Form.Item
{...formItemModalLayout}
key={optKey}
label={t(`dict:${dictID}_${optKey}`)}
>{
getFieldDecorator(`${dictPath}#options#${optKey}`, {
initialValue: value,
valuePropName: 'checked',
})(
<Switch />
)
}</Form.Item>
)
case 'string':
return (
<Form.Item
{...formItemModalLayout}
key={optKey}
label={t(`dict:${dictID}_${optKey}`)}
>{
getFieldDecorator(`${dictPath}#options#${optKey}`, {
initialValue: value,
})(
<Select>
{allDict[dictID].options_sel![optKey].map(option => (
<Option value={option} key={option}>{
t(`dict:${dictID}_${optKey}-${option}`)
}</Option>
))}
</Select>
)
}</Form.Item>
)
}
})}
</>
}
{this.renderMoreOptions(dictID)}
</Form>
}
</Modal>
Expand Down

0 comments on commit d164efb

Please sign in to comment.