简体   繁体   English

在 react-admin 中,如何为 UrlField href 添加前缀?

[英]In react-admin, how can I prefix a UrlField href?

Let's say I have the following:假设我有以下内容:

<UrlField source='fbUsername' />

If the result is fb username foo , this links relatively to /foo .如果结果是 fb username foo ,则此链接相对于/foo

(?) How can I prefix this url to: (?) 我如何将这个 url 作为前缀:

https://facebook.com/

So the result would be:所以结果是:

https://facebook.com/${fbUsername}

https://facebook.com/foo

<UrlField> only accepts a URL value. <UrlField>仅接受 URL 值。 For your use case, you should write a custom field based on the UrlField source .对于您的用例,您应该基于UrlField编写自定义字段。 Something like:就像是:


import * as React from 'react';
import { Link } from '@material-ui/core';
import { useRecordContext } from 'react-admin';

const MyUrlField = ({ source }) => {
    const record = useRecordContext();
    const value = record && record[source];

    if (value == null) {
        return null;
    }

    return (
        <Link href={`https://facebook.com/${value}`}>
            {value}
        </Link>
    );
});

MyUrlField.defaultProps = {
    addLabel: true,
};

Based on @françois-zaninotto 's answer, I fixed a syntax error fixed some of the missing useRecordContext() param that made it work:根据@françois-zaninotto 的回答,我修复了一个语法错误,修复了一些缺失的 useRecordContext() 参数,使其工作:

// ###############################################
// FbUrlField.js
import * as React from 'react';
import { Link } from '@material-ui/core';
import { useRecordContext } from 'react-admin';

const FbUrlField = ( props ) =>
{
    const { source, target, rel } = props;
    const record = useRecordContext(props);
    const value = record && record[source];

    if (value == null) {
        return null;
    }

    return (
        <Link href={`https://facebook.com/${value}`} target={target} rel={rel}>
            {value}
        </Link>
    );
};

FbUrlField.defaultProps = {
    addLabel: true,
};

export default FbUrlField;


// ###############################################
// SomeList.js
import FbUrlField from './FbUrlField';
[...]
<FbUrlField
    label='FB'
    source='fbUsername'
    target='_blank' // New window
    rel="noopener noreferrer" // For security
/>

I'm new to react-admin.我是反应管理员的新手。 I was following the tutorial and came across the idea of doing something like this because the user's website is just bla.com and it doesn't actually work because you get http://localhost:xxxx/bla.com as a link.我正在按照教程进行操作并遇到了做这样的事情的想法,因为用户的网站只是 bla.com 并且它实际上不起作用因为你得到http://localhost:xxxx/bla.com作为链接。 I came up with this method to do it, in typescript. Take it with a grain of salt:我在 typescript 中想出了这个方法来做到这一点。对它持保留态度:

/// user-url.field.tsx
import { UrlField, useRecordContext } from 'react-admin';

export const UserURLField = ({source}: {source: string}) => {
  const record = useRecordContext();
  const fakeRecord = {
    [source]: `http://${record[source]}`,
  };
  return (
  <UrlField source={source} record={fakeRecord}></UrlField>
  );
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何有条件地在react-admin中设置字段标签的格式? - How can I conditionally format the label for a field in react-admin? React-Admin:如何在没有 `source` 的情况下访问值? - React-Admin: How can I access a value without `source`? 如何将数据推送到 react-admin 存储? - How can i push data into react-admin store? 我如何使用像 react-image-editor 这样的组件在 react-admin 中裁剪图像 - How can I use a component like react-image-editor for cropping an image in react-admin 在 react-admin 中,如何在使用 fetch 获取数据时更新当前视图? - In react-admin, how can i update my current view when i using fetch to get data? 如何在 React Typescript 应用程序中导入 react-admin? - How do I import react-admin in a React Typescript appplication? 在哪里可以更改react-admin标头中的个人资料图片? - Where I can change the profile picture in the react-admin header? 如何使用#react-admin创建自定义页面而不使用登录页面等菜单侧栏? - How can I create a custom page with #react-admin without the menu sidebar like login page? 如何在 react-admin 中执行多个过滤器? - How can I execute multiple filters on top of each other in react-admin? 如何强制排序箭头始终显示在 react-admin Datagrid 中? - How can I force sorting arrows to always display in a react-admin Datagrid?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM