@nutui/nutui-react-taro
Version:
京东风格的轻量级移动端 React 组件库,支持一套代码生成 H5 和小程序
211 lines (210 loc) • 5.87 kB
JavaScript
import { _ as __rest } from "./tslib.es6-iWu3F_1J.js";
import * as React from "react";
import React__default, { createContext, useContext } from "react";
import classNames from "classnames";
import kebabCase from "lodash.kebabcase";
import isequal from "lodash.isequal";
function useMemo(getValue, condition, shouldUpdate) {
const cacheRef = React.useRef({});
if (!("value" in cacheRef.current) || shouldUpdate(cacheRef.current.condition, condition)) {
cacheRef.current.value = getValue();
cacheRef.current.condition = condition;
}
return cacheRef.current.value;
}
const zhCN = {
save: "保存",
confirm: "确认",
cancel: "取消",
done: "完成",
noData: "暂无数据",
placeholder: "请输入内容",
select: "请选择",
edit: "编辑",
reset: "重置",
video: {
errorTip: "视频加载失败",
clickRetry: "点击重试"
},
fixednav: {
activeText: "收起导航",
inactiveText: "快速导航"
},
infiniteloading: {
pullRefreshText: "松开刷新",
loadText: "加载中",
loadMoreText: "没有更多了"
},
pagination: {
prev: "上一页",
next: "下一页"
},
range: {
rangeText: "不在该区间内"
},
calendaritem: {
weekdays: ["日", "一", "二", "三", "四", "五", "六"],
end: "结束",
start: "开始",
confirm: "确认",
title: "日历选择",
week: "周",
month: "月",
year: "年",
quarter: "季度",
monthTitle: (year, month) => `${year}年${Number(month) < 10 ? `0${Number(month)}` : month}月`,
today: "今天",
loadPreviousMonth: "加载上一个月",
noEarlierMonth: "没有更早月份"
},
shortpassword: {
title: "请输入密码",
description: "您使用了虚拟资产,请进行验证",
tips: "忘记密码"
},
uploader: {
list: "上传文件",
ready: "准备完成",
readyUpload: "准备上传",
waitingUpload: "等待上传",
uploading: "上传中...",
success: "上传成功",
error: "上传失败",
deleteWord: "用户阻止了删除!"
},
countdown: {
day: "天",
hour: "时",
minute: "分",
second: "秒"
},
address: {
selectRegion: "请选择地址",
deliveryTo: "配送至",
chooseAnotherAddress: "选择其他地址"
},
signature: {
reSign: "重签",
unsupported: "对不起,当前浏览器不支持Canvas,无法使用本控件!"
},
ecard: {
chooseText: "请选择电子卡面值",
otherValueText: "其他面值",
placeholder: "请输入1-5000整数"
},
timeselect: {
pickupTime: "取件时间"
},
sku: {
buyNow: "立即购买",
buyNumber: "购买数量",
addToCard: "加入购物车"
},
skuheader: {
skuId: "商品编号"
},
addresslist: {
addAddress: "新建地址"
},
comment: {
complaintsText: "我要投诉",
additionalReview: (day) => `购买${day}天后追评`,
additionalImages: (length) => `${length}张追评图片`
},
searchbar: {
basePlaceholder: "上京东,购好物",
text: "文本",
test: "测试",
title1: "基础用法",
title2: "搜索框形状及最大长度",
title3: "搜索框内外背景设置",
title4: "搜索框文本设置",
title5: "自定义图标设置",
title6: "数据改变监听"
},
audio: {
back: "快退",
forward: "快进",
pause: "暂停",
start: "开始",
mute: "静音",
tips: "onPlayEnd事件在loop=false时才会触发"
},
avatarCropper: { rotate: "旋转", selectImage: "选择图片" },
datepicker: {
year: "年",
month: "月",
day: "日",
hour: "时",
min: "分",
seconds: "秒"
},
pullToRefresh: {
pullingText: "下拉刷新",
canReleaseText: "松手刷新",
refreshingText: "刷新中",
completeText: "刷新成功"
},
tour: {
prevStepText: "上一步",
completeText: "完成",
nextStepText: "下一步"
},
watermark: {
errorCanvasTips: "当前环境不支持Canvas"
}
};
const classPrefix = "nut-configprovider";
const defaultConfigRef = {
current: {
locale: zhCN,
direction: "ltr"
}
};
const setDefaultConfig = (config) => {
defaultConfigRef.current = config;
};
const getDefaultConfig = () => {
return defaultConfigRef.current;
};
const ConfigContext = createContext(null);
const useConfig = () => {
var _a;
return (_a = useContext(ConfigContext)) !== null && _a !== void 0 ? _a : getDefaultConfig();
};
function convertThemeVarsToCSSVars(themeVars) {
const cssVars = {};
Object.keys(themeVars).forEach((key) => {
cssVars[`--${kebabCase(key)}`] = themeVars[key];
});
return cssVars;
}
const useRtl = () => {
const { direction } = useConfig();
return direction === "rtl";
};
const ConfigProvider = (props) => {
const { style, className, children, direction } = props, config = __rest(props, ["style", "className", "children", "direction"]);
const mergedConfig = useMemo(() => {
return Object.assign(Object.assign(Object.assign({}, getDefaultConfig()), config), { direction });
}, [config, direction], (prev, next) => prev.some((prevTheme, index) => {
const nextTheme = next[index];
return !isequal(prevTheme, nextTheme);
}));
const cssVarStyle = React__default.useMemo(() => {
return convertThemeVarsToCSSVars(mergedConfig.theme || {});
}, [mergedConfig.theme]);
return React__default.createElement(
ConfigContext.Provider,
{ value: mergedConfig },
React__default.createElement("div", { className: classNames(classPrefix, className, `nut-${direction}`), style: Object.assign(Object.assign(Object.assign({}, cssVarStyle), style), { direction }) }, children)
);
};
ConfigProvider.displayName = "NutConfigProvider";
export {
ConfigProvider as C,
useRtl as a,
getDefaultConfig as g,
setDefaultConfig as s,
useConfig as u
};