helpAndExtra.jsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* eslint-disable no-unused-vars */
  2. import React, { useState, useLayoutEffect, useEffect, useRef } from 'react';
  3. import { storiesOf } from '@storybook/react';
  4. import { Button, Modal, TreeSelect, Row, Col, Avatar, Tabs, TabPane, Badge } from '@douyinfe/semi-ui';
  5. import {
  6. Form,
  7. useFormState,
  8. useFormApi,
  9. useFieldApi,
  10. useFieldState,
  11. withFormState,
  12. withFormApi,
  13. withField,
  14. ArrayField
  15. } from '../../index';
  16. import {
  17. UseFormApiDemo,
  18. UseFormStateDemo,
  19. UseFieldApiDemo,
  20. UseFieldStateDemo,
  21. WithFormStateDemo,
  22. WithFormApiDemo,
  23. ComponentUsingFormState,
  24. CustomStringify
  25. } from '../Hook/hookDemo';
  26. const { Input, Select, DatePicker, Switch, Slider, CheckboxGroup, Checkbox, RadioGroup, Radio, TimePicker } = Form;
  27. class HelpAndExtra extends React.Component {
  28. constructor(props) {
  29. super(props);
  30. this.state = {
  31. helpText: '密码建议包含大小写字母,中英文数字,以及特殊字符',
  32. validateStatus: 'default'
  33. };
  34. this.formApi = null;
  35. this.getFormApi = this.getFormApi.bind(this);
  36. this.validate = this.validate.bind(this);
  37. this.random = this.random.bind(this);
  38. }
  39. getFormApi(formApi) {
  40. this.formApi = formApi;
  41. }
  42. validate(val, values) {
  43. if (!val) {
  44. this.setState({ validateStatus: 'error' });
  45. return <span>密码不能为空</span>;
  46. } else if (val && val.length <= 3) {
  47. this.setState({
  48. helpText: <span style={{ color: 'var(--semi-color-warning)' }}>密码强度:弱</span>,
  49. validateStatus: 'warning'
  50. }); // show helpText
  51. return ''; // validate pass
  52. } else {
  53. this.setState({
  54. helpText: '',
  55. validateStatus: 'success'
  56. });
  57. return '';
  58. }
  59. }
  60. random() {
  61. let pw = (Math.random() * 100000).toString().slice(0, 5);
  62. this.formApi.setValue('password', pw);
  63. }
  64. render() {
  65. let { helpText, validateStatus } = this.state;
  66. return (
  67. <Form
  68. getFormApi={this.getFormApi}
  69. showValidateIcon={true}
  70. onSubmit={value => console.log('submit success')}
  71. onSubmitFail={errors => console.log(errors)}
  72. >
  73. <Form.Input
  74. validate={this.validate}
  75. field="password"
  76. validateStatus={validateStatus}
  77. helpText={helpText}
  78. extraText={(
  79. <div
  80. style={{
  81. color: 'rgba(var(--semi-blue-5), 1)',
  82. fontSize: 14,
  83. userSelect: 'none',
  84. cursor: 'pointer'
  85. }}
  86. onClick={this.random}>
  87. 没有想到合适的密码?点击随机生成一个
  88. </div>
  89. )}
  90. />
  91. <Button htmlType="submit">Submit</Button>
  92. </Form>
  93. );
  94. }
  95. }
  96. const ExtraPositionDemo = () => {
  97. const helpText = <span style={{ color: 'var(--semi-color-warning)' }}>密码强度:弱</span>;
  98. return (
  99. <Form extraTextPosition="middle">
  100. <Form.Input
  101. field="a"
  102. label="extraTextPosition-middle: reactNode"
  103. helpText={helpText}
  104. // extraTextPosition='middle'
  105. extraText={(
  106. <div style={{
  107. color: 'rgba(var(--semi-blue-5), 1)',
  108. fontSize: 14,
  109. userSelect: 'none',
  110. cursor: 'pointer'
  111. }}
  112. >
  113. i am extra text
  114. </div>
  115. )}
  116. />
  117. <Form.Input
  118. field="b"
  119. helpText={helpText}
  120. label="extraTextPosition-bottom: string"
  121. extraTextPosition="bottom"
  122. extraText={'i am extra text'}
  123. />
  124. </Form>
  125. );
  126. };
  127. export { HelpAndExtra, ExtraPositionDemo };