Tabs 组件
6.3k
等12人参与

完成 选项卡切换组件。 提供平级的区域将大块内容进行收纳和展现,保持界面整洁。

原型图
第一种样式:点击选项卡,可以切换到对应的内容
图片描述

第二种样式:选项可以被 disabled 掉功能点要求:
图片描述
1,参考Menu 组件,合理设计组件结构,注意合理的暴露一些回调函数
比如:
图片描述
2, label 支持自定义,不一定是 string

我的作业
去发布

登录后即可发布作业,立即

全部作业
import React, {
  FC,
  createContext,
  useEffect,
  useState,
  useRef,
  FunctionComponentElement
} from 'react';
import { ISelectOptionProps } from './options';
import classNames from 'classnames';
import useClickOutside from '../../hooks/useClickOutside';
import Icon from '../Icon/Icon';
import Input from '../Input/input';
import Transition from '../Transition/transition';

export interface ISelectProps {
  // 指定默认选中的条目	 可以是是字符串或者字符串数组
  defaultValue?: string | string[];
  items?: ISelectOptionProps[];
  // 选择框默认文字
  placeholder?: string;
  // 是否禁用
  disabled?: boolean;
  // 是否多选
  multiple?: boolean;
  // select input 的 name 属性
  name?: string;
  // 选中值发生变化时触发
  onChange?: (selectedValue: string, selectedValues: string[]) => void;
  // 下拉框出现/隐藏时触发
  onVisibleChange?: (visible: boolean) => void;
  children?: React.ReactNode;
}

export interface ISelectContext {
  onSelect?: (value: string, isSelected?: boolean) => void;
  selectedValues: string[];
  multiple?: boolean;
}

export const SelectContext = createContext<ISelectContext>({
  selectedValues: []
});

const Select: FC<ISelectProps> = (props) => {
  const {
    defaultValue,
    items,
    placeholder,
    disabled,
    multiple,
    name,
    onChange,
    onVisibleChange,
    children
  } = props;

  const input = useRef<HTMLInputElement>(null);
  const containerRef = useRef<HTMLInputElement>(null);
  const containerWidth = useRef(0);
  const [selectedValues, setSelectedValues] = useState<string[]>(
    Array.isArray(defaultValue) ? defaultValue : []
  );
  const [menuOpen, setMenuOpen] = useState<boolean>(false);
  const [value, setValue] = useState(
    typeof defaultValue === 'string' ? defaultValue : ''
  );

  const handleOptionClick = (value: string, isSelected?: boolean) => {
    if (!multiple) {
      setMenuOpen(false);
      setValue(value);
      if (onVisibleChange) {
        onVisibleChange(false);
      }
    } else {
      setValue('');
    }

    let updateValues = [value];
    if (multiple) {
      updateValues = isSelected
        ? selectedValues.filter((v) => v !== value)
        : [...selectedValues, value];
        
      setSelectedValues(updateValues);
    }

    if (onChange) {
      onChange(value, updateValues);
    }
  };

  useEffect(() => {
    const inputElement = input.current;
    if (inputElement) {
      inputElement.focus();
      if (multiple && selectedValues.length > 0) {
        inputElement.placeholder = '';
      } else {
        if (placeholder) inputElement.placeholder = placeholder;
      }
    }
  }, [selectedValues, multiple, placeholder]);

  useEffect(() => {
    if (containerRef.current) {
      containerWidth.current =
        containerRef.current.getBoundingClientRect().width;
    }
  });
  useClickOutside(containerRef, () => {
    setMenuOpen(false);
    if (onVisibleChange && menuOpen) {
      onVisibleChange(false);
    }
  });

  const passedContext: ISelectContext = {
    onSelect: handleOptionClick,
    multiple: multiple,
    selectedValues: selectedValues
  };

  const handleClick = (e: React.MouseEvent) => {
    e.preventDefault();
    if (!disabled) {
      setMenuOpen(!menuOpen);
      if (onVisibleChange) {
        onVisibleChange(!menuOpen);
      }
    }
  };

  const containerClass = classNames('tui-select', {
    'menu-is-open': menuOpen,
    'is-disabled': disabled,
    'is-multiple': multiple
  });

  const generateOptions = () => {
    if (items) {
      return items.map((item, index) => {
        const isSelected = selectedValues.includes(item.value);
        const optionsClasses = classNames('tui-select-item', {
          'is-disabled': disabled,
          'is-selected': isSelected,
          [`select-${index}`]: index
        });

        return (
          <li
            className={optionsClasses}
            key={index}
            onClick={() => handleOptionClick(item.value, isSelected)}
          >
            {children || (item.label ? item.label : value)}
            {multiple && isSelected && <Icon icon={'check'} />}
          </li>
        );
      });
    } else {
      return React.Children.map(children, (child, i) => {
        const childElement =
          child as FunctionComponentElement<ISelectOptionProps>;
        if (childElement.type.displayName === 'Option') {
          return React.cloneElement(childElement, {
            index: `select-${i}`
          });
        } else {
          console.error(
            'Warning: Select has a child which is not a Option component'
          );
        }
      });
    }
  };

  return (
    <div className={containerClass} ref={containerRef}>
      <div className="tui-select-input" onClick={handleClick}>
        <Input
          value={value}
          ref={input}
          placeholder={placeholder}
          disabled={disabled}
          readOnly
          icon="arrow-down"
          name={name}
        />
      </div>

      <SelectContext.Provider value={passedContext}>
        <Transition in={menuOpen} animation="zoom-in-top" timeout={300}>
          <ul className="tui-select-dropdown">{generateOptions()}</ul>
        </Transition>
      </SelectContext.Provider>

      {multiple && (
        <div
          className="tui-selected-tags"
          style={{ maxWidth: containerWidth.current - 32 }}
        >
          {selectedValues.map((value, index) => {
            return (
              <span className="tui-tag" key={`tag-${index}`}>
                {value}
                <Icon
                  icon={'times'}
                  onClick={() => handleOptionClick(value, true)}
                />
              </span>
            );
          })}
        </div>
      )}
    </div>
  );
};

Select.defaultProps = {
  name: 'tui-select',
  placeholder: '请选择'
};

export default Select;

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
代码块
复制 预览
复制成功!
代码块
复制 预览
复制成功!
0
评论
提交于  2024-01-04 22:56:22

登录后即可查看更多作业,立即

微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号