demo.tsx 7.9 KB
Newer Older
Y
Ymm 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
import React, { useRef } from 'react'
import { SwipeInstance, Swipe } from './swipe'
import { useTranslate } from '../../sites/assets/locale'
import Cell from '@/packages/cell'
import Button from '@/packages/button'
import Toast from '@/packages/toast'
import Dialog from '@/packages/dialog'
import InputNumber from '@/packages/inputnumber'

type TSwipeDemo = {
  title1: string
  title2: string
  title3: string
  title4: string
  title5: string
16 17 18
  title6: string
  openOrClose: string
  title7: string
Y
Ymm 已提交
19 20 21 22 23 24 25 26 27
  click: string
  sure: string
  del: string
  choose: string
  event: string
  goods: string
  collect: string
  open: string
  close: string
28
  closeLeft: string
Y
Ymm 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
  tips: string
  cart: string
  leftDel: string
  disabled: string
  chooseTips: string
  deleteTips: string
}

const SwipeDemo = () => {
  const [translated] = useTranslate<TSwipeDemo>({
    'zh-CN': {
      title1: '基础用法',
      title2: '禁用滑动',
      title3: '事件监听',
      title4: '非同步控制',
      title5: '自定义內容',
45 46 47
      title6: '通过实例方法控制',
      openOrClose: '点击下方按钮打开或关闭',
      title7: '点击关闭',
Y
Ymm 已提交
48 49 50 51 52 53 54 55 56
      click: '点击',
      sure: '确定',
      del: '删除',
      choose: '选择',
      event: '事件',
      goods: '商品',
      collect: '收藏',
      open: '打开',
      close: '关闭',
57
      closeLeft: '点击右侧按钮关闭',
Y
Ymm 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70
      tips: '提示',
      cart: '加入购物车',
      leftDel: '左滑删除',
      disabled: '禁用滑动',
      chooseTips: '确定选择吗?',
      deleteTips: '确定删除吗?',
    },
    'zh-TW': {
      title1: '基礎用法',
      title2: '禁用滑動',
      title3: '事件監聽',
      title4: '非同步控制',
      title5: '自定義內容',
71 72 73
      title6: '通過實例方法控制',
      openOrClose: '點擊下方按鈕打開或關閉',
      title7: '点击关闭',
Y
Ymm 已提交
74 75 76 77 78 79 80 81 82
      click: '點擊',
      sure: '確定',
      del: '刪除',
      choose: '選擇',
      event: '事件',
      goods: '商品',
      collect: '收藏',
      open: '打開',
      close: '關閉',
83
      closeLeft: '點擊右側按鈕關閉',
Y
Ymm 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96
      tips: '提示',
      cart: '加入購物車',
      leftDel: '左滑刪除',
      disabled: '禁用滑動',
      chooseTips: '確定選擇嗎? ',
      deleteTips: '確定刪除嗎? ',
    },
    'en-US': {
      title1: 'Basic usage',
      title2: 'Disable sliding',
      title3: 'Event monitoring',
      title4: 'Asynchronous control',
      title5: 'Custom content',
97 98 99
      title6: 'Control via instance method',
      openOrClose: 'Click the button below',
      title7: 'Click to close',
Y
Ymm 已提交
100 101 102 103 104 105 106 107 108
      click: 'click',
      sure: 'ok',
      del: 'delete',
      choose: 'select',
      event: 'event',
      goods: 'goods',
      collect: 'collect',
      open: 'open',
      close: 'close',
109
      closeLeft: 'Click the right button to close',
Y
Ymm 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122
      tips: 'tips',
      cart: 'add to shopping cart',
      leftDel: 'left slide delete',
      disabled: 'Disable sliding',
      chooseTips: 'are you sure to choose?',
      deleteTips: 'are you sure to delete?',
    },
    'id-ID': {
      title1: 'penggunaan dasar',
      title2: 'Lumpuhkan sliding',
      title3: 'Monitor waktu',
      title4: 'kontrol asinkron',
      title5: 'isi suai',
123 124 125
      title6: 'kontrol melalui metode instance',
      openOrClose: 'Klik tombol di bawah untuk membuka atau menutup',
      title7: 'klik untuk menutup',
Y
Ymm 已提交
126 127 128 129 130 131 132 133 134
      click: 'klik',
      sure: 'OK',
      del: 'Hapus',
      choose: 'pilih',
      event: 'peristiwa',
      goods: 'barang',
      collect: 'kumpulkan',
      open: 'buka',
      close: 'tutup',
135
      closeLeft: 'Klik tombol kanan untuk menutup',
Y
Ymm 已提交
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
      tips: 'tip',
      cart: 'tambah ke kereta belanja',
      leftDel: 'padam slide kiri',
      disabled: 'Lumpuhkan sliding',
      chooseTips: 'Apakah kamu benar-benar memilih?',
      deleteTips: 'Apakah Anda yakin untuk menghapus?',
    },
  })

  const refDom = useRef<SwipeInstance>(null)
  const handleChange = () => {
    Toast.text(translated.click)
  }
  const beforeClose = (postion: string) => {
    Dialog.alert({
      title: translated.tips,
      content:
        postion === 'left' ? translated.chooseTips : translated.deleteTips,
      onOk: () => {
        refDom.current && refDom.current.close()
      },
    })
  }

  const handleClose = () => {
    Toast.text('close')
  }

164 165 166
  const closeRef = useRef(null)
  const openRef = useRef(null)

Y
Ymm 已提交
167 168 169 170 171 172 173 174 175 176
  return (
    <>
      <div className="demo">
        <h2>{translated.title1}</h2>
        <Swipe
          rightAction={
            <Button type="primary" shape="square">
              {translated.del}
            </Button>
          }
177 178 179
          onTouchStart={(event) => console.log('touch start', event)}
          onTouchMove={(event) => console.log('touch Move', event)}
          onTouchEnd={(event) => console.log('touch End', event)}
Y
Ymm 已提交
180 181 182
        >
          <Cell title={translated.leftDel} roundRadius={0} />
        </Swipe>
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
        <h2>{translated.title6}</h2>
        <Swipe
          ref={openRef}
          rightAction={
            <Button shape="square" type="danger">
              {translated.del}
            </Button>
          }
        >
          <Cell title={translated.openOrClose} roundRadius={0} />
        </Swipe>
        <Button onClick={() => (openRef.current as any)?.open()}>
          {translated.open}
        </Button>
        <Button onClick={() => (openRef.current as any)?.close()}>
          {translated.close}
        </Button>
        <h2>{translated.title7}</h2>
        <Swipe
          ref={closeRef}
          rightAction={
            <Button shape="square" type="danger">
              {translated.del}
            </Button>
          }
          onActionClick={() => {
            ;(closeRef.current as any)?.close()
          }}
        >
          <Cell title={translated.closeLeft} roundRadius={0} />
        </Swipe>
Y
Ymm 已提交
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
        <h2>{translated.title2}</h2>
        <Swipe
          rightAction={
            <Button shape="square" type="danger">
              {translated.del}
            </Button>
          }
          disabled
        >
          <Cell title={translated.disabled} roundRadius={0} />
        </Swipe>
        <h2>{translated.title3}</h2>
        <Swipe
          leftAction={
            <Button shape="square" type="success">
              {translated.choose}
            </Button>
          }
          rightAction={
            <>
              <Button shape="square" type="danger">
                {translated.del}
              </Button>
              <Button shape="square" type="info">
                {translated.collect}
              </Button>
            </>
          }
          onActionClick={handleChange}
          onOpen={({ name, position }) => {
            Toast.text(translated.open)
          }}
          onClose={handleClose}
        >
          <Cell title={translated.event} />
        </Swipe>
        <h2>{translated.title4}</h2>
        <Swipe
          ref={refDom}
          beforeClose={beforeClose}
          leftAction={
            <Button shape="square" type="success">
              {translated.choose}
            </Button>
          }
          rightAction={
            <>
              <Button shape="square" type="danger">
                {translated.del}
              </Button>
            </>
          }
        >
          <Cell title={translated.event} />
        </Swipe>
        <h2>{translated.title5}</h2>
        <Swipe
          rightAction={
            <>
              <Button shape="square" type="danger">
                {translated.cart}
              </Button>
            </>
          }
        >
          <Cell>
            <div
              style={{
                display: 'flex',
                justifyContent: 'space-between',
                width: '100%',
              }}
            >
              <span>{translated.goods}</span>
              <InputNumber style={{ float: 'right' }} />
            </div>
          </Cell>
        </Swipe>
      </div>
    </>
  )
}

export default SwipeDemo