diff --git a/packages/form-render-react/src/components/Description.tsx b/packages/form-render-react/src/components/Description.tsx new file mode 100644 index 0000000..52dab24 --- /dev/null +++ b/packages/form-render-react/src/components/Description.tsx @@ -0,0 +1,7 @@ +import type { FC, ReactNode } from 'react' + +const Description: FC<{ children: ReactNode }> = ({ children }) => { + return <>{children} +} + +export default Description diff --git a/packages/form-render-react/src/constants/index.tsx b/packages/form-render-react/src/constants/index.tsx index f74dcf6..775dd09 100644 --- a/packages/form-render-react/src/constants/index.tsx +++ b/packages/form-render-react/src/constants/index.tsx @@ -53,3 +53,9 @@ export type IActionsLoading = typeof ACTIONS_DEFAULT_LOADING_STATE * Actions 渲染器名称 */ export const ACTIONS_RENDER_TYPE = '__FORM_RENDER_ACTIONS__' + +/** + * 默认时间展示格式 + */ +export const DEFAULT_DATE_FORMAT = 'YYYY-MM-DD' +export const DEFAULT_DATE_TIME_FORMAT = 'YYYY-MM-DD HH:mm:ss' diff --git a/packages/form-render-react/src/locale/en_US.ts b/packages/form-render-react/src/locale/en_US.ts index b569806..11ca24f 100644 --- a/packages/form-render-react/src/locale/en_US.ts +++ b/packages/form-render-react/src/locale/en_US.ts @@ -7,5 +7,7 @@ export default { reset: 'Reset', placeholderInput: 'Please enter ${title}', placeholderSelect: 'Please select ${title}', + comma: ',', + displayDate: '${start} to ${end}', }, } diff --git a/packages/form-render-react/src/locale/zh_CN.ts b/packages/form-render-react/src/locale/zh_CN.ts index 5e4d50e..7a499a4 100644 --- a/packages/form-render-react/src/locale/zh_CN.ts +++ b/packages/form-render-react/src/locale/zh_CN.ts @@ -7,5 +7,7 @@ export default { reset: '重置', placeholderInput: '请输入${title}', placeholderSelect: '请选择${title}', + comma: ',', + displayDate: '${start} 至 ${end}', }, } diff --git a/packages/form-render-react/src/renderers/Checkbox.tsx b/packages/form-render-react/src/renderers/Checkbox.tsx index 04c714a..5598c59 100644 --- a/packages/form-render-react/src/renderers/Checkbox.tsx +++ b/packages/form-render-react/src/renderers/Checkbox.tsx @@ -2,14 +2,25 @@ import type { IOpenComponentParams } from '@schema-render/core-react' import { Checkbox as AntCheckbox } from 'antd' import React from 'react' +import Description from '../components/Description' +import { getOptionsLabels } from '../utils' + type IValue = Array const Checkbox: React.FC> = ({ schema, disabled, + readonly, value, onChange, + locale, }) => { + // 只读态 + if (readonly) { + const labels = getOptionsLabels(schema.renderOptions?.options, value) + return {labels.join(locale.FormRender.comma)} + } + return ( > = ({ schema, value, onChange, disabled, + readonly, locale, validator, }) => { @@ -20,6 +24,22 @@ const DatePicker: React.FC> = ({ [schema.title, locale.FormRender.placeholderSelect] ) + // 只读态 + if (readonly) { + return ( + + {value + ? dayjs(value).format( + schema.renderOptions?.format || + (schema.renderOptions?.showTime + ? DEFAULT_DATE_TIME_FORMAT + : DEFAULT_DATE_FORMAT) + ) + : ''} + + ) + } + return ( > = ({ value, onChange, disabled, + readonly, validator, + locale, }) => { + // 只读态 + if (readonly) { + let displayText = '' + + if (utils.isArray(value) && value[0] && value[1]) { + const defaultFormat = schema.renderOptions?.showTime + ? DEFAULT_DATE_TIME_FORMAT + : DEFAULT_DATE_FORMAT + const format = schema.renderOptions?.format || defaultFormat + displayText = utils.templateCompiled(locale.FormRender.displayDate, { + start: dayjs(value?.[0]).format(format), + end: dayjs(value?.[1]).format(format), + }) + } + + return {displayText} + } + return ( > = ({ schema, disabled, + readonly, value, onChange, validator, @@ -19,6 +22,11 @@ const InputNumber: React.FC> = ({ [schema.title, locale.FormRender.placeholderInput] ) + // 只读态 + if (readonly) { + return {value} + } + return ( > = ({ schema, disabled, + readonly, value, onChange, validator, @@ -28,6 +31,11 @@ const InputText: React.FC> = ({ } } + // 只读态 + if (readonly) { + return {value} + } + return ( > = ({ schema, disabled, + readonly, value, onChange, validator, @@ -28,6 +31,11 @@ const Password: React.FC> = ({ } } + // 只读态 + if (readonly) { + return {value} + } + return ( > = ({ schema, disabled, + readonly, value, onChange, }) => { @@ -22,6 +26,12 @@ const Radio: React.FC> = ({ onChange(e.target.value) } + // 只读态 + if (readonly) { + const labels = getOptionsLabels(options, [value]) + return {labels[0]} + } + return ( {options.map((item) => { diff --git a/packages/form-render-react/src/renderers/Rate.tsx b/packages/form-render-react/src/renderers/Rate.tsx index eef5fdb..07885a9 100644 --- a/packages/form-render-react/src/renderers/Rate.tsx +++ b/packages/form-render-react/src/renderers/Rate.tsx @@ -4,6 +4,7 @@ import { Rate as AntRate } from 'antd' const Rate: React.FC> = ({ schema, disabled, + readonly, value, onChange, }) => { @@ -12,7 +13,7 @@ const Rate: React.FC> = ({ {...schema.renderOptions} value={value ?? 0} onChange={(val) => onChange(val)} - disabled={disabled} + disabled={disabled || readonly} /> ) } diff --git a/packages/form-render-react/src/renderers/Select.tsx b/packages/form-render-react/src/renderers/Select.tsx index 08add52..f0d2baa 100644 --- a/packages/form-render-react/src/renderers/Select.tsx +++ b/packages/form-render-react/src/renderers/Select.tsx @@ -3,9 +3,13 @@ import { utils } from '@schema-render/core-react' import { Select as AntSelect } from 'antd' import React, { useMemo } from 'react' +import Description from '../components/Description' +import { getOptionsLabels } from '../utils' + const Select: React.FC> = ({ schema, disabled, + readonly, value, onChange, locale, @@ -19,6 +23,12 @@ const Select: React.FC> = ({ [schema.title, locale.FormRender.placeholderSelect] ) + // 只读态 + if (readonly) { + const labels = getOptionsLabels(schema.renderOptions?.options, [value]) + return {labels.join(locale.FormRender?.comma)} + } + return ( > = ({ schema, disabled, + readonly, value, onChange, validator, @@ -19,6 +23,12 @@ const SelectMultiple: React.FC> = ({ [schema.title, locale.FormRender.placeholderSelect] ) + // 只读态 + if (readonly) { + const labels = getOptionsLabels(schema.renderOptions?.options, value) + return {labels.join(locale.FormRender?.comma)} + } + return ( > = ({ schema, disabled, + readonly, value, onChange, }) => { @@ -13,7 +14,7 @@ const Switch: React.FC> = ({ {...schema.renderOptions} checked={value} onChange={(val) => onChange(val)} - disabled={disabled} + disabled={disabled || readonly} /> ) } diff --git a/packages/form-render-react/src/renderers/TextArea.tsx b/packages/form-render-react/src/renderers/TextArea.tsx index aa6d42b..4daae78 100644 --- a/packages/form-render-react/src/renderers/TextArea.tsx +++ b/packages/form-render-react/src/renderers/TextArea.tsx @@ -3,9 +3,12 @@ import { utils } from '@schema-render/core-react' import { Input } from 'antd' import React, { useMemo } from 'react' +import Description from '../components/Description' + const TextArea: React.FC> = ({ schema, disabled, + readonly, value, onChange, validator, @@ -28,6 +31,11 @@ const TextArea: React.FC> = ({ } } + // 只读态 + if (readonly) { + return {value} + } + return ( { + if (!utils.isNil(value)) { + const result = utils.find(options, { value }) + if (result) { + labels.push(result.label as string) + } + } + }) + } + + return labels +}