Skip to content

Commit b60cfbc

Browse files
committed
Customizable url field and add basic story to test it.
1 parent 98359d5 commit b60cfbc

File tree

2 files changed

+83
-3
lines changed

2 files changed

+83
-3
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { Paper, Stack } from '@mui/material';
2+
import * as React from 'react';
3+
4+
import { UrlField, UrlFieldProps } from './UrlField';
5+
import { AdminContext } from '../AdminContext';
6+
7+
export default { title: 'ra-ui-materialui/fields/UrlField' };
8+
9+
export const Basic = ({
10+
value,
11+
theme,
12+
...props
13+
}: Partial<UrlFieldProps> & { value?: string; theme?: 'light' | 'dark' }) => {
14+
return (
15+
<AdminContext defaultTheme={theme}>
16+
<Paper sx={{ p: 2 }}>
17+
<Stack direction="row">
18+
<UrlField record={{ value }} source="value" {...props} />
19+
</Stack>
20+
</Paper>
21+
</AdminContext>
22+
);
23+
};
24+
25+
Basic.argTypes = {
26+
value: {
27+
options: ['filled', 'empty', 'undefined'],
28+
mapping: {
29+
filled: 'https://example.org',
30+
empty: '',
31+
undefined: undefined,
32+
},
33+
control: { type: 'select' },
34+
},
35+
theme: {
36+
options: ['light', 'dark'],
37+
control: { type: 'select' },
38+
},
39+
};
40+
Basic.args = {
41+
theme: 'light',
42+
value: 'filled',
43+
};

packages/ra-ui-materialui/src/field/UrlField.tsx

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,26 @@ import * as React from 'react';
22
import { AnchorHTMLAttributes } from 'react';
33
import { sanitizeFieldRestProps } from './sanitizeFieldRestProps';
44
import { Typography, Link } from '@mui/material';
5+
import {
6+
ComponentsOverrides,
7+
styled,
8+
useThemeProps,
9+
} from '@mui/material/styles';
510
import { useFieldValue, useTranslate } from 'ra-core';
11+
612
import { FieldProps } from './types';
713
import { genericMemo } from './genericMemo';
814

915
const UrlFieldImpl = <
1016
RecordType extends Record<string, any> = Record<string, any>,
1117
>(
12-
props: UrlFieldProps<RecordType>
18+
inProps: UrlFieldProps<RecordType>
1319
) => {
20+
const props = useThemeProps({
21+
props: inProps,
22+
name: PREFIX,
23+
});
24+
1425
const { className, emptyText, content, ...rest } = props;
1526
const value = useFieldValue(props);
1627
const translate = useTranslate();
@@ -29,15 +40,15 @@ const UrlFieldImpl = <
2940
}
3041

3142
return (
32-
<Link
43+
<StyledLink
3344
className={className}
3445
href={value}
3546
onClick={stopPropagation}
3647
variant="body2"
3748
{...sanitizeFieldRestProps(rest)}
3849
>
3950
{content ?? value}
40-
</Link>
51+
</StyledLink>
4152
);
4253
};
4354
UrlFieldImpl.displayName = 'UrlFieldImpl';
@@ -53,3 +64,29 @@ export interface UrlFieldProps<
5364

5465
// useful to prevent click bubbling in a Datagrid with rowClick
5566
const stopPropagation = e => e.stopPropagation();
67+
68+
const PREFIX = 'RaUrlField';
69+
70+
const StyledLink = styled(Link, {
71+
name: PREFIX,
72+
overridesResolver: (props, styles) => styles.root,
73+
})({});
74+
75+
declare module '@mui/material/styles' {
76+
interface ComponentNameToClassKey {
77+
[PREFIX]: 'root';
78+
}
79+
80+
interface ComponentsPropsList {
81+
[PREFIX]: Partial<UrlFieldProps>;
82+
}
83+
84+
interface Components {
85+
[PREFIX]?: {
86+
defaultProps?: ComponentsPropsList[typeof PREFIX];
87+
styleOverrides?: ComponentsOverrides<
88+
Omit<Theme, 'components'>
89+
>[typeof PREFIX];
90+
};
91+
}
92+
}

0 commit comments

Comments
 (0)