@daisu-ui/ui 0.0.0-stage → 0.1.0-beta.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +67 -0
- package/LICENSE +21 -0
- package/README.md +179 -2
- package/THIRD_PARTY_NOTICES.md +19 -0
- package/dist/ActionSheet.d.ts +45 -0
- package/dist/ActionSheet.jsx +37 -0
- package/dist/AnchoredBubble.d.ts +36 -0
- package/dist/AnchoredBubble.jsx +120 -0
- package/dist/Avatar.d.ts +35 -0
- package/dist/Avatar.jsx +29 -0
- package/dist/Backtop.d.ts +22 -0
- package/dist/Backtop.jsx +18 -0
- package/dist/Badge.d.ts +19 -0
- package/dist/Badge.jsx +11 -0
- package/dist/BarCode.d.ts +20 -0
- package/dist/BarCode.jsx +38 -0
- package/dist/Button.d.ts +21 -0
- package/dist/Button.jsx +19 -0
- package/dist/Calendar.d.ts +90 -0
- package/dist/Calendar.jsx +164 -0
- package/dist/CalendarView.d.ts +51 -0
- package/dist/CalendarView.jsx +136 -0
- package/dist/Card.d.ts +21 -0
- package/dist/Card.jsx +21 -0
- package/dist/Cascader.d.ts +75 -0
- package/dist/Cascader.jsx +212 -0
- package/dist/Cell.d.ts +34 -0
- package/dist/Cell.jsx +37 -0
- package/dist/Checkbox.d.ts +50 -0
- package/dist/Checkbox.jsx +74 -0
- package/dist/Circle.d.ts +19 -0
- package/dist/Circle.jsx +35 -0
- package/dist/Collapse.d.ts +31 -0
- package/dist/Collapse.jsx +61 -0
- package/dist/ConfigProvider.d.ts +15 -0
- package/dist/ConfigProvider.jsx +20 -0
- package/dist/CountDown.d.ts +20 -0
- package/dist/CountDown.jsx +15 -0
- package/dist/CountTo.d.ts +28 -0
- package/dist/CountTo.jsx +27 -0
- package/dist/Curtain.d.ts +29 -0
- package/dist/Curtain.jsx +51 -0
- package/dist/DatetimePicker.d.ts +46 -0
- package/dist/DatetimePicker.jsx +138 -0
- package/dist/DatetimePickerView.d.ts +38 -0
- package/dist/DatetimePickerView.jsx +36 -0
- package/dist/Dialog.d.ts +64 -0
- package/dist/Dialog.jsx +105 -0
- package/dist/Divider.d.ts +16 -0
- package/dist/Divider.jsx +9 -0
- package/dist/DropMenu.d.ts +55 -0
- package/dist/DropMenu.jsx +210 -0
- package/dist/Empty.d.ts +14 -0
- package/dist/Empty.jsx +14 -0
- package/dist/EmptyState.d.ts +6 -0
- package/dist/EmptyState.jsx +9 -0
- package/dist/Fab.d.ts +30 -0
- package/dist/Fab.jsx +127 -0
- package/dist/FloatingPanel.d.ts +21 -0
- package/dist/FloatingPanel.jsx +102 -0
- package/dist/Form.d.ts +63 -0
- package/dist/Form.jsx +108 -0
- package/dist/FormFieldRow.d.ts +56 -0
- package/dist/FormFieldRow.jsx +63 -0
- package/dist/Gap.d.ts +10 -0
- package/dist/Gap.jsx +14 -0
- package/dist/Grid.d.ts +47 -0
- package/dist/Grid.jsx +31 -0
- package/dist/Icon.d.ts +11 -0
- package/dist/Icon.jsx +12 -0
- package/dist/ImagePreview.d.ts +62 -0
- package/dist/ImagePreview.jsx +78 -0
- package/dist/Img.d.ts +28 -0
- package/dist/Img.jsx +59 -0
- package/dist/ImgCropper.d.ts +74 -0
- package/dist/ImgCropper.jsx +114 -0
- package/dist/IndexBar.d.ts +36 -0
- package/dist/IndexBar.jsx +149 -0
- package/dist/Input.d.ts +11 -0
- package/dist/Input.jsx +3 -0
- package/dist/InputNumber.d.ts +32 -0
- package/dist/InputNumber.jsx +165 -0
- package/dist/Keyboard.d.ts +37 -0
- package/dist/Keyboard.jsx +130 -0
- package/dist/Layout.d.ts +31 -0
- package/dist/Layout.jsx +14 -0
- package/dist/Loading.d.ts +18 -0
- package/dist/Loading.jsx +15 -0
- package/dist/Loadmore.d.ts +18 -0
- package/dist/Loadmore.jsx +16 -0
- package/dist/Navbar.d.ts +35 -0
- package/dist/Navbar.jsx +50 -0
- package/dist/NoticeBar.d.ts +30 -0
- package/dist/NoticeBar.jsx +86 -0
- package/dist/Notify.d.ts +34 -0
- package/dist/Notify.jsx +52 -0
- package/dist/NumberStepper.d.ts +11 -0
- package/dist/NumberStepper.jsx +41 -0
- package/dist/Overlay.d.ts +16 -0
- package/dist/Overlay.jsx +12 -0
- package/dist/PagedList.d.ts +13 -0
- package/dist/PagedList.jsx +36 -0
- package/dist/Pagination.d.ts +29 -0
- package/dist/Pagination.jsx +33 -0
- package/dist/PasswordInput.d.ts +16 -0
- package/dist/PasswordInput.jsx +20 -0
- package/dist/Picker.d.ts +61 -0
- package/dist/Picker.jsx +80 -0
- package/dist/PickerView.d.ts +43 -0
- package/dist/PickerView.jsx +142 -0
- package/dist/Popover.d.ts +23 -0
- package/dist/Popover.jsx +35 -0
- package/dist/Popup.d.ts +30 -0
- package/dist/Popup.jsx +32 -0
- package/dist/Progress.d.ts +17 -0
- package/dist/Progress.jsx +45 -0
- package/dist/QrCode.d.ts +46 -0
- package/dist/QrCode.jsx +67 -0
- package/dist/Radio.d.ts +21 -0
- package/dist/Radio.jsx +34 -0
- package/dist/Rate.d.ts +25 -0
- package/dist/Rate.jsx +39 -0
- package/dist/Resize.d.ts +17 -0
- package/dist/Resize.jsx +17 -0
- package/dist/RichText.d.ts +133 -0
- package/dist/RichText.jsx +288 -0
- package/dist/RootPortal.d.ts +11 -0
- package/dist/RootPortal.jsx +19 -0
- package/dist/Search.d.ts +39 -0
- package/dist/Search.jsx +94 -0
- package/dist/Segmented.d.ts +18 -0
- package/dist/Segmented.jsx +35 -0
- package/dist/SelectPicker.d.ts +54 -0
- package/dist/SelectPicker.jsx +136 -0
- package/dist/Sidebar.d.ts +34 -0
- package/dist/Sidebar.jsx +43 -0
- package/dist/Signature.d.ts +46 -0
- package/dist/Signature.jsx +203 -0
- package/dist/Skeleton.d.ts +27 -0
- package/dist/Skeleton.jsx +29 -0
- package/dist/SlideVerify.d.ts +27 -0
- package/dist/SlideVerify.jsx +138 -0
- package/dist/Slider.d.ts +21 -0
- package/dist/Slider.jsx +131 -0
- package/dist/SortButton.d.ts +16 -0
- package/dist/SortButton.jsx +19 -0
- package/dist/Steps.d.ts +23 -0
- package/dist/Steps.jsx +25 -0
- package/dist/Sticky.d.ts +17 -0
- package/dist/Sticky.jsx +69 -0
- package/dist/SwipeAction.d.ts +27 -0
- package/dist/SwipeAction.jsx +187 -0
- package/dist/Swiper.d.ts +54 -0
- package/dist/Swiper.jsx +94 -0
- package/dist/SwiperNav.d.ts +18 -0
- package/dist/SwiperNav.jsx +10 -0
- package/dist/Switch.d.ts +31 -0
- package/dist/Switch.jsx +46 -0
- package/dist/SyncedInput.d.ts +12 -0
- package/dist/SyncedInput.jsx +18 -0
- package/dist/SyncedTextarea.d.ts +11 -0
- package/dist/SyncedTextarea.jsx +18 -0
- package/dist/Tabbar.d.ts +37 -0
- package/dist/Tabbar.jsx +46 -0
- package/dist/Table.d.ts +51 -0
- package/dist/Table.jsx +125 -0
- package/dist/Tabs.d.ts +65 -0
- package/dist/Tabs.jsx +132 -0
- package/dist/Tag.d.ts +33 -0
- package/dist/Tag.jsx +63 -0
- package/dist/Text.d.ts +19 -0
- package/dist/Text.jsx +7 -0
- package/dist/TextField.d.ts +89 -0
- package/dist/TextField.jsx +149 -0
- package/dist/Textarea.d.ts +22 -0
- package/dist/Textarea.jsx +3 -0
- package/dist/Toast.d.ts +36 -0
- package/dist/Toast.jsx +30 -0
- package/dist/Tooltip.d.ts +8 -0
- package/dist/Tooltip.jsx +5 -0
- package/dist/Tour.d.ts +85 -0
- package/dist/Tour.jsx +150 -0
- package/dist/Transition.d.ts +32 -0
- package/dist/Transition.jsx +43 -0
- package/dist/Upload.d.ts +64 -0
- package/dist/Upload.jsx +163 -0
- package/dist/VideoPreview.d.ts +47 -0
- package/dist/VideoPreview.jsx +59 -0
- package/dist/Watermark.d.ts +36 -0
- package/dist/Watermark.jsx +18 -0
- package/dist/action-sheet.css +19 -0
- package/dist/avatar.css +23 -0
- package/dist/avatarModel.d.ts +8 -0
- package/dist/avatarModel.js +17 -0
- package/dist/backtop.css +6 -0
- package/dist/badge.css +15 -0
- package/dist/badgeModel.d.ts +2 -0
- package/dist/badgeModel.js +18 -0
- package/dist/bar-code.css +5 -0
- package/dist/barCodeModel.d.ts +80 -0
- package/dist/barCodeModel.js +99 -0
- package/dist/brand/logo.svg +8 -0
- package/dist/bubble.css +14 -0
- package/dist/bubblePosition.d.ts +19 -0
- package/dist/bubblePosition.js +41 -0
- package/dist/button.css +56 -0
- package/dist/calendar.css +56 -0
- package/dist/calendarModel.d.ts +62 -0
- package/dist/calendarModel.js +141 -0
- package/dist/card.css +8 -0
- package/dist/cascader.css +16 -0
- package/dist/cascaderModel.d.ts +21 -0
- package/dist/cascaderModel.js +32 -0
- package/dist/cell.css +41 -0
- package/dist/choice.css +27 -0
- package/dist/choice.d.ts +28 -0
- package/dist/choice.jsx +16 -0
- package/dist/circle.css +4 -0
- package/dist/circleModel.d.ts +15 -0
- package/dist/circleModel.js +31 -0
- package/dist/config-provider.css +2 -0
- package/dist/configContext.d.ts +23 -0
- package/dist/configContext.js +24 -0
- package/dist/configTheme.d.ts +3 -0
- package/dist/configTheme.js +22 -0
- package/dist/count-down.css +2 -0
- package/dist/count-to.css +8 -0
- package/dist/countDownModel.d.ts +11 -0
- package/dist/countDownModel.js +7 -0
- package/dist/countToModel.d.ts +3 -0
- package/dist/countToModel.js +3 -0
- package/dist/curtain.css +10 -0
- package/dist/datetime-picker.css +11 -0
- package/dist/datetimePickerModel.d.ts +47 -0
- package/dist/datetimePickerModel.js +128 -0
- package/dist/dialog.css +23 -0
- package/dist/divider.css +14 -0
- package/dist/drop-menu.css +28 -0
- package/dist/empty.css +7 -0
- package/dist/fab.css +23 -0
- package/dist/fabModel.d.ts +26 -0
- package/dist/fabModel.js +16 -0
- package/dist/floating-panel.css +6 -0
- package/dist/floatingPanelModel.d.ts +3 -0
- package/dist/floatingPanelModel.js +10 -0
- package/dist/form.css +10 -0
- package/dist/formContext.d.ts +7 -0
- package/dist/formContext.js +2 -0
- package/dist/formModel.d.ts +38 -0
- package/dist/formModel.js +16 -0
- package/dist/gap.css +1 -0
- package/dist/grid.css +17 -0
- package/dist/icon.css +8 -0
- package/dist/icons/LICENSE.tabler +21 -0
- package/dist/icons/paths.d.ts +309 -0
- package/dist/icons/paths.js +310 -0
- package/dist/icons/render.d.ts +2 -0
- package/dist/icons/render.js +5 -0
- package/dist/image-preview.css +8 -0
- package/dist/img-cropper.css +1 -0
- package/dist/img.css +5 -0
- package/dist/imgCropperModel.d.ts +55 -0
- package/dist/imgCropperModel.js +41 -0
- package/dist/imgModel.d.ts +13 -0
- package/dist/imgModel.js +22 -0
- package/dist/index-bar.css +12 -0
- package/dist/index.d.ts +200 -0
- package/dist/index.js +99 -0
- package/dist/indexBarModel.d.ts +19 -0
- package/dist/indexBarModel.js +37 -0
- package/dist/input-number.css +24 -0
- package/dist/inputNumberModel.d.ts +23 -0
- package/dist/inputNumberModel.js +40 -0
- package/dist/keyboard.css +26 -0
- package/dist/keyboardModel.d.ts +11 -0
- package/dist/keyboardModel.js +34 -0
- package/dist/layout.css +2 -0
- package/dist/legacy/RadioGroup.d.ts +13 -0
- package/dist/legacy/RadioGroup.jsx +13 -0
- package/dist/legacy-controls.css +87 -0
- package/dist/loading.css +31 -0
- package/dist/loadingModel.d.ts +2 -0
- package/dist/loadingModel.js +21 -0
- package/dist/loadmore.css +5 -0
- package/dist/navbar.css +16 -0
- package/dist/notice-bar.css +19 -0
- package/dist/notify.css +14 -0
- package/dist/overlay.css +2 -0
- package/dist/pagination.css +12 -0
- package/dist/paginationModel.d.ts +6 -0
- package/dist/paginationModel.js +7 -0
- package/dist/password-input.css +14 -0
- package/dist/passwordInputModel.d.ts +6 -0
- package/dist/passwordInputModel.js +11 -0
- package/dist/picker.css +42 -0
- package/dist/pickerModel.d.ts +32 -0
- package/dist/pickerModel.js +49 -0
- package/dist/popover.css +5 -0
- package/dist/popup.css +17 -0
- package/dist/progress.css +16 -0
- package/dist/progressModel.d.ts +12 -0
- package/dist/progressModel.js +19 -0
- package/dist/qr-code.css +4 -0
- package/dist/qrCodeModel.d.ts +36 -0
- package/dist/qrCodeModel.js +61 -0
- package/dist/rate.css +20 -0
- package/dist/rateModel.d.ts +4 -0
- package/dist/rateModel.js +38 -0
- package/dist/resize.css +2 -0
- package/dist/rich-text.css +19 -0
- package/dist/richTextMedia.d.ts +28 -0
- package/dist/richTextMedia.js +3 -0
- package/dist/richTextModel.d.ts +52 -0
- package/dist/richTextModel.js +195 -0
- package/dist/root-portal.css +1 -0
- package/dist/router/RouterView.d.ts +21 -0
- package/dist/router/RouterView.jsx +120 -0
- package/dist/router/core.d.ts +85 -0
- package/dist/router/core.js +255 -0
- package/dist/router/hooks.d.ts +17 -0
- package/dist/router/hooks.jsx +71 -0
- package/dist/router/index.d.ts +5 -0
- package/dist/router/index.js +3 -0
- package/dist/router/location.d.ts +8 -0
- package/dist/router/location.js +46 -0
- package/dist/router/router.css +7 -0
- package/dist/router/runtime.d.ts +1 -0
- package/dist/router/runtime.js +25 -0
- package/dist/search.css +23 -0
- package/dist/segmented.css +19 -0
- package/dist/segmentedModel.d.ts +10 -0
- package/dist/segmentedModel.js +8 -0
- package/dist/sidebar.css +18 -0
- package/dist/sidebarModel.d.ts +8 -0
- package/dist/sidebarModel.js +27 -0
- package/dist/signature.css +5 -0
- package/dist/signatureModel.d.ts +23 -0
- package/dist/signatureModel.js +80 -0
- package/dist/skeleton.css +16 -0
- package/dist/slide-verify.css +7 -0
- package/dist/slider.css +49 -0
- package/dist/sliderModel.d.ts +10 -0
- package/dist/sliderModel.js +27 -0
- package/dist/sort-button.css +12 -0
- package/dist/steps.css +35 -0
- package/dist/sticky.css +7 -0
- package/dist/styles.css +136 -0
- package/dist/styles.d.ts +1 -0
- package/dist/swipe-action.css +6 -0
- package/dist/swiper.css +27 -0
- package/dist/swiperAnimation.d.ts +3 -0
- package/dist/swiperAnimation.js +6 -0
- package/dist/swiperModel.d.ts +2 -0
- package/dist/swiperModel.js +3 -0
- package/dist/switch.css +12 -0
- package/dist/tabbar.css +17 -0
- package/dist/tabbarModel.d.ts +8 -0
- package/dist/tabbarModel.js +27 -0
- package/dist/table.css +21 -0
- package/dist/tableModel.d.ts +10 -0
- package/dist/tableModel.js +17 -0
- package/dist/tabs.css +31 -0
- package/dist/tabsModel.d.ts +7 -0
- package/dist/tabsModel.js +14 -0
- package/dist/tag.css +66 -0
- package/dist/text-field.css +15 -0
- package/dist/text.css +7 -0
- package/dist/textModel.d.ts +3 -0
- package/dist/textModel.js +24 -0
- package/dist/toast.css +13 -0
- package/dist/tokens.d.ts +34 -0
- package/dist/tokens.js +10 -0
- package/dist/tour.css +8 -0
- package/dist/transition.css +12 -0
- package/dist/upload.css +11 -0
- package/dist/useCountDown.d.ts +14 -0
- package/dist/useCountDown.js +73 -0
- package/dist/useDialog.d.ts +9 -0
- package/dist/useDialog.js +33 -0
- package/dist/useImagePreview.d.ts +7 -0
- package/dist/useImagePreview.js +17 -0
- package/dist/useNotify.d.ts +9 -0
- package/dist/useNotify.js +26 -0
- package/dist/usePressState.d.ts +6 -0
- package/dist/usePressState.js +17 -0
- package/dist/useToast.d.ts +11 -0
- package/dist/useToast.js +23 -0
- package/dist/useVideoPreview.d.ts +7 -0
- package/dist/useVideoPreview.js +17 -0
- package/dist/utils.d.ts +4 -0
- package/dist/utils.js +4 -0
- package/dist/video-preview.css +7 -0
- package/dist/watermark.css +6 -0
- package/dist/web/imgCropperElement.d.ts +2 -0
- package/dist/web/imgCropperElement.js +309 -0
- package/dist/web/index.d.ts +6 -0
- package/dist/web/index.js +6 -0
- package/dist/web/qrCodeExportElement.d.ts +4 -0
- package/dist/web/qrCodeExportElement.js +70 -0
- package/dist/web/richTextMediaElement.d.ts +2 -0
- package/dist/web/richTextMediaElement.js +276 -0
- package/package.json +98 -4
package/dist/Img.jsx
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { useEffect, useRef, useState } from '@lynx-js/react';
|
|
2
|
+
import { Icon } from "./Icon.jsx";
|
|
3
|
+
import { imgDimension, imgFrame } from './imgModel.js';
|
|
4
|
+
// A source change remounts image state, including when returning to a previously failed URL.
|
|
5
|
+
export function Img(props) { return <ImgSource key={props.src || ''} {...props}/>; }
|
|
6
|
+
function ImgSource({ src = '', previewSrc, width, height, mode = 'scaleToFill', round = false, radius, showLoading = true, showError = true, loading, error, enablePreview = false, onPreview, onPreviewError, onClick, onLoad, onError, alt = '', customImage = '', className = '', style }) {
|
|
7
|
+
const [status, setStatus] = useState('loading');
|
|
8
|
+
const [natural, setNatural] = useState(null), [box, setBox] = useState({ width: 0, height: 0 });
|
|
9
|
+
const active = useRef(true), state = useRef(status), previewing = useRef(false);
|
|
10
|
+
useEffect(() => () => { active.current = false; }, []);
|
|
11
|
+
const nativeMode = mode === 'scaleToFill' || mode === 'aspectFit' || mode === 'aspectFill';
|
|
12
|
+
// Native image sizing follows its parent on the main thread. Measuring these
|
|
13
|
+
// modes would send every animated height through the background renderer.
|
|
14
|
+
const frame = !nativeMode && natural && box.width > 0 && box.height > 0 ? imgFrame(mode, box, natural) : null;
|
|
15
|
+
const autoHeight = mode === 'widthFix' && height === undefined && natural && box.width > 0 ? Math.round(box.width * natural.height / natural.width) : undefined;
|
|
16
|
+
const autoWidth = mode === 'heightFix' && width === undefined && natural && box.height > 0 ? Math.round(box.height * natural.width / natural.height) : undefined;
|
|
17
|
+
const tap = () => {
|
|
18
|
+
'background only';
|
|
19
|
+
onClick?.();
|
|
20
|
+
if (!enablePreview || state.current !== 'success' || !src || previewing.current)
|
|
21
|
+
return;
|
|
22
|
+
previewing.current = true;
|
|
23
|
+
void Promise.resolve().then(() => {
|
|
24
|
+
if (!onPreview)
|
|
25
|
+
throw new Error('请提供图片预览适配器');
|
|
26
|
+
return onPreview(previewSrc || src);
|
|
27
|
+
}).catch(reason => { if (active.current)
|
|
28
|
+
onPreviewError?.(reason instanceof Error ? reason : new Error(String(reason))); }).finally(() => { previewing.current = false; });
|
|
29
|
+
};
|
|
30
|
+
return <view className={`ui-img ${round ? 'ui-img-round' : ''} ui-img-${status} ${className}`} style={{ width: autoWidth === undefined ? imgDimension(width, 320) : `${autoWidth}px`, height: autoHeight === undefined ? imgDimension(height, 240) : `${autoHeight}px`, ...(radius === undefined ? {} : { borderRadius: imgDimension(radius, 0) }), ...style }} flatten={false} bindlayoutchange={event => {
|
|
31
|
+
'background only';
|
|
32
|
+
if (nativeMode)
|
|
33
|
+
return;
|
|
34
|
+
const { width: w, height: h } = event.detail;
|
|
35
|
+
if (Number.isFinite(w) && Number.isFinite(h) && w >= 0 && h >= 0)
|
|
36
|
+
setBox(previous => previous.width === w && previous.height === h ? previous : { width: w, height: h });
|
|
37
|
+
}} bindtap={tap} accessibility-element={!!alt || enablePreview} accessibility-label={alt || '图片'} accessibility-traits={enablePreview ? 'button' : 'image'}>
|
|
38
|
+
{src && status !== 'error' ? <image className={`ui-img-image ${customImage}`} src={src} mode={frame?.mode || (mode === 'aspectFit' || mode === 'aspectFill' ? mode : 'scaleToFill')} style={frame ? { width: `${frame.width}px`, height: `${frame.height}px`, left: `${frame.left}px`, top: `${frame.top}px` } : { width: '100%', height: '100%', left: 0, top: 0 }} bindload={event => {
|
|
39
|
+
'background only';
|
|
40
|
+
if (!active.current || state.current !== 'loading')
|
|
41
|
+
return;
|
|
42
|
+
const { width: w, height: h } = event.detail;
|
|
43
|
+
if (Number.isFinite(w) && Number.isFinite(h) && w > 0 && h > 0)
|
|
44
|
+
setNatural({ width: w, height: h });
|
|
45
|
+
state.current = 'success';
|
|
46
|
+
setStatus('success');
|
|
47
|
+
onLoad?.(event);
|
|
48
|
+
}} binderror={event => {
|
|
49
|
+
'background only';
|
|
50
|
+
if (!active.current || state.current !== 'loading')
|
|
51
|
+
return;
|
|
52
|
+
state.current = 'error';
|
|
53
|
+
setStatus('error');
|
|
54
|
+
onError?.(event);
|
|
55
|
+
}}/> : null}
|
|
56
|
+
{status === 'loading' && showLoading ? <view className="ui-img-placeholder ui-img-loading-placeholder">{loading ?? <Icon name="image" size={32} color="#a9acb8"/>}</view> : null}
|
|
57
|
+
{status === 'error' && showError ? <view className="ui-img-placeholder ui-img-error-placeholder">{error ?? <Icon name="image-failloading" size={32} color="#a9acb8"/>}</view> : null}
|
|
58
|
+
</view>;
|
|
59
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type { CSSProperties, NodesRef, StandardProps } from '@lynx-js/types';
|
|
2
|
+
import type { ImgCropperResult, ImgCropperSignal } from './imgCropperModel.js';
|
|
3
|
+
export type { ImgCropperResult } from './imgCropperModel.js';
|
|
4
|
+
type BrowserCropperProps = StandardProps & {
|
|
5
|
+
'cropper-config': string;
|
|
6
|
+
ref?: {
|
|
7
|
+
current: NodesRef | null;
|
|
8
|
+
};
|
|
9
|
+
bindcropperevent?: (event: {
|
|
10
|
+
detail: ImgCropperSignal;
|
|
11
|
+
}) => void;
|
|
12
|
+
};
|
|
13
|
+
declare module '@lynx-js/types' {
|
|
14
|
+
interface IntrinsicElements {
|
|
15
|
+
'daisu-img-cropper': BrowserCropperProps;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
declare global {
|
|
19
|
+
namespace JSX {
|
|
20
|
+
interface IntrinsicElements {
|
|
21
|
+
'daisu-img-cropper': BrowserCropperProps;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
export type ImgCropperProps = {
|
|
26
|
+
show?: boolean;
|
|
27
|
+
onShowChange?: (show: boolean) => void;
|
|
28
|
+
imgSrc?: string;
|
|
29
|
+
imgWidth?: number | string;
|
|
30
|
+
imgHeight?: number | string;
|
|
31
|
+
aspectRatio?: string;
|
|
32
|
+
disabledRotate?: boolean;
|
|
33
|
+
exportScale?: number;
|
|
34
|
+
maxScale?: number;
|
|
35
|
+
quality?: number;
|
|
36
|
+
fileType?: 'png' | 'jpg' | 'jpeg';
|
|
37
|
+
cancelButtonText?: string;
|
|
38
|
+
confirmButtonText?: string;
|
|
39
|
+
autoChoose?: boolean;
|
|
40
|
+
mode?: 'browser' | 'none';
|
|
41
|
+
rootPortal?: boolean;
|
|
42
|
+
topInset?: number;
|
|
43
|
+
zIndex?: number;
|
|
44
|
+
onConfirm?: (result: ImgCropperResult) => void;
|
|
45
|
+
onCancel?: () => void;
|
|
46
|
+
onImgLoaded?: (event: {
|
|
47
|
+
res: {
|
|
48
|
+
width: number;
|
|
49
|
+
height: number;
|
|
50
|
+
};
|
|
51
|
+
}) => void;
|
|
52
|
+
onImgLoadError?: (event: {
|
|
53
|
+
err: string;
|
|
54
|
+
}) => void;
|
|
55
|
+
onError?: (error: {
|
|
56
|
+
message: string;
|
|
57
|
+
phase?: string;
|
|
58
|
+
}) => void;
|
|
59
|
+
className?: string;
|
|
60
|
+
style?: CSSProperties;
|
|
61
|
+
};
|
|
62
|
+
export type ImgCropperRef = {
|
|
63
|
+
open: (src?: string) => boolean;
|
|
64
|
+
close: () => void;
|
|
65
|
+
chooseImage: () => boolean;
|
|
66
|
+
confirm: () => boolean;
|
|
67
|
+
setRotate: (deg: number) => boolean;
|
|
68
|
+
resetImg: () => boolean;
|
|
69
|
+
revertIsAnimation: (animation: boolean) => boolean;
|
|
70
|
+
downloadResult: (url: string) => boolean;
|
|
71
|
+
revokeResult: (url: string) => void;
|
|
72
|
+
};
|
|
73
|
+
/** Browser canvas editor. The browser host must call registerImgCropper(). */
|
|
74
|
+
export declare const ImgCropper: (props: ImgCropperProps & import("react").RefAttributes<ImgCropperRef>) => React.ReactElement | null;
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { forwardRef, useEffect, useImperativeHandle, useMemo, useRef, useState } from '@lynx-js/react';
|
|
2
|
+
import { Overlay } from "./Overlay.jsx";
|
|
3
|
+
import { cropperAspect } from './imgCropperModel.js';
|
|
4
|
+
/** Browser canvas editor. The browser host must call registerImgCropper(). */
|
|
5
|
+
export const ImgCropper = forwardRef(function ImgCropper({ show, onShowChange, imgSrc = '', imgWidth, imgHeight, aspectRatio = '1:1', disabledRotate = false, exportScale = 2, maxScale = 3, quality = 1, fileType = 'png', cancelButtonText = '取消', confirmButtonText = '完成', autoChoose = false, mode = 'none', rootPortal = false, topInset = 0, zIndex = 1000, onConfirm, onCancel, onImgLoaded, onImgLoadError, onError, className = '', style }, ref) {
|
|
6
|
+
const [innerShow, setShow] = useState(false), [override, setOverride] = useState(), node = useRef(null), closing = useRef(false);
|
|
7
|
+
const visible = show ?? innerShow;
|
|
8
|
+
const error = useMemo(() => { try {
|
|
9
|
+
cropperAspect(aspectRatio);
|
|
10
|
+
if (!['png', 'jpg', 'jpeg'].includes(fileType))
|
|
11
|
+
throw new Error('fileType 应为 png 或 jpg');
|
|
12
|
+
for (const dimension of [imgWidth, imgHeight])
|
|
13
|
+
if (dimension !== undefined && !(typeof dimension === 'number' && Number.isFinite(dimension) && dimension > 0) && !(typeof dimension === 'string' && /^\d+(\.\d+)?%$/.test(dimension) && parseFloat(dimension) > 0))
|
|
14
|
+
throw new Error('初始图片尺寸应为正数 px 或百分比');
|
|
15
|
+
if (!Number.isFinite(exportScale) || exportScale <= 0 || exportScale > 8)
|
|
16
|
+
throw new Error('exportScale 应为0–8内的正数');
|
|
17
|
+
if (!Number.isFinite(maxScale) || maxScale < 1 || maxScale > 20)
|
|
18
|
+
throw new Error('maxScale 应为1–20');
|
|
19
|
+
if (!Number.isFinite(quality) || quality < 0 || quality > 1)
|
|
20
|
+
throw new Error('quality 应为0–1');
|
|
21
|
+
return '';
|
|
22
|
+
}
|
|
23
|
+
catch (reason) {
|
|
24
|
+
return String(reason);
|
|
25
|
+
} }, [aspectRatio, exportScale, maxScale, quality, fileType, imgWidth, imgHeight]);
|
|
26
|
+
const state = useRef({ visible, onShowChange, onConfirm, onCancel, onImgLoaded, onImgLoadError, onError, mode, error });
|
|
27
|
+
state.current = { visible, onShowChange, onConfirm, onCancel, onImgLoaded, onImgLoadError, onError, mode, error };
|
|
28
|
+
const change = (value) => {
|
|
29
|
+
'background only';
|
|
30
|
+
setShow(value);
|
|
31
|
+
state.current.onShowChange?.(value);
|
|
32
|
+
};
|
|
33
|
+
const cancel = () => {
|
|
34
|
+
'background only';
|
|
35
|
+
if (!state.current.visible || closing.current)
|
|
36
|
+
return;
|
|
37
|
+
closing.current = true;
|
|
38
|
+
change(false);
|
|
39
|
+
state.current.onCancel?.();
|
|
40
|
+
};
|
|
41
|
+
const invoke = (method, params = {}, closed = false) => {
|
|
42
|
+
'background only';
|
|
43
|
+
if (state.current.mode !== 'browser' || !node.current || (!closed && !state.current.visible))
|
|
44
|
+
return false;
|
|
45
|
+
node.current.invoke({ method, params }).exec();
|
|
46
|
+
return true;
|
|
47
|
+
};
|
|
48
|
+
useImperativeHandle(ref, () => ({
|
|
49
|
+
open(src) {
|
|
50
|
+
'background only';
|
|
51
|
+
if (state.current.mode !== 'browser' || state.current.error) {
|
|
52
|
+
state.current.onError?.({ message: state.current.error || '当前环境不支持图片裁剪' });
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
setOverride(src);
|
|
56
|
+
closing.current = false;
|
|
57
|
+
change(true);
|
|
58
|
+
return true;
|
|
59
|
+
}, close: cancel,
|
|
60
|
+
chooseImage() {
|
|
61
|
+
'background only';
|
|
62
|
+
return invoke('chooseImage');
|
|
63
|
+
}, confirm() {
|
|
64
|
+
'background only';
|
|
65
|
+
return invoke('confirm');
|
|
66
|
+
},
|
|
67
|
+
setRotate(deg) {
|
|
68
|
+
'background only';
|
|
69
|
+
return Number.isFinite(deg) && !disabledRotate && invoke('setRotate', { deg });
|
|
70
|
+
}, resetImg() {
|
|
71
|
+
'background only';
|
|
72
|
+
return invoke('resetImg');
|
|
73
|
+
},
|
|
74
|
+
revertIsAnimation(animation) {
|
|
75
|
+
'background only';
|
|
76
|
+
return invoke('revertIsAnimation', { animation });
|
|
77
|
+
}, downloadResult(url) {
|
|
78
|
+
'background only';
|
|
79
|
+
return invoke('downloadResult', { url }, true);
|
|
80
|
+
}, revokeResult(url) {
|
|
81
|
+
'background only';
|
|
82
|
+
invoke('revokeResult', { url }, true);
|
|
83
|
+
}
|
|
84
|
+
}), [disabledRotate]);
|
|
85
|
+
useEffect(() => { if (visible)
|
|
86
|
+
closing.current = false; if (visible && (error || mode !== 'browser'))
|
|
87
|
+
state.current.onError?.({ message: error || '当前环境不支持图片裁剪', phase: 'config' }); }, [visible, error, mode]);
|
|
88
|
+
const inset = Math.max(0, Number.isFinite(topInset) ? topInset : 0), config = { show: visible && !error, imgSrc: override ?? imgSrc, imgWidth, imgHeight, aspectRatio, disabledRotate, exportScale, maxScale, quality, fileType: fileType === 'jpeg' ? 'jpg' : fileType, cancelButtonText, confirmButtonText, topInset: inset, autoChoose };
|
|
89
|
+
return <Overlay show={visible && mode === 'browser' && !error} rootPortal={rootPortal} duration={0} zIndex={zIndex} style={{ top: `${inset}px`, backgroundColor: '#000' }} lockScroll>
|
|
90
|
+
{mode === 'browser' ? <daisu-img-cropper ref={node} className={`ui-img-cropper ${className}`} style={style} cropper-config={JSON.stringify(config)} bindcropperevent={({ detail: event }) => {
|
|
91
|
+
'background only';
|
|
92
|
+
if (!state.current.visible || closing.current)
|
|
93
|
+
return;
|
|
94
|
+
if (event.kind === 'confirm' && event.result) {
|
|
95
|
+
closing.current = true;
|
|
96
|
+
try {
|
|
97
|
+
state.current.onConfirm?.(event.result);
|
|
98
|
+
}
|
|
99
|
+
finally {
|
|
100
|
+
change(false);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
else if (event.kind === 'cancel')
|
|
104
|
+
cancel();
|
|
105
|
+
else if (event.kind === 'loaded')
|
|
106
|
+
state.current.onImgLoaded?.({ res: { width: event.width || 0, height: event.height || 0 } });
|
|
107
|
+
else if (event.kind === 'error') {
|
|
108
|
+
if (event.phase !== 'export')
|
|
109
|
+
state.current.onImgLoadError?.({ err: event.message || '图片处理失败' });
|
|
110
|
+
state.current.onError?.({ message: event.message || '图片处理失败', phase: event.phase });
|
|
111
|
+
}
|
|
112
|
+
}}/> : null}
|
|
113
|
+
</Overlay>;
|
|
114
|
+
});
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { ReactNode } from '@lynx-js/react';
|
|
2
|
+
import type { CSSProperties } from '@lynx-js/types';
|
|
3
|
+
import type { AnchorIndex, IndexBarSection } from './indexBarModel.js';
|
|
4
|
+
export type IndexAnchorProps = {
|
|
5
|
+
index: AnchorIndex;
|
|
6
|
+
children?: ReactNode;
|
|
7
|
+
className?: string;
|
|
8
|
+
style?: CSSProperties;
|
|
9
|
+
};
|
|
10
|
+
export declare function IndexAnchor({ index, children, className, style }: IndexAnchorProps): import("@lynx-js/react").JSX.Element;
|
|
11
|
+
export type IndexBarRenderItem<T> = {
|
|
12
|
+
item: T;
|
|
13
|
+
itemIndex: number;
|
|
14
|
+
section: IndexBarSection<T>;
|
|
15
|
+
sectionIndex: number;
|
|
16
|
+
};
|
|
17
|
+
export type IndexBarProps<T> = {
|
|
18
|
+
listId: string;
|
|
19
|
+
sections: readonly IndexBarSection<T>[];
|
|
20
|
+
renderItem: (state: IndexBarRenderItem<T>) => ReactNode;
|
|
21
|
+
renderAnchor?: (state: {
|
|
22
|
+
section: IndexBarSection<T>;
|
|
23
|
+
sectionIndex: number;
|
|
24
|
+
}) => ReactNode;
|
|
25
|
+
getItemKey?: (state: IndexBarRenderItem<T>) => string | number;
|
|
26
|
+
sticky?: boolean;
|
|
27
|
+
itemHeight?: number;
|
|
28
|
+
anchorHeight?: number;
|
|
29
|
+
onChange?: (event: {
|
|
30
|
+
index: AnchorIndex;
|
|
31
|
+
}) => void;
|
|
32
|
+
className?: string;
|
|
33
|
+
style?: CSSProperties;
|
|
34
|
+
};
|
|
35
|
+
/** Stable, predictable row sizes let the native List jump without mounting every city. */
|
|
36
|
+
export declare function IndexBar<T>(props: IndexBarProps<T>): import("@lynx-js/react").JSX.Element;
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { useEffect, useRef, useState } from '@lynx-js/react';
|
|
2
|
+
import { List } from '@lynx-js/lynx-ui';
|
|
3
|
+
import { anchorKey, indexBarIndexAtPoint, indexBarLayout, indexBarSectionAtOffset, indexBarSize, indexBarStickyOffset } from './indexBarModel.js';
|
|
4
|
+
export function IndexAnchor({ index, children, className = '', style }) {
|
|
5
|
+
const content = children ?? index;
|
|
6
|
+
return <view className={`ui-index-anchor ${className}`} style={style}>{typeof content === 'string' || typeof content === 'number' ? <text>{content}</text> : content}</view>;
|
|
7
|
+
}
|
|
8
|
+
/** Stable, predictable row sizes let the native List jump without mounting every city. */
|
|
9
|
+
export function IndexBar(props) {
|
|
10
|
+
const identity = JSON.stringify([props.listId, props.sections.map(section => [section.index, section.data.length]), props.itemHeight, props.anchorHeight]);
|
|
11
|
+
return <IndexBarContent key={identity} {...props}/>;
|
|
12
|
+
}
|
|
13
|
+
function IndexBarContent({ listId, sections, renderItem, renderAnchor, getItemKey, sticky = false, itemHeight = 44, anchorHeight = 36, onChange, className = '', style }) {
|
|
14
|
+
const list = useRef(null);
|
|
15
|
+
const rail = useRef(null);
|
|
16
|
+
const [selected, setSelected] = useState(sections.length ? 0 : -1);
|
|
17
|
+
const [touching, setTouching] = useState(false);
|
|
18
|
+
const [stickyOffset, setStickyOffset] = useState(0);
|
|
19
|
+
const active = useRef(selected);
|
|
20
|
+
const lastTouch = useRef(0);
|
|
21
|
+
const lastEnd = useRef(0);
|
|
22
|
+
const drag = useRef({ sequence: 0, pressed: false, ended: false, moved: false, point: 0 });
|
|
23
|
+
const rowSize = indexBarSize(itemHeight, 44);
|
|
24
|
+
const anchorSize = indexBarSize(anchorHeight, 36);
|
|
25
|
+
const layout = indexBarLayout(sections, rowSize, anchorSize);
|
|
26
|
+
useEffect(() => () => { drag.current.sequence++; drag.current.pressed = false; }, []);
|
|
27
|
+
const activate = (sectionIndex, scroll) => {
|
|
28
|
+
'background only';
|
|
29
|
+
const section = sections[sectionIndex];
|
|
30
|
+
if (!section)
|
|
31
|
+
return;
|
|
32
|
+
if (active.current !== sectionIndex) {
|
|
33
|
+
active.current = sectionIndex;
|
|
34
|
+
setSelected(sectionIndex);
|
|
35
|
+
onChange?.({ index: section.index });
|
|
36
|
+
}
|
|
37
|
+
if (scroll) {
|
|
38
|
+
setStickyOffset(0);
|
|
39
|
+
list.current?.scrollTo(false, 'top', layout[sectionIndex].position, 0);
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
const coordinate = (event) => {
|
|
43
|
+
'background only';
|
|
44
|
+
const touch = event.touches?.[0] ?? event.changedTouches?.[0];
|
|
45
|
+
return [touch?.clientY, touch?.pageY, event.clientY, event.pageY, event.detail?.clientY, event.detail?.pageY, touch?.y, event.y, event.detail?.y].find(point => typeof point === 'number' && Number.isFinite(point));
|
|
46
|
+
};
|
|
47
|
+
const applyPoint = () => {
|
|
48
|
+
'background only';
|
|
49
|
+
const current = drag.current;
|
|
50
|
+
if (!current.rect || (!current.moved && !current.ended))
|
|
51
|
+
return;
|
|
52
|
+
const index = indexBarIndexAtPoint(current.point, current.rect.top, current.rect.height, sections.length);
|
|
53
|
+
if (index !== active.current || current.ended)
|
|
54
|
+
activate(index, true);
|
|
55
|
+
if (current.ended) {
|
|
56
|
+
current.pressed = false;
|
|
57
|
+
setTouching(false);
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
const start = (event, mouse = false) => {
|
|
61
|
+
'background only';
|
|
62
|
+
if (mouse && Date.now() - lastTouch.current < 500)
|
|
63
|
+
return;
|
|
64
|
+
if (!mouse)
|
|
65
|
+
lastTouch.current = Date.now();
|
|
66
|
+
const point = coordinate(event);
|
|
67
|
+
if (point === undefined || !rail.current || !sections.length)
|
|
68
|
+
return;
|
|
69
|
+
const sequence = drag.current.sequence + 1;
|
|
70
|
+
drag.current = { sequence, pressed: true, ended: false, moved: false, point };
|
|
71
|
+
setTouching(true);
|
|
72
|
+
rail.current.invoke({ method: 'boundingClientRect', params: { androidEnableTransformProps: true, iosEnableTransformProps: true }, success: (result) => {
|
|
73
|
+
'background only';
|
|
74
|
+
if (drag.current.sequence !== sequence)
|
|
75
|
+
return;
|
|
76
|
+
drag.current.rect = result;
|
|
77
|
+
applyPoint();
|
|
78
|
+
}, fail: () => {
|
|
79
|
+
'background only';
|
|
80
|
+
if (drag.current.sequence === sequence) {
|
|
81
|
+
drag.current.pressed = false;
|
|
82
|
+
setTouching(false);
|
|
83
|
+
}
|
|
84
|
+
} }).exec();
|
|
85
|
+
};
|
|
86
|
+
const move = (event) => {
|
|
87
|
+
'background only';
|
|
88
|
+
if (!drag.current.pressed)
|
|
89
|
+
return;
|
|
90
|
+
const point = coordinate(event);
|
|
91
|
+
if (point === undefined)
|
|
92
|
+
return;
|
|
93
|
+
drag.current.point = point;
|
|
94
|
+
drag.current.moved = true;
|
|
95
|
+
applyPoint();
|
|
96
|
+
};
|
|
97
|
+
const end = (event) => {
|
|
98
|
+
'background only';
|
|
99
|
+
if (!drag.current.pressed || drag.current.ended)
|
|
100
|
+
return;
|
|
101
|
+
const point = coordinate(event);
|
|
102
|
+
if (point !== undefined)
|
|
103
|
+
drag.current.point = point;
|
|
104
|
+
drag.current.ended = true;
|
|
105
|
+
lastEnd.current = Date.now();
|
|
106
|
+
setTouching(false);
|
|
107
|
+
applyPoint();
|
|
108
|
+
};
|
|
109
|
+
const cancel = () => {
|
|
110
|
+
'background only';
|
|
111
|
+
drag.current.sequence++;
|
|
112
|
+
drag.current.pressed = false;
|
|
113
|
+
setTouching(false);
|
|
114
|
+
};
|
|
115
|
+
return <view className={`ui-index-bar ${className}`} style={style}>
|
|
116
|
+
<List ref={list} listId={listId} listType="single" spanCount={1} scrollOrientation="vertical" scrollEventThrottle={16} className="ui-index-bar-list" onScroll={event => {
|
|
117
|
+
'background only';
|
|
118
|
+
const section = indexBarSectionAtOffset(layout, event.detail.scrollTop);
|
|
119
|
+
if (!drag.current.pressed)
|
|
120
|
+
activate(section, false);
|
|
121
|
+
if (sticky)
|
|
122
|
+
setStickyOffset(indexBarStickyOffset(layout, section, event.detail.scrollTop, anchorSize));
|
|
123
|
+
}}>
|
|
124
|
+
{sections.flatMap((section, sectionIndex) => [
|
|
125
|
+
<list-item key={`anchor:${anchorKey(section.index)}`} item-key={`anchor:${anchorKey(section.index)}`} estimated-main-axis-size-px={anchorSize} style={{ height: `${anchorSize}px` }}><IndexAnchor index={section.index} style={{ height: `${anchorSize}px` }}>{renderAnchor?.({ section, sectionIndex })}</IndexAnchor></list-item>,
|
|
126
|
+
...section.data.map((item, itemIndex) => {
|
|
127
|
+
const state = { item, itemIndex, section, sectionIndex };
|
|
128
|
+
const key = `item:${anchorKey(section.index)}:${getItemKey?.(state) ?? itemIndex}`;
|
|
129
|
+
return <list-item key={key} item-key={key} estimated-main-axis-size-px={rowSize} style={{ height: `${rowSize}px` }}>{renderItem(state)}</list-item>;
|
|
130
|
+
}),
|
|
131
|
+
])}
|
|
132
|
+
</List>
|
|
133
|
+
{sticky && sections[selected] ? <view className="ui-index-bar-sticky" user-interaction-enabled={false} style={{ transform: `translateY(${stickyOffset}px)` }}><IndexAnchor index={sections[selected].index} style={{ height: `${anchorSize}px` }}>{renderAnchor?.({ section: sections[selected], sectionIndex: selected })}</IndexAnchor></view> : null}
|
|
134
|
+
{touching && sections[selected] ? <view className="ui-index-bar-current" user-interaction-enabled={false}><text>{sections[selected].index}</text></view> : null}
|
|
135
|
+
{sections.length ? <view ref={rail} className="ui-index-bar-rail" consume-slide-event={[[-180, 180]]} catchtouchstart={event => {
|
|
136
|
+
'background only';
|
|
137
|
+
start(event);
|
|
138
|
+
}} catchtouchmove={move} catchtouchend={end} catchtouchcancel={cancel} catchmousedown={event => {
|
|
139
|
+
'background only';
|
|
140
|
+
start(event, true);
|
|
141
|
+
}} global-bindmousemove={move} global-bindmouseup={end} global-bindtouchmove={move} global-bindtouchend={end} global-bindtouchcancel={cancel}>
|
|
142
|
+
{sections.map((section, index) => <view key={anchorKey(section.index)} className={`ui-index-bar-index ${selected === index ? 'ui-index-bar-index-active' : ''}`} accessibility-element accessibility-traits="button" accessibility-label={`索引 ${section.index}${selected === index ? ',已选中' : ''}`} bindtap={() => {
|
|
143
|
+
'background only';
|
|
144
|
+
if (Date.now() - lastEnd.current >= 400)
|
|
145
|
+
activate(index, true);
|
|
146
|
+
}}><text>{section.index}</text></view>)}
|
|
147
|
+
</view> : null}
|
|
148
|
+
</view>;
|
|
149
|
+
}
|
package/dist/Input.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { TextFieldProps, TextFieldRef } from "./TextField.jsx";
|
|
2
|
+
export type InputProps = TextFieldProps & {
|
|
3
|
+
type?: 'text' | 'number' | 'digit' | 'password' | 'tel' | 'email';
|
|
4
|
+
showPassword?: boolean;
|
|
5
|
+
};
|
|
6
|
+
export type InputRef = TextFieldRef;
|
|
7
|
+
export declare const Input: (props: TextFieldProps & {
|
|
8
|
+
type?: "text" | "number" | "digit" | "password" | "tel" | "email";
|
|
9
|
+
showPassword?: boolean;
|
|
10
|
+
} & import("react").RefAttributes<TextFieldRef>) => React.ReactElement | null;
|
|
11
|
+
export type { TextFieldEvent } from "./TextField.jsx";
|
package/dist/Input.jsx
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { CSSProperties } from '@lynx-js/types';
|
|
2
|
+
import type { TextFieldEvent } from "./TextField.jsx";
|
|
3
|
+
import type { InputNumberValue, NumberRules } from './inputNumberModel.js';
|
|
4
|
+
export type { InputNumberValue } from './inputNumberModel.js';
|
|
5
|
+
export type InputNumberTheme = 'default' | 'primary' | 'outline-split' | 'outline';
|
|
6
|
+
export type InputNumberProps = NumberRules & {
|
|
7
|
+
value?: InputNumberValue;
|
|
8
|
+
defaultValue?: InputNumberValue;
|
|
9
|
+
onChange?: (event: {
|
|
10
|
+
value: InputNumberValue;
|
|
11
|
+
}) => void;
|
|
12
|
+
disabled?: boolean;
|
|
13
|
+
disableInput?: boolean;
|
|
14
|
+
disableMinus?: boolean;
|
|
15
|
+
disablePlus?: boolean;
|
|
16
|
+
withoutInput?: boolean;
|
|
17
|
+
inputWidth?: number | string;
|
|
18
|
+
placeholder?: string;
|
|
19
|
+
avoidKeyboard?: boolean;
|
|
20
|
+
beforeChange?: (value: InputNumberValue) => boolean | Promise<boolean>;
|
|
21
|
+
longPress?: boolean;
|
|
22
|
+
immediateChange?: boolean;
|
|
23
|
+
updateOnInit?: boolean;
|
|
24
|
+
inputType?: 'number' | 'digit';
|
|
25
|
+
theme?: InputNumberTheme;
|
|
26
|
+
round?: boolean;
|
|
27
|
+
className?: string;
|
|
28
|
+
style?: CSSProperties;
|
|
29
|
+
onFocus?: (event: TextFieldEvent) => void;
|
|
30
|
+
onBlur?: (event: TextFieldEvent) => void;
|
|
31
|
+
};
|
|
32
|
+
export declare function InputNumber({ value, defaultValue, onChange, min, max, step, precision, stepStrictly, allowNull, disabled: disabledProp, disableInput, disableMinus, disablePlus, withoutInput, inputWidth, placeholder, avoidKeyboard, beforeChange, longPress, immediateChange, updateOnInit, inputType, theme, round, className, style, onFocus, onBlur }: InputNumberProps): import("@lynx-js/react").JSX.Element;
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { FormFieldContext } from './formContext.js';
|
|
2
|
+
import { useContext, useEffect, useRef, useState } from '@lynx-js/react';
|
|
3
|
+
import { TextField } from "./TextField.jsx";
|
|
4
|
+
import { addNumber, normalizeNumber, numberDisplay, numberDraft, numberRules, sameNumber } from './inputNumberModel.js';
|
|
5
|
+
export function InputNumber({ value, defaultValue = 1, onChange, min = 1, max = Number.MAX_SAFE_INTEGER, step = 1, precision = 0, stepStrictly = false, allowNull = false, disabled: disabledProp, disableInput = false, disableMinus = false, disablePlus = false, withoutInput = false, inputWidth = 36, placeholder = '', avoidKeyboard = true, beforeChange, longPress = false, immediateChange = true, updateOnInit = true, inputType = 'digit', theme = 'default', round = false, className = '', style, onFocus, onBlur }) {
|
|
6
|
+
const field = useContext(FormFieldContext), disabled = disabledProp ?? field?.disabled ?? false;
|
|
7
|
+
const rules = numberRules({ min, max, step, precision, stepStrictly, allowNull });
|
|
8
|
+
const [inner, setInner] = useState(updateOnInit ? normalizeNumber(defaultValue, rules) : defaultValue);
|
|
9
|
+
const source = value === undefined ? inner : value;
|
|
10
|
+
const [draft, setDraft] = useState(() => numberDisplay(source, rules, updateOnInit));
|
|
11
|
+
const [settled, setSettled] = useState(0);
|
|
12
|
+
const first = useRef(true), generation = useRef(0), mounted = useRef(true), pending = useRef(null);
|
|
13
|
+
const currentDraft = useRef(draft);
|
|
14
|
+
currentDraft.current = draft;
|
|
15
|
+
const latest = useRef({ rules, source, disabled, disableMinus, disablePlus, beforeChange, onChange, controlled: value !== undefined });
|
|
16
|
+
latest.current = { rules, source, disabled, disableMinus, disablePlus, beforeChange, onChange, controlled: value !== undefined };
|
|
17
|
+
const timer = useRef(), repeat = useRef(false);
|
|
18
|
+
const stop = () => {
|
|
19
|
+
'background only';
|
|
20
|
+
clearTimeout(timer.current);
|
|
21
|
+
timer.current = undefined;
|
|
22
|
+
};
|
|
23
|
+
const display = (next) => {
|
|
24
|
+
'background only';
|
|
25
|
+
currentDraft.current = next;
|
|
26
|
+
setDraft(next);
|
|
27
|
+
};
|
|
28
|
+
useEffect(() => {
|
|
29
|
+
generation.current++;
|
|
30
|
+
pending.current = null;
|
|
31
|
+
if (first.current) {
|
|
32
|
+
first.current = false;
|
|
33
|
+
if (updateOnInit) {
|
|
34
|
+
const next = normalizeNumber(source, rules);
|
|
35
|
+
if (!sameNumber(source, next))
|
|
36
|
+
onChange?.({ value: next });
|
|
37
|
+
}
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
display(numberDisplay(source, rules));
|
|
41
|
+
}, [value, min, max, step, precision, stepStrictly, allowNull, disabled]);
|
|
42
|
+
useEffect(() => { stop(); }, [disabled, longPress, min, max, step, precision, stepStrictly]);
|
|
43
|
+
// A parent may reject even an identical edit. Reconcile after each transaction.
|
|
44
|
+
useEffect(() => {
|
|
45
|
+
if (settled && value !== undefined && !sameNumber(value, currentDraft.current))
|
|
46
|
+
display(numberDisplay(value, rules));
|
|
47
|
+
}, [settled]);
|
|
48
|
+
useEffect(() => { mounted.current = true; return () => { mounted.current = false; generation.current++; stop(); }; }, []);
|
|
49
|
+
const commit = async (raw, preserveDraft = false) => {
|
|
50
|
+
'background only';
|
|
51
|
+
const state = latest.current;
|
|
52
|
+
if (state.disabled)
|
|
53
|
+
return;
|
|
54
|
+
const next = normalizeNumber(raw, state.rules);
|
|
55
|
+
if (pending.current !== null && sameNumber(pending.current, next))
|
|
56
|
+
return;
|
|
57
|
+
const token = ++generation.current;
|
|
58
|
+
pending.current = null;
|
|
59
|
+
if (sameNumber(state.source, next)) {
|
|
60
|
+
display(preserveDraft ? String(raw) : numberDisplay(next, state.rules));
|
|
61
|
+
setSettled(n => n + 1);
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
pending.current = next;
|
|
65
|
+
try {
|
|
66
|
+
const decision = state.beforeChange?.(next) ?? true;
|
|
67
|
+
const accepted = typeof decision === 'boolean' ? decision : await decision;
|
|
68
|
+
if (!mounted.current || generation.current !== token)
|
|
69
|
+
return;
|
|
70
|
+
if (accepted) {
|
|
71
|
+
if (!state.controlled) {
|
|
72
|
+
latest.current.source = next;
|
|
73
|
+
setInner(next);
|
|
74
|
+
}
|
|
75
|
+
display(preserveDraft && sameNumber(raw, next) ? String(raw) : numberDisplay(next, state.rules));
|
|
76
|
+
latest.current.onChange?.({ value: next });
|
|
77
|
+
}
|
|
78
|
+
else
|
|
79
|
+
display(numberDisplay(latest.current.source, latest.current.rules));
|
|
80
|
+
}
|
|
81
|
+
catch {
|
|
82
|
+
if (mounted.current && generation.current === token)
|
|
83
|
+
display(numberDisplay(latest.current.source, latest.current.rules));
|
|
84
|
+
}
|
|
85
|
+
finally {
|
|
86
|
+
if (mounted.current && generation.current === token) {
|
|
87
|
+
pending.current = null;
|
|
88
|
+
setSettled(n => n + 1);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
const buttonDisabled = (direction) => {
|
|
93
|
+
const state = latest.current, base = normalizeNumber(currentDraft.current, state.rules);
|
|
94
|
+
if (base === '')
|
|
95
|
+
return state.disabled || (direction < 0 ? state.disableMinus : state.disablePlus);
|
|
96
|
+
const next = addNumber(base, direction * state.rules.step);
|
|
97
|
+
return state.disabled || (direction < 0 ? state.disableMinus || next < state.rules.min : state.disablePlus || next > state.rules.max);
|
|
98
|
+
};
|
|
99
|
+
const changeBy = (direction) => {
|
|
100
|
+
'background only';
|
|
101
|
+
if (buttonDisabled(direction) || pending.current !== null)
|
|
102
|
+
return;
|
|
103
|
+
const state = latest.current, base = normalizeNumber(currentDraft.current, state.rules);
|
|
104
|
+
void commit(base === '' ? state.rules.min : addNumber(base, direction * state.rules.step));
|
|
105
|
+
};
|
|
106
|
+
const start = (direction) => {
|
|
107
|
+
'background only';
|
|
108
|
+
stop();
|
|
109
|
+
repeat.current = false;
|
|
110
|
+
if (!longPress || buttonDisabled(direction))
|
|
111
|
+
return;
|
|
112
|
+
const tick = () => {
|
|
113
|
+
'background only';
|
|
114
|
+
repeat.current = true;
|
|
115
|
+
changeBy(direction);
|
|
116
|
+
timer.current = setTimeout(tick, 250);
|
|
117
|
+
};
|
|
118
|
+
timer.current = setTimeout(tick, 600);
|
|
119
|
+
};
|
|
120
|
+
const tap = (direction) => {
|
|
121
|
+
'background only';
|
|
122
|
+
stop();
|
|
123
|
+
if (repeat.current) {
|
|
124
|
+
repeat.current = false;
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
changeBy(direction);
|
|
128
|
+
};
|
|
129
|
+
const edit = (text) => {
|
|
130
|
+
'background only';
|
|
131
|
+
if (disabled || disableInput)
|
|
132
|
+
return;
|
|
133
|
+
generation.current++;
|
|
134
|
+
pending.current = null;
|
|
135
|
+
const clean = numberDraft(text, rules.precision);
|
|
136
|
+
display(clean);
|
|
137
|
+
if (!immediateChange || clean === '-' || clean.endsWith('.') || (clean === '' && !allowNull))
|
|
138
|
+
return;
|
|
139
|
+
void commit(clean, true);
|
|
140
|
+
};
|
|
141
|
+
const action = (direction) => <view className={`ui-input-number-action ui-input-number-${direction < 0 ? 'minus' : 'plus'} ${buttonDisabled(direction) ? 'ui-input-number-action-disabled' : ''}`} accessibility-element accessibility-traits="button" accessibility-label={direction < 0 ? '减少' : '增加'} catchtap={() => {
|
|
142
|
+
'background only';
|
|
143
|
+
tap(direction);
|
|
144
|
+
}} bindtouchstart={() => {
|
|
145
|
+
'background only';
|
|
146
|
+
start(direction);
|
|
147
|
+
}} bindtouchend={stop} bindtouchcancel={stop} bindtouchmove={stop} bindmousedown={() => {
|
|
148
|
+
'background only';
|
|
149
|
+
start(direction);
|
|
150
|
+
}} global-bindmouseup={stop}>
|
|
151
|
+
<view className="ui-input-number-horizontal"/>{direction > 0 ? <view className="ui-input-number-vertical"/> : null}<view className="ui-input-number-divider"/>
|
|
152
|
+
</view>;
|
|
153
|
+
return <view className={`ui-input-number ui-input-number-${theme} ${disabled ? 'ui-input-number-disabled' : ''} ${round ? 'ui-input-number-round' : ''} ${withoutInput ? 'ui-input-number-without-input' : ''} ${className}`} style={style}>
|
|
154
|
+
{action(-1)}
|
|
155
|
+
{!withoutInput ? <view className="ui-input-number-inner"><TextField compact type={inputType} textAlign="center" className="ui-input-number-editor" style={{ width: typeof inputWidth === 'number' ? `${Math.max(0, inputWidth)}px` : inputWidth }} value={draft} onChange={edit} disabled={disabled || disableInput} placeholder={placeholder} avoidKeyboard={avoidKeyboard} onFocus={onFocus} onBlur={event => {
|
|
156
|
+
'background only';
|
|
157
|
+
void commit(event.value);
|
|
158
|
+
onBlur?.(event);
|
|
159
|
+
}} onConfirm={event => {
|
|
160
|
+
'background only';
|
|
161
|
+
void commit(event.value);
|
|
162
|
+
}}/></view> : null}
|
|
163
|
+
{action(1)}
|
|
164
|
+
</view>;
|
|
165
|
+
}
|