feat: #22, link component supports params

This commit is contained in:
Emil Zhai 2021-12-20 20:52:11 +08:00
parent d5cded9180
commit bd342cb797
No known key found for this signature in database
GPG Key ID: 780B385DB72F1EBD
4 changed files with 82 additions and 3 deletions

View File

@ -22,6 +22,7 @@
"devDependencies": { "devDependencies": {
"@rollup/plugin-eslint": "^8.0.1", "@rollup/plugin-eslint": "^8.0.1",
"@types/cheerio": "0.22.22", "@types/cheerio": "0.22.22",
"@types/lodash": "^4.14.178",
"@types/react-window": "^1.8.5", "@types/react-window": "^1.8.5",
"@typescript-eslint/eslint-plugin": "4.33.0", "@typescript-eslint/eslint-plugin": "4.33.0",
"@typescript-eslint/parser": "4.33.0", "@typescript-eslint/parser": "4.33.0",

View File

@ -9,6 +9,7 @@
import React from 'react'; import React from 'react';
import { DripTableRecordTypeBase } from '@/types'; import { DripTableRecordTypeBase } from '@/types';
import { finalizeString } from '../utils';
import { DripTableComponentProps, DripTableComponentSchema } from '../component'; import { DripTableComponentProps, DripTableComponentSchema } from '../component';
export interface DTCLinkSchema extends DripTableComponentSchema { export interface DTCLinkSchema extends DripTableComponentSchema {
@ -46,7 +47,7 @@ export default class DTCLink<RecordType extends DripTableRecordTypeBase> extends
} }
return false; return false;
} }
if (schema.mode === 'single' && (schema.href || schema.event)) { if (schema.mode === 'single' && (finalizeString('pattern', schema.href || '', this.props.data) || schema.event)) {
return true; return true;
} }
return false; return false;
@ -73,7 +74,7 @@ export default class DTCLink<RecordType extends DripTableRecordTypeBase> extends
</a> </a>
); );
} }
return <a href={schema.href} target={schema.target}>{ schema.label }</a>; return <a href={finalizeString('pattern', schema.href || '', this.props.data)} target={schema.target}>{ schema.label }</a>;
} }
return ( return (
<div> <div>
@ -96,7 +97,16 @@ export default class DTCLink<RecordType extends DripTableRecordTypeBase> extends
</a> </a>
); );
} }
return <a style={{ marginRight: '5px' }} key={config.name || index} href={config.href} target={config.target}>{ config.label }</a>; return (
<a
style={{ marginRight: '5px' }}
key={config.name || index}
href={finalizeString('pattern', config.href || '', this.props.data)}
target={config.target}
>
{ config.label }
</a>
);
}) })
} }
</div> </div>

View File

@ -0,0 +1,63 @@
/**
* This file is part of the drip-table project.
* @link : https://drip-table.jd.com/
* @author : Emil Zhai (root@derzh.com)
* @modifier : Emil Zhai (root@derzh.com)
* @copyright: Copyright (c) 2021 JD Network Technology Co., Ltd.
*/
import get from 'lodash/get';
import { DripTableRecordTypeBase } from '@/types';
/**
*
* @param v
* @returns
*/
export const stringify = (v: unknown) => {
if (typeof v === 'object' && v !== null) {
try {
v = JSON.stringify(v);
} catch {}
}
if (v === void 0) {
return '';
}
return String(v);
};
/**
*
* @param mode
* @param text
* @param rec
* @returns
*/
export const finalizeString = (mode: 'plain' | 'key' | 'pattern' | 'script', text: string, rec: DripTableRecordTypeBase) => {
let value = '';
if (!mode || mode === 'plain') {
value = stringify(text);
} else if (mode === 'key') {
value = stringify(get(rec, text, ''));
} else if (mode === 'pattern') {
value = stringify(text)
.replace(/\{\{(.+?)\}\}/guis, (s, s1) => {
try {
return stringify(new Function('rec', `return ${s1}`)(rec));
} catch (error) {
return error instanceof Error
? `{{Render Error: ${error.message}}}`
: '{{Unknown Render Error}}';
}
});
} else if (mode === 'script') {
try {
value = stringify(new Function('rec', text)(rec));
} catch (error) {
value = error instanceof Error
? `Render Error: ${error.message}`
: 'Unknown Render Error';
}
}
return value;
};

View File

@ -2577,6 +2577,11 @@
resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.29.tgz#ee28707ae94e11d2b827bcbe5270bcea7f3e71ee" resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.29.tgz#ee28707ae94e11d2b827bcbe5270bcea7f3e71ee"
integrity sha1-7ihweulOEdK4J7y+UnC86n8+ce4= integrity sha1-7ihweulOEdK4J7y+UnC86n8+ce4=
"@types/lodash@^4.14.178":
version "4.14.178"
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.178.tgz#341f6d2247db528d4a13ddbb374bcdc80406f4f8"
integrity sha512-0d5Wd09ItQWH1qFbEyQ7oTQ3GZrMfth5JkbN3EvTKLXcHLRDSXeLnlvlOn0wvxVIwK5o2M8JzP/OWz7T3NRsbw==
"@types/mathjax@^0.0.36": "@types/mathjax@^0.0.36":
version "0.0.36" version "0.0.36"
resolved "https://registry.yarnpkg.com/@types/mathjax/-/mathjax-0.0.36.tgz#18cf766f88ac0cd4e7ee8282b1286049bb6aa682" resolved "https://registry.yarnpkg.com/@types/mathjax/-/mathjax-0.0.36.tgz#18cf766f88ac0cd4e7ee8282b1286049bb6aa682"