Form

表单相关组件。

组件

Field

<Field> 组件,用于生成 <input> 元素。

Props

type
PropType: string

输入框类型,可选值为:

默认值:defaultValue / defaultChecked

<Field> 组件默认值设置遵循 React 对 <input>textarea表单组件的处理方式

  • defaultValue: 适用于 typetexttextarea 等可输入类型及 select<Field> 组件。

typeselect 时:

  • 若为多选(设置了 multiple 属性),则默认值应该传递一个数组;
  1. <Field
  2. type="select"
  3. label="Multiple Select"
  4. multiple
  5. defaultValue={['x', 'y']}
  6. >
  7. {/*...*/}
  8. </Field>
  • 若为单选,则传递字符串。
  1. <Field
  2. type="select"
  3. label="Single Select"
  4. defaultValue="x"
  5. >
  6. {/*...*/}
  7. </Field>

getValue() 的返回值类型也遵循上面的规则。

  • defaultChecked: 适用于 type 属性为 radiocheckbox<Field> 组件。
label
PropType: node

输入框标签。

btnBefore
PropType: node

组建输入框组时输入框前面的按钮。

btnAfter
PropType: node

组建输入框组时输入框后面的按钮。

labelBefore
PropType: node

组建输入框组时输入框前面的文字。

labelAfter
PropType: node

组建输入框组时输入框后面的文字。

containerClassName
PropType: string

输入框容器 className,设置 label/btnAfter/btnBefore/labelAfter/labelBefore 中任意属性后应用在包裹输入框的容器上。

如:

  1. <Field
  2. label="Your Name"
  3. containerClassName="my-label"
  4. placeholder="What's your name."
  5. />

渲染为:

  1. <label class="field-container my-label">
  2. <span class="field-label">Your Name</span>
  3. <input type="text" placeholder="What's your name." class="field">
  4. </label>

方法

.getValue()

返回输入框的值。

.getChecked()

返回输入框是否选中,当 typeradiocheckbox 时有效。

.getSelectedOptions()

返回 <select> 的值,type'select' 时有效。


Switch

<Switch> 组件,开关按钮。

Props

name
PropType: string

name 属性应用到内部的 input 元素上,可用于传统的表单提交。

value
PropType: bool

value 值应用到内部 input 元素的 defaultChecked 属性上,设置是否选中(uncontrolled)。

disabled
PropType: bool

是否禁用 Switch。

onValueChange
PropType: func

Switch 值发生变化时处理函数。

Ref

field

指向 Switch 内部的 input 元素,可用于获取 Switch 状态。

  1. function handleSwitch() {
  2. console.log(this.refs.field.checked);
  3. }
  4. const mySwitch = <Switch onValueChange={handleSwitch} />;

方法

.getValue()

获取状态,同使用 ref 一样,选中时返回 true,否则返回 false

  1. function handleSwitch() {
  2. console.log(this.getValue());
  3. }
  4. const mySwitch = <Switch onValueChange={handleSwitch} />;

示例

  1. import React from 'react';
  2. import {
  3. Container,
  4. Grid,
  5. Col,
  6. Group,
  7. Icon,
  8. List,
  9. Field,
  10. Button,
  11. Switch,
  12. } from 'amazeui-touch';
  13. let fields = [
  14. {
  15. label: 'Username',
  16. type: 'text',
  17. icon: 'person'
  18. },
  19. {
  20. label: 'Password',
  21. type: 'password',
  22. icon: 'info'
  23. },
  24. {
  25. label: 'Birth date',
  26. type: 'date',
  27. icon: 'refresh'
  28. }
  29. ];
  30. const devices = ['iPhone 6', 'MacBook Pro Retina', 'iMac 5K'];
  31. function handleSwitch() {
  32. console.log(this.getValue());
  33. }
  34. const mySwitch = <Switch onValueChange={handleSwitch} defaultChecked />;
  35. const FormExample = React.createClass({
  36. handleSubmit(e) {
  37. e.preventDefault();
  38. console.log(this.refs.select.getValue());
  39. },
  40. render() {
  41. return (
  42. <Container {...this.props}>
  43. <Group
  44. header="基本样式"
  45. >
  46. <Field
  47. label="Your Name"
  48. containerClassName="my-label"
  49. placeholder="What's your name."
  50. />
  51. <Field
  52. label="Password"
  53. placeholder="Yout password."
  54. type="password"
  55. />
  56. <Field
  57. label="Age"
  58. placeholder="Your age."
  59. type="number"
  60. />
  61. <Field
  62. type="select"
  63. label="Select"
  64. ref="select"
  65. defaultValue="m"
  66. >
  67. <option value="m">Male</option>
  68. <option value="f">Female</option>
  69. </Field>
  70. <Field
  71. label="Range"
  72. type="range"
  73. defaultValue="10"
  74. />
  75. <Field
  76. label="Commnet"
  77. placeholder="Say something you whant."
  78. type="textarea"
  79. />
  80. <Field
  81. value="提交"
  82. type="submit"
  83. amStyle="secondary"
  84. block
  85. onClick={this.handleSubmit}
  86. />
  87. </Group>
  88. {/*<h3>Range</h3>
  89. <p>
  90. <input type="range" />
  91. </p>
  92. <h3>Progress</h3>
  93. <p>
  94. <progress value="50" max="100" />
  95. <progress max="100" />
  96. <progress className="high" value="25" max="100" />
  97. <progress className="medium" value="75" max="100" />
  98. <progress className="low" value="60" max="100" />
  99. </p>
  100. <h3>Meter</h3>
  101. <p>
  102. <meter
  103. min="0"
  104. max="100"
  105. low="25"
  106. high="75"
  107. optimum="100"
  108. value="10"
  109. />
  110. <meter min="0" max="100" low="25" high="75" optimum="100"
  111. value="50" />
  112. <meter min="0" max="100" low="25" high="75" optimum="100"
  113. value="80" />
  114. </p>*/}
  115. <Group
  116. header="Form Set"
  117. >
  118. <div className="form-set">
  119. <Field placeholder="Name." />
  120. <Field placeholder="Email." />
  121. <Field placeholder="Password." />
  122. </div>
  123. <Button amStyle="primary" block>提交</Button>
  124. </Group>
  125. <Group
  126. header="Field Group"
  127. >
  128. <Field
  129. placeholder="You domain."
  130. labelBefore="www."
  131. labelAfter=".com"
  132. containerClassName="my-group"
  133. />
  134. <Field
  135. placeholder="Your email."
  136. labelBefore={<Icon name="person" />}
  137. btnAfter={<Button>Subscribe</Button>}
  138. />
  139. <Field
  140. placeholder="Keywords..."
  141. labelBefore={<Icon name="search" />}
  142. btnAfter={<Button>Search</Button>}
  143. />
  144. </Group>
  145. <h2>Form in List</h2>
  146. <Group
  147. header="Fields List"
  148. noPadded
  149. >
  150. <List>
  151. {fields.map((field, i) => {
  152. return (
  153. <List.Item
  154. key={i}
  155. nested="input"
  156. >
  157. <Field
  158. {...field}
  159. label={null}
  160. placeholder={field.label + '...'}
  161. />
  162. </List.Item>
  163. );
  164. })}
  165. </List>
  166. </Group>
  167. <Group
  168. header="With Label"
  169. noPadded
  170. >
  171. <List>
  172. {fields.map((field, i) => {
  173. return (
  174. <List.Item
  175. key={i}
  176. nested="input"
  177. >
  178. <Field
  179. {...field}
  180. placeholder={field.label + '...'}
  181. />
  182. </List.Item>
  183. );
  184. })}
  185. <List.Item
  186. title="Switch"
  187. nested="input"
  188. after={mySwitch}
  189. />
  190. </List>
  191. </Group>
  192. <Group
  193. header="List with Icon"
  194. noPadded
  195. >
  196. <List>
  197. {fields.map((field, i) => {
  198. return (
  199. <List.Item
  200. key={i}
  201. media={<Icon name={field.icon} />}
  202. nested="input"
  203. >
  204. <Field
  205. {...field}
  206. label={null}
  207. placeholder={field.label + '...'}
  208. />
  209. </List.Item>
  210. );
  211. })}
  212. </List>
  213. </Group>
  214. <Group
  215. header="List with Label & Icon"
  216. noPadded
  217. >
  218. <List>
  219. {fields.map((field, i) => {
  220. return (
  221. <List.Item
  222. key={i}
  223. media={<Icon name={field.icon} />}
  224. nested="input"
  225. >
  226. <Field
  227. {...field}
  228. placeholder={field.label + '...'}
  229. />
  230. </List.Item>
  231. );
  232. })}
  233. </List>
  234. </Group>
  235. <h3>Checkboxes & Radios</h3>
  236. <Group
  237. header="Checkboxes"
  238. footer="点击列表选择"
  239. noPadded
  240. >
  241. <List>
  242. {devices.map((device, i) => {
  243. return (
  244. <List.Item
  245. nested="checkbox"
  246. key={i}
  247. >
  248. <Field
  249. label={device}
  250. type="checkbox"
  251. name="checkbox-list-1"
  252. >
  253. </Field>
  254. </List.Item>
  255. );
  256. })}
  257. </List>
  258. </Group>
  259. <Group
  260. header="Radios"
  261. footer="点击选择一项"
  262. noPadded
  263. >
  264. <List>
  265. {devices.map((device, i) => {
  266. return (
  267. <List.Item
  268. nested="radio"
  269. key={i}
  270. >
  271. <Field
  272. label={device}
  273. type="radio"
  274. name="radio-list-1"
  275. >
  276. </Field>
  277. </List.Item>
  278. );
  279. })}
  280. </List>
  281. </Group>
  282. </Container>
  283. );
  284. }
  285. });
  286. export default FormExample;

DemoCopy

Version: 1.0.0

原文: http://t.amazeui.org/#/docs/form