demo.tsx 5.9 KB
Newer Older
L
liuyijun 已提交
1
import React, { useState } from 'react'
J
junjun666 已提交
2
import { useTranslate } from '../../sites/assets/locale'
L
liuyijun 已提交
3
import { InputNumber } from './inputnumber'
4
import ConfigProvider from '@/packages/configprovider'
L
liuyijun 已提交
5 6
import Cell from '@/packages/cell'
import Toast from '@/packages/toast'
O
oasis-cloud 已提交
7
import './demo.scss'
L
liuyijun 已提交
8

O
oasis-cloud 已提交
9
interface ValState {
L
liuyijun 已提交
10 11 12 13 14 15 16 17 18
  val1: number | string
  val2: number | string
  val3: number | string
  val4: number | string
  val5: number | string
  val6: number | string
  val7: number | string
  val8: number | string
}
J
junjun666 已提交
19 20 21 22 23 24 25 26 27

interface T {
  '6333c786': string
  '0137871a': string
  '84aa6bce': string
  '55cc5fb7': string
  '9636103a': string
  '181965e2': string
  e7b2ce1f: string
28 29
  e7b2ce1g: string
  e7b2ce1y: string
J
junjun666 已提交
30 31 32
  '3a42134b': string
  '65bafb1d': string
  '7e2394ae': string
O
oasis-cloud 已提交
33
  '7e2394be': string
J
junjun666 已提交
34
}
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

const customTheme = {
  nutuiInputnumberButtonWidth: '30px',
  nutuiInputnumberButtonHeight: '30px',
  nutuiInputnumberButtonBorderRadius: '2px',
  nutuiInputnumberButtonBackgroundColor: `#f4f4f4`,
  nutuiInputnumberInputHeight: '30px',
  nutuiInputnumberInputMargin: '0 2px',
}

const customTheme2 = {
  nutuiInputnumberButtonWidth: '30px',
  nutuiInputnumberButtonHeight: '30px',
  nutuiInputnumberButtonBackgroundColor: `#f4f4f4`,
  nutuiInputnumberInputBackgroundColor: '#fff',
  nutuiInputnumberInputMargin: '0 2px',
}

L
liuyijun 已提交
53
const InputNumberDemo = () => {
J
junjun666 已提交
54 55 56 57 58 59 60 61 62
  const [translated] = useTranslate<T>({
    'zh-CN': {
      '6333c786': '超出限制事件触发',
      '0137871a': '异步演示2秒后更改',
      '84aa6bce': '基础用法',
      '55cc5fb7': '步长设置',
      '9636103a': '限制输入范围',
      '181965e2': '禁用操作',
      e7b2ce1f: '只读禁用输入框',
63 64
      e7b2ce1g: '设置按钮样式1',
      e7b2ce1y: '设置按钮样式2',
J
junjun666 已提交
65 66 67
      '3a42134b': '支持小数点',
      '65bafb1d': '支持异步修改',
      '7e2394ae': '自定义按钮大小',
O
oasis-cloud 已提交
68
      '7e2394be': '支持formatter',
J
junjun666 已提交
69 70 71 72 73 74 75 76 77
    },
    'zh-TW': {
      '6333c786': '超出限制事件觸發',
      '0137871a': '異步演示2秒後更改',
      '84aa6bce': '基礎用法',
      '55cc5fb7': '步長設置',
      '9636103a': '限制輸入範圍',
      '181965e2': '禁用操作',
      e7b2ce1f: '只讀禁用輸入框',
78 79
      e7b2ce1g: '设置按钮样式1',
      e7b2ce1y: '设置按钮样式2',
J
junjun666 已提交
80 81 82
      '3a42134b': '支持小數點',
      '65bafb1d': '支持異步修改',
      '7e2394ae': '自定義按鈕大小',
O
oasis-cloud 已提交
83
      '7e2394be': '支持formatter',
J
junjun666 已提交
84 85 86 87 88 89 90 91 92
    },
    'en-US': {
      '6333c786': 'Exceeded limit event triggered',
      '0137871a': 'Async demo changes after 2 seconds',
      '84aa6bce': 'Basic usage',
      '55cc5fb7': 'Step size setting',
      '9636103a': 'Limit input range',
      '181965e2': 'Disable operation',
      e7b2ce1f: 'read-only disabled input box',
93 94
      e7b2ce1g: 'Button CSS1',
      e7b2ce1y: 'Button CSS2',
J
junjun666 已提交
95 96 97
      '3a42134b': 'support decimal point',
      '65bafb1d': 'Support for asynchronous modification',
      '7e2394ae': 'custom button size',
O
oasis-cloud 已提交
98
      '7e2394be': 'support formatter',
J
junjun666 已提交
99 100 101
    },
  })

O
oasis-cloud 已提交
102
  const [inputState, setInputState] = useState<ValState>({
L
liuyijun 已提交
103 104 105 106 107 108 109 110 111 112 113
    val1: 1,
    val2: 0,
    val3: 10,
    val4: 0,
    val5: 1,
    val6: 5.5,
    val7: 1,
    val8: 1,
  })
  const overlimit = (e: MouseEvent) => {
    console.log(e)
J
junjun666 已提交
114
    Toast.warn(translated['6333c786'])
L
liuyijun 已提交
115 116
  }
  const onChange = (value: string | number) => {
J
junjun666 已提交
117
    Toast.loading(translated['0137871a'])
L
liuyijun 已提交
118 119 120 121 122 123 124 125 126
    setTimeout(() => {
      inputState.val7 = Number(value)
      setInputState({ ...inputState })
      Toast.hide()
    }, 2000)
  }
  return (
    <>
      <div className="demo">
J
junjun666 已提交
127
        <h2>{translated['84aa6bce']}</h2>
L
liuyijun 已提交
128 129 130
        <Cell>
          <InputNumber modelValue={inputState.val1} />
        </Cell>
J
junjun666 已提交
131
        <h2>{translated['55cc5fb7']}</h2>
L
liuyijun 已提交
132 133 134
        <Cell>
          <InputNumber modelValue={inputState.val2} step="5" />
        </Cell>
J
junjun666 已提交
135
        <h2>{translated['9636103a']}</h2>
L
liuyijun 已提交
136
        <Cell>
O
oasis-cloud 已提交
137 138 139 140
          <InputNumber
            modelValue={inputState.val3}
            min="10"
            max="20"
141
            onOverlimit={overlimit}
O
oasis-cloud 已提交
142
          />
L
liuyijun 已提交
143
        </Cell>
J
junjun666 已提交
144
        <h2>{translated['181965e2']}</h2>
L
liuyijun 已提交
145 146 147
        <Cell>
          <InputNumber modelValue={inputState.val4} disabled />
        </Cell>
J
junjun666 已提交
148
        <h2>{translated.e7b2ce1f}</h2>
L
liuyijun 已提交
149 150 151
        <Cell>
          <InputNumber modelValue={inputState.val5} readonly />
        </Cell>
152 153 154 155 156 157 158 159 160 161 162 163
        <h2>{translated.e7b2ce1g}</h2>
        <Cell>
          <ConfigProvider theme={customTheme}>
            <InputNumber modelValue={inputState.val5} />
          </ConfigProvider>
        </Cell>
        <h2>{translated.e7b2ce1y}</h2>
        <Cell>
          <ConfigProvider theme={customTheme2}>
            <InputNumber modelValue={inputState.val5} />
          </ConfigProvider>
        </Cell>
J
junjun666 已提交
164
        <h2>{translated['3a42134b']}</h2>
L
liuyijun 已提交
165
        <Cell>
O
oasis-cloud 已提交
166 167 168 169 170 171
          <InputNumber
            modelValue={inputState.val6}
            step="0.1"
            decimalPlaces="1"
            readonly
          />
L
liuyijun 已提交
172
        </Cell>
J
junjun666 已提交
173
        <h2>{translated['65bafb1d']}</h2>
L
liuyijun 已提交
174
        <Cell>
175 176
          <InputNumber
            modelValue={inputState.val7}
H
hanyuxinting 已提交
177
            onChange={onChange}
178 179
            isAsync
          />
L
liuyijun 已提交
180
        </Cell>
J
junjun666 已提交
181
        <h2>{translated['7e2394ae']}</h2>
L
liuyijun 已提交
182
        <Cell>
O
oasis-cloud 已提交
183 184
          <InputNumber
            modelValue={inputState.val8}
O
oasis-cloud 已提交
185
            buttonSize="25px"
O
oasis-cloud 已提交
186
            inputWidth="150px"
O
oasis-cloud 已提交
187
          />
L
liuyijun 已提交
188
        </Cell>
O
oasis-cloud 已提交
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210

        <h2>支持formatter</h2>
        <Cell>
          <InputNumber
            className="format-width"
            modelValue="1000"
            min={10}
            max={15020}
            formatter={(value) =>
              `$ ${value}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',')
            }
          />
        </Cell>
        <Cell>
          <InputNumber
            className="format-width"
            modelValue="100"
            min={0}
            max={100}
            formatter={(value) => `${value}%`}
          />
        </Cell>
L
liuyijun 已提交
211 212 213 214 215 216
      </div>
    </>
  )
}

export default InputNumberDemo