TextField 文本输入框

文本输入框。

何时使用

  • 需要用户输入表单域内容时。
  • 提供组合型输入框,带搜索的输入框,限制输入配置等。

代码演示

基本使用

基本使用。

TextField文本输入框 - 图1

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { TextField, Row, Col } from 'choerodon-ui/pro';
  4. function log(value) {
  5. console.log('[basic]', value);
  6. }
  7. ReactDOM.render(
  8. <Row gutter={10}>
  9. <Col span={8}>
  10. <TextField placeholder="请输入" onChange={log} />
  11. </Col>
  12. <Col span={8}>
  13. <TextField placeholder="清除按钮" defaultValue="点击清除" clearButton onChange={log} />
  14. </Col>
  15. <Col span={8}>
  16. <TextField value="不可用" disabled />
  17. </Col>
  18. </Row>,
  19. document.getElementById('container')
  20. );

受控输入框

受控输入框

TextField文本输入框 - 图2

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { TextField } from 'choerodon-ui/pro';
  4. class App extends React.Component {
  5. constructor(props) {
  6. super(props);
  7. this.state = {
  8. value: 'default',
  9. };
  10. }
  11. handleChange = (value, oldValue) => {
  12. console.log('[newValue]', value, '[oldValue]', oldValue);
  13. this.setState({
  14. value,
  15. });
  16. }
  17. handleInput = (e) => {
  18. console.log('[input]', e.target.value);
  19. }
  20. render() {
  21. return <TextField value={this.state.value} onChange={this.handleChange} onInput={this.handleInput} />;
  22. }
  23. }

数据源

绑定数据源。

TextField文本输入框 - 图3

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { DataSet, TextField } from 'choerodon-ui/pro';
  4. function handleDataSetChange({ record, name, value, oldValue }) {
  5. console.log(
  6. '[dataset newValue]',
  7. value,
  8. '[oldValue]',
  9. oldValue,
  10. `[record.get('${name}')]`,
  11. record.get(name),
  12. );
  13. }
  14. class App extends React.Component {
  15. ds = new DataSet({
  16. autoCreate: true,
  17. fields: [
  18. {
  19. name: 'first-name',
  20. type: 'string',
  21. defaultValue: 'Zhangsan',
  22. readOnly: true,
  23. },
  24. ],
  25. events: {
  26. update: handleDataSetChange,

Format

格式化。

TextField文本输入框 - 图4

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { DataSet, TextField, Row, Col } from 'choerodon-ui/pro';
  4. function handleDataSetChange({ record, name, value, oldValue }) {
  5. console.log(
  6. '[dataset newValue]',
  7. value,
  8. '[oldValue]',
  9. oldValue,
  10. `[record.get('${name}')]`,
  11. record.get(name),
  12. );
  13. }
  14. class App extends React.Component {
  15. ds = new DataSet({
  16. autoCreate: true,
  17. fields: [
  18. {
  19. name: 'first-name',
  20. type: 'string',
  21. defaultValue: 'zhangsan',
  22. required: true,
  23. format: 'uppercase',
  24. },
  25. {
  26. name: 'middle-name',

只读

只读。

TextField文本输入框 - 图5

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { DataSet, TextField, Row, Col, Button } from 'choerodon-ui/pro';
  4. class App extends React.Component {
  5. ds = new DataSet({
  6. autoCreate: true,
  7. fields: [{ name: 'bind', readOnly: true, defaultValue: 'zhangsan' }],
  8. });
  9. handleClick = () => {
  10. this.ds.getField('bind').readOnly = false;
  11. };
  12. render() {
  13. return (
  14. <Row gutter={10}>
  15. <Col span="12">
  16. <TextField placeholder="只读" readOnly />
  17. </Col>
  18. <Col span="6">
  19. <TextField
  20. dataSet={this.ds}
  21. name="bind"
  22. placeholder="DataSet限定只读"
  23. clearButton
  24. />
  25. </Col>

数据索引

使用dataIndex进行指定数据所在数据源的索引,默认取数据源的当前索引。

TextField文本输入框 - 图6

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { DataSet, TextField, Row, Col } from 'choerodon-ui/pro';
  4. const data = [
  5. { bind: 'data1' },
  6. { bind: 'data2' },
  7. { bind: 'data3' },
  8. ];
  9. class App extends React.Component {
  10. ds = new DataSet({
  11. fields: [
  12. { name: 'bind' },
  13. ],
  14. data,
  15. });
  16. render() {
  17. return (
  18. <Row gutter={10}>
  19. <Col span="8">
  20. <TextField dataSet={this.ds} name="bind" dataIndex={0} />
  21. </Col>
  22. <Col span="8">
  23. <TextField dataSet={this.ds} name="bind" dataIndex={1} />
  24. </Col>
  25. <Col span="8">

前后缀

前后缀。

TextField文本输入框 - 图7

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { TextField, Row, Col, Icon } from 'choerodon-ui/pro';
  4. ReactDOM.render(
  5. <Row gutter={10}>
  6. <Col span={12}>
  7. <TextField placeholder="前缀" prefix={<Icon type="person" />} />
  8. </Col>
  9. <Col span={12}>
  10. <TextField placeholder="后缀" suffix={<Icon type="person" />} />
  11. </Col>
  12. </Row>,
  13. document.getElementById('container')
  14. );

多值

通过属性multiple设置为多值。

TextField文本输入框 - 图8

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { DataSet, TextField, Row, Col } from 'choerodon-ui/pro';
  4. function handleChange(value, oldValue) {
  5. console.log('[multiple]', value, '[oldValue]', oldValue);
  6. }
  7. function handleDataSetChange({ record, name, value, oldValue }) {
  8. console.log('[dataset multiple]', value, '[oldValue]', oldValue, `[record.get('${name}')]`, record.get(name));
  9. }
  10. class App extends React.Component {
  11. ds = new DataSet({
  12. autoCreate: true,
  13. fields: [
  14. { name: 'user', type: 'string', label: '用户', defaultValue: '', required: true, multiple: ',' },
  15. ],
  16. events: {
  17. update: handleDataSetChange,
  18. },
  19. });
  20. render() {
  21. return (
  22. <Row gutter={10}>
  23. <Col span={12}>
  24. <TextField dataSet={this.ds} name="user" placeholder="数据源多选" />

渲染器

渲染器。

TextField文本输入框 - 图9

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { DataSet, TextField, Icon, Row, Col } from 'choerodon-ui/pro';
  4. function valueRenderer({ value }) {
  5. return `${value}个`;
  6. }
  7. function colorRenderer({ text }) {
  8. return <span style={{ color: 'red' }}>{text}</span>;
  9. }
  10. class App extends React.Component {
  11. ds = new DataSet({
  12. data: [{ count: '30' }],
  13. fields: [{ name: 'count' }],
  14. });
  15. render() {
  16. return (
  17. <Row gutter={10}>
  18. <Col span="12">
  19. <TextField value="50" renderer={valueRenderer} />
  20. </Col>
  21. <Col span="12">
  22. <TextField
  23. dataSet={this.ds}
  24. name="count"

限制输入

限制输入。

TextField文本输入框 - 图10

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { TextField } from 'choerodon-ui/pro';
  4. ReactDOM.render(
  5. <TextField placeholder="限制数字" restrict="0-9" />,
  6. document.getElementById('container')
  7. );

API

参数说明类型默认值
placeholder占位词。当为 range 时,可以设定两个占位词string | string[]
prefix前缀,一般用于放置图标ReactNode
suffix后缀,一般用于放置图标ReactNode
clearButton是否显示清除按钮booleanfalse
minLength最小长度number
maxLength最大长度number
pattern正则校验string | RegExp
autoComplete自动完成,可选值:on | offstringoff
addonBefore设置前置标签string | ReactNode
addonAfter设置后置标签string | ReactNode
restrict限制可输入的字符string

更多属性请参考 FormField