| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348 |
- import React, { Component, useState } from 'react';
- import CustomTrigger from './CustomTrigger';
- import AutoComplete from '../index';
- import { IconSearch } from '@douyinfe/semi-icons';
- export default {
- title: 'AutoComplete',
- parameters: {
- chromatic: { disableSnapshot: true },
- },
- }
- const props = {
- onBlur: (v, e) => {
- console.log('onBlur');
- console.log(v, e);
- },
- onFocus: (v, e) => {
- console.log('onFocus');
- console.log(v, e);
- },
- };
- class Demo extends React.Component {
- constructor() {
- super();
- this.state = {
- data: [],
- data2: ['mike', 'tony', 'steve'],
- };
- this.acref = React.createRef();
- }
- handleSearch(value) {
- // let data = !value ? [] : [value, value + value, value + value + value];
- let result; // if (!value || value.indexOf('@') >= 0) {
- // result = [];
- // } else {
- if (value) {
- result = ['gmail.com', '163.com', 'qq.com'].map(domain => `${value}@${domain}`);
- } else {
- result = [];
- } // }
- this.setState({
- data: result,
- });
- }
- handleSearch2(value) {
- // let data2 = !value ? [] : [value, value + value, value + value + value];
- let result;
- if (!value || value.indexOf('@') >= 0) {
- result = [];
- } else {
- result = ['gmail.com', '163.com', 'qq.com'].map(domain => `${value}@${domain}`);
- }
- this.setState({
- data2: result,
- });
- }
- render() {
- const { data, data2 } = this.state;
- return (
- <div>
- <AutoComplete
- placeholder="fe"
- className="test-ac"
- prefix={<IconSearch />}
- showClear
- data={data}
- style={{
- width: 300,
- }}
- onSearch={this.handleSearch.bind(this)}
- onSelect={v => console.log(v)}
- {...props}
- ref={this.acref}
- />
- </div>
- );
- }
- }
- export const BasicUsage = () => <Demo />;
- class CustomOptionDemo extends Component {
- constructor(props) {
- super(props);
- this.state = {
- data: [],
- data2: [],
- };
- }
- search = value => {
- let result;
- if (!value) {
- result = [];
- } else {
- result = ['gmail.com', '163.com', 'qq.com'].map(domain => `${value}@${domain}`);
- }
- this.setState({
- data: result,
- });
- };
- renderOption = item => {
- return (
- <>
- <span
- style={{
- color: 'pink',
- }}
- >
- 邮箱
- </span>
- : <span>{item}</span>
- </>
- );
- };
- search2 = value => {
- let result;
- if (!value) {
- result = [];
- } else {
- result = ['gmail.com', '163.com', 'qq.com'].map(domain => {
- return {
- email: `${value}@${domain}`,
- time: new Date().valueOf(),
- value: `${value}@${domain}`,
- };
- });
- }
- this.setState({
- data2: result,
- });
- };
- renderObjectOption = item => {
- return (
- <div>
- <span
- style={{
- color: 'pink',
- }}
- >
- 邮箱
- </span>
- : <span>{item.email}</span>
- <span>time</span>: <span>{item.time}</span>
- </div>
- );
- };
- render() {
- return (
- <>
- <AutoComplete
- showClear
- data={this.state.data}
- renderItem={this.renderOption}
- style={{
- width: '250px',
- }}
- onSearch={this.search}
- ></AutoComplete>
- <br />
- <br />
- <AutoComplete
- onChangeWithObject
- style={{
- width: '250px',
- }}
- renderItem={this.renderObjectOption}
- renderSelectedItem={node => node.email}
- data={this.state.data2}
- onSearch={this.search2}
- />
- </>
- );
- }
- }
- export const RenderItem = () => <CustomOptionDemo />;
- class WithDefaultValue extends React.Component {
- constructor() {
- super();
- this.state = {
- data: ['[email protected]', '[email protected]', '[email protected]'],
- };
- this.onSearch = this.onSearch.bind(this);
- }
- onSearch(value) {
- let result;
- if (!value) {
- result = [];
- } else {
- result = ['gmail.com', '163.com', 'qq.com'].map(domain => `${value}@${domain}`);
- }
- this.setState({
- data: result,
- });
- }
- render() {
- let { data } = this.state;
- return (
- <>
- {/* <AutoComplete
- defaultValue='[email protected]'
- data={data}
- onSearch={this.onSearch}
- /> */}
- <AutoComplete defaultValue="semi" data={data} onSearch={this.onSearch} />
- </>
- );
- }
- }
- export const DefaultValue = () => <WithDefaultValue />;
- class ControlledMode extends React.Component {
- constructor() {
- super();
- this.state = {
- data: [],
- dataObject: [],
- value: '',
- };
- this.onSearch = this.onSearch.bind(this);
- this.onChange = this.onChange.bind(this);
- }
- onSearch(value) {
- let result, resultObject;
- if (!value) {
- result = [];
- resultObject = [];
- } else {
- result = ['gmail.com', '163.com', 'qq.com'].map(domain => `${value}@${domain}`);
- resultObject = ['gmail.com', '163.com', 'qq.com'].map(domain => ({
- label: `${value}@${domain}`,
- value: `${value}@${domain}`,
- }));
- }
- this.setState({
- data: result,
- dataObject: resultObject,
- });
- }
- onChange(value) {
- this.setState({
- value: value,
- });
- }
- render() {
- let { data, value, dataObject } = this.state;
- return (
- <>
- <AutoComplete
- showClear
- value={value}
- data={data}
- onChange={this.onChange}
- onSearch={this.onSearch}
- style={{
- width: 200,
- }}
- />
- <br />
- <AutoComplete
- showClear
- value={value}
- data={dataObject}
- onChange={this.onChange}
- onSearch={this.onSearch}
- style={{
- width: 200,
- }}
- />
- <br />
- <AutoComplete
- defaultValue={'hello semi'}
- showClear
- value={value}
- data={dataObject}
- onChange={this.onChange}
- onSearch={this.onSearch}
- style={{
- width: 200,
- }}
- />
- </>
- );
- }
- }
- export const EmptyContent = () => {
- let [data, setData] = useState([]);
- const [loading, setLoading] = useState(false);
- const fetchData = v => {
- setLoading(true);
- setTimeout(() => {
- if (!v) {
- setData([]);
- setLoading(false);
- return;
- }
- setData(() => {
- const res = Array.from(Array(5)).map(c => Math.random());
- return res;
- });
- setLoading(false);
- }, 1000);
- };
- return (
- <AutoComplete loading={loading} data={data} emptyContent={'空数据'} onSearch={fetchData} />
- );
- };
- export const AutoFocus = () => {
- return <AutoComplete autoFocus />;
- };
- export const ControlledValue = () => <ControlledMode />;
- export const CustomTriggerDemo = () => <CustomTrigger />;
- export const Disabled = () => <AutoComplete disabled />;
|