Skip to content

Add readonly field #3798

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

Add readonly field #3798

wants to merge 4 commits into from

Conversation

piotrgulbis
Copy link
Contributor

@piotrgulbis piotrgulbis commented Apr 22, 2025

Description

Until now, we have supported read-only fields using a readOnly prop on the field component. This has some drawbacks:

Comparison

Approach Advantages Disadvantages
readOnly Prop on Field - Simple from a user perspective (user = developer using the component)
- Styling is already correct
- Adds unnecessary complexity to the form
- Form state contains values that are not editable
Dedicated ReadOnlyField - No uneditable fields in the form state
- No need for complex fields (e.g., Selects that load data asynchronously)
- No filtering needed in onSubmit
- Harder to build "dynamic" forms (where fields become read-only client-side)
- Slightly more effort to use

Plan

Therefore, we want to additionally offer a ReadOnlyField component. This will support multiple types and render them correctly (e.g., String / Number / Boolean / Date / File / Block — see table below). The values will be passed directly to the field, not managed via the form state.

For block content, the read-only mode should simply show the PreviewContent.

We still plan to continue supporting the readOnly prop on regular fields to make it easy to build dynamic forms.

Acceptance criteria

Screenshots/screencasts

Boolean Readonly Field

image

Date Readonly Field

image

File Readonly Field

image

Files Readonly Field

image

Select Readonly Field

image

MultiSelect Readonly Field

image

Number Readonly Field

image

Rich Text Readonly Field

image

Text Readonly Field

image

Text Area Readonly Field

image

Null and Undefined Value Readonly Fields

image

Open TODOs/questions

  • Add changeset
  • Add Block Readonly Field, probably needs to be in a separate PR due to missing access to BlockPreviewContent.

Further information

@piotrgulbis piotrgulbis force-pushed the feat/read-only-field branch from 7775251 to 24ed73a Compare May 19, 2025 12:56
@piotrgulbis piotrgulbis force-pushed the feat/read-only-field branch from 24ed73a to 70acc04 Compare June 2, 2025 07:47
@piotrgulbis piotrgulbis marked this pull request as ready for review June 2, 2025 09:09
@auto-assign auto-assign bot requested a review from johnnyomair June 2, 2025 09:09
@nsams
Copy link
Member

nsams commented Jun 2, 2025

I'd like to challenge the need for ReadOnlyField

  • inferring the value doesn't work always (Text vs. TextField, Select vs. Switch, all nullalbe fields), so you have to specify type anyway in many cases
  • we also don't have it for normal (non-readonly) fields
  • additional settings (eg. currency or endAdornment for ReadOnlyNumberField) won't be typed

@johnnyomair johnnyomair requested review from nsams and jamesricky June 3, 2025 14:55
};

const Wrapper = createComponentSlot("div")({
componentName: "ReadOnlyBlockField",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jamesricky these components are themable but not exported in src/index.ts. Does this work?

};

const Label = createComponentSlot(FormLabel)({
componentName: "ReadOnlySwitchField",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
componentName: "ReadOnlySwitchField",
componentName: "ReadOnlyFieldLabel",

Comment on lines +17 to +23
color: ${theme.palette.grey[900]};
font-family: ${theme.typography.fontFamily};
font-size: 16px;
font-style: normal;
font-weight: 600;
line-height: 20px;
letter-spacing: 0px;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this a standard typography?

id="readOnlyField.fileField.size"
defaultMessage="{size}"
values={{
size: `${(file.size / 1024).toFixed(2)} MB`,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a PrettyBytes component for this.

Comment on lines +13 to +21
<Wrapper className={className}>
{label && <ReadOnlyFieldLabel label={label} />}
{value && (
<Box>
<TextContent>{value}</TextContent>
<Lock />
</Box>
)}
</Wrapper>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most for the readonly fields are pretty similar. What do you think about creating a base component that supports label and adornments that is then used by the components?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could also use FieldContainer

);

const TextContent = createComponentSlot("span")({
componentName: "ReadOnlyMultiSelectField",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check all component names if they're correct.

"@comet/admin": minor
---

Add Read Only Field Component
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Add Read Only Field Component
Add `ReadOnlyField` component

value?: Date | string | undefined;
className?: string;
}> = ({ label, value, className }) => {
const date = value instanceof Date ? value : value ? new Date(value) : undefined;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For dates without time we use string (e.g., 2025-03-06). This field should work with strings as well.

<Box>
<InnerBox>
<Calendar />
{date && date.toLocaleDateString()}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be possible to add multiple support to ReadOnlySelectField? We don't have a MultiSelectField component.

@johnnyomair
Copy link
Collaborator

There are some more Field components, e.g. CheckboxListField. Do we plan to add readonly versions for these as well? https://github.com/vivid-planet/comet/tree/main/packages/admin/admin/src/form/fields

@johnnyomair johnnyomair changed the title feat: add read only field Add readonly field Jun 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants