Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 使用子增 id 替换随机数 #1108

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions compiled/alipay/src/ImageUpload/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEvent } from 'functional-mini/component';
import { useEvent, useRef } from 'functional-mini/component';
import '../_util/assert-component2';
import { mountComponent } from '../_util/component';
import { useComponentEvent } from '../_util/hooks/useComponentEvent';
Expand All @@ -11,8 +11,26 @@ import {
LocalFile,
UploaderFunctionalProps,
} from './props';
import { useId } from 'functional-mini/compat';

/**
* 获取一个内部使用的 uid
* 每次获取时自增
*/
const useCounter = () => {
const counterRef = useRef(0);
// 使用 Date.now() 与 useId 作为前缀,防止每次前缀都相同
const prefix = useId() + '-' + Date.now();
return {
getCount() {
counterRef.current = counterRef.current + 1;
return `${prefix}-${counterRef.current}`;
},
};
};

const ImageUpload = (props: IUploaderProps) => {
const { getCount } = useCounter();
const [fileList, { isControlled, update, triggerUpdater }] = useMixState(
props.defaultFileList,
{
Expand All @@ -28,7 +46,7 @@ const ImageUpload = (props: IUploaderProps) => {
file.url = '';
}
if (typeof item.uid === 'undefined') {
file.uid = String(Math.random());
file.uid = getCount();
}
if (typeof item.status === 'undefined') {
file.status = 'done';
Expand All @@ -39,13 +57,14 @@ const ImageUpload = (props: IUploaderProps) => {
},
}
);

triggerRefEvent();
const { triggerEvent } = useComponentEvent(props);

async function uploadFile(localFile: LocalFile) {
const { onUpload } = props;

const uid = String(Math.random());
const uid = getCount();

triggerUpdater((oldFiles) => {
const tempFileList: File[] = [
Expand Down
23 changes: 20 additions & 3 deletions compiled/wechat/src/ImageUpload/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,32 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
}
return to.concat(ar || Array.prototype.slice.call(from));
};
import { useEvent } from 'functional-mini/component';
import { useEvent, useRef } from 'functional-mini/component';
import '../_util/assert-component2';
import { mountComponent } from '../_util/component';
import { useComponentEvent } from '../_util/hooks/useComponentEvent';
import { useMixState } from '../_util/hooks/useMixState';
import { triggerRefEvent } from '../_util/hooks/useReportRef';
import { chooseImage } from '../_util/jsapi/choose-image';
import { UploaderFunctionalProps, } from './props';
import { useId } from 'functional-mini/compat';
/**
* 获取一个内部使用的 uid
* 每次获取时自增
*/
var useCounter = function () {
var counterRef = useRef(0);
// 使用 Date.now() 与 useId 作为前缀,防止每次前缀都相同
var prefix = useId() + '-' + Date.now();
return {
getCount: function () {
counterRef.current = counterRef.current + 1;
return "".concat(prefix, "-").concat(counterRef.current);
},
};
};
var ImageUpload = function (props) {
var getCount = useCounter().getCount;
var _a = useMixState(props.defaultFileList, {
value: props.fileList,
postState: function (fileList) {
Expand All @@ -74,7 +91,7 @@ var ImageUpload = function (props) {
file.url = '';
}
if (typeof item.uid === 'undefined') {
file.uid = String(Math.random());
file.uid = getCount();
}
if (typeof item.status === 'undefined') {
file.status = 'done';
Expand All @@ -93,7 +110,7 @@ var ImageUpload = function (props) {
switch (_a.label) {
case 0:
onUpload = props.onUpload;
uid = String(Math.random());
uid = getCount();
triggerUpdater(function (oldFiles) {
var tempFileList = __spreadArray(__spreadArray([], oldFiles, true), [
{
Expand Down
25 changes: 22 additions & 3 deletions src/ImageUpload/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEvent } from 'functional-mini/component';
import { useEvent, useRef } from 'functional-mini/component';
import '../_util/assert-component2';
import { mountComponent } from '../_util/component';
import { useComponentEvent } from '../_util/hooks/useComponentEvent';
Expand All @@ -11,8 +11,26 @@ import {
LocalFile,
UploaderFunctionalProps,
} from './props';
import { useId } from 'functional-mini/compat';

/**
* 获取一个内部使用的 uid
* 每次获取时自增
*/
const useCounter = () => {
const counterRef = useRef(0);
// 使用 Date.now() 与 useId 作为前缀,防止每次前缀都相同
const prefix = useId() + '-' + Date.now();
return {
getCount() {
counterRef.current = counterRef.current + 1;
return `${prefix}-${counterRef.current}`;
},
};
};

const ImageUpload = (props: IUploaderProps) => {
const { getCount } = useCounter();
const [fileList, { isControlled, update, triggerUpdater }] = useMixState(
props.defaultFileList,
{
Expand All @@ -28,7 +46,7 @@ const ImageUpload = (props: IUploaderProps) => {
file.url = '';
}
if (typeof item.uid === 'undefined') {
file.uid = String(Math.random());
file.uid = getCount();
}
if (typeof item.status === 'undefined') {
file.status = 'done';
Expand All @@ -39,13 +57,14 @@ const ImageUpload = (props: IUploaderProps) => {
},
}
);

triggerRefEvent();
const { triggerEvent } = useComponentEvent(props);

async function uploadFile(localFile: LocalFile) {
const { onUpload } = props;

const uid = String(Math.random());
const uid = getCount();

triggerUpdater((oldFiles) => {
const tempFileList: File[] = [
Expand Down
Loading