TextField文本输入框

文本输入框。

何时使用

代码演示

TextField 文本输入框 - 图1

基本使用

基本使用。

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

TextField 文本输入框 - 图2

数据源

绑定数据源。

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

TextField 文本输入框 - 图3

只读

只读。

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

TextField 文本输入框 - 图4

前后缀

前后缀。

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

TextField 文本输入框 - 图5

渲染器

渲染器。

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

TextField 文本输入框 - 图6

受控输入框

受控输入框

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

TextField 文本输入框 - 图7

Format

格式化。

  1. import { DataSet, TextField, Row, Col } from 'choerodon-ui/pro';
  2. function handleDataSetChange({ record, name, value, oldValue }) {
  3. console.log(
  4. '[dataset newValue]',
  5. value,
  6. '[oldValue]',
  7. oldValue,
  8. `[record.get('${name}')]`,
  9. record.get(name),
  10. );
  11. }
  12. class App extends React.Component {
  13. ds = new DataSet({
  14. autoCreate: true,
  15. fields: [
  16. {
  17. name: 'first-name',
  18. type: 'string',
  19. defaultValue: 'huazhen',
  20. required: true,
  21. format: 'uppercase',
  22. },
  23. {
  24. name: 'middle-name',
  25. type: 'string',
  26. defaultValue: 'HUGH',
  27. required: true,
  28. format: 'lowercase',
  29. },
  30. {
  31. name: 'last-name',
  32. type: 'string',
  33. defaultValue: 'wu',
  34. required: true,
  35. format: 'capitalize',
  36. },
  37. ],
  38. events: {
  39. update: handleDataSetChange,
  40. },
  41. });
  42. render() {
  43. return (
  44. <Row>
  45. <Col span={8}>
  46. <TextField dataSet={this.ds} name="first-name" />
  47. </Col>
  48. <Col span={8}>
  49. <TextField dataSet={this.ds} name="middle-name" />
  50. </Col>
  51. <Col span={8}>
  52. <TextField dataSet={this.ds} name="last-name" />
  53. </Col>
  54. </Row>
  55. );
  56. }
  57. }
  58. ReactDOM.render(<App />, mountNode);

TextField 文本输入框 - 图8

数据索引

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

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

TextField 文本输入框 - 图9

多值

通过属性multiple设置为多值。

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

TextField 文本输入框 - 图10

限制输入

限制输入。

  1. import { TextField } from 'choerodon-ui/pro';
  2. ReactDOM.render(
  3. <TextField placeholder="限制数字" restrict="0-9" />,
  4. mountNode
  5. );

API

TextField

参数说明类型默认值
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