Skip to content
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

Add a picture uploader to picture-card-editor #18695

Merged
merged 9 commits into from
May 29, 2024

Conversation

karwosts
Copy link
Contributor

@karwosts karwosts commented Nov 18, 2023

Proposed change

Add a picture-upload form to the picture-card editor. I think this makes working with images much more accessible, otherwise it can be a source of a lot of confusion how to add pictures to the server and figure out the path.

Maybe this should be wrapped up in some kind of image-selector element that can be reused?

That would allow for either entering an external URL, uploading, or maybe opening a file picker and allowing to select from the list of current uploaded images either in /image/ or /www/?

image-picker

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New feature (thank you!)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Example configuration

Additional information

Checklist

  • The code change is tested and works locally.
  • There is no commented out code in this PR.
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

Summary by CodeRabbit

  • New Features

    • Introduced an image selector component allowing users to upload or specify an image via URL.
    • Added new image-related options in various card editors for enhanced customization.
  • Style

    • Updated button styles to better display warning buttons with a distinct color.
  • Translations

    • Added new English translations for image selection, upload, and URL input options.

@bramkragten
Copy link
Member

Oh I do like this!

Maybe this should be wrapped up in some kind of image-selector element that can be reused?

Yes, that would be a good idea, similar to the file selector

That would allow for either entering an external URL, uploading, or maybe opening a file picker and allowing to select from the list of current uploaded images either in /image/ or /www/?

That also sounds good, but should be options on the selector then probably? The last option would need backend support first. (We do need something similar for uploaded pictures though probably, as there is no way to manage or reuse already uploaded images)

@karwosts
Copy link
Contributor Author

If we had image-selector, what do you think of how the current UI looks?

OK to just have a <ha-picture-upload> stacked on top of a textbox for the path. Or is that take up too much space? Maybe have to click a button to show the uploader?

Image browser/picker could come later if/when that ever gets implemented.

@matthiasdebaat
Copy link
Collaborator

I like this one too! I'm seeing 3 options to select an image, correct me if I'm wrong:

  • Select a local file using image URL
  • Upload image from device, that converts to a local image URL
  • Upload image from web, that converts to a local image URL

In my opinion, the input-select with image URL isn't user friendly and pretty advanced. You have to know the path to a file, that requires access to your system. Can we drop this one? That means there should be an option to select an image from the system, like we're doing with the Media panel.

@karwosts
Copy link
Contributor Author

I like this one too! I'm seeing 3 options to select an image, correct me if I'm wrong:

  • Select a local file using image URL

The URL input can also be remote URL on the web, it doesn't have to be local.

@bramkragten
Copy link
Member

I think I would hide the input by default, but allow the user to switch to it with a button

@matthiasdebaat
Copy link
Collaborator

I like this one too! I'm seeing 3 options to select an image, correct me if I'm wrong:

  • Select a local file using image URL
  • Upload image from device, that converts to a local image URL
  • Upload image from web, that converts to a local image URL

The URL input can also be remote URL on the web, it doesn't have to be local.

That is the third option in my mind. So it would be:

  • Upload image from web, that converts web URL to a local image URL

@matthiasdebaat
Copy link
Collaborator

I think I would hide the input by default, but allow the user to switch to it with a button

I agree. There should be a way to select between selecting a local (already uploaded) file, upload new image or upload image from the web. This last option only have an input. The other doesn't need one in my mind.

@karwosts
Copy link
Contributor Author

I think I would hide the input by default

Can you clarify, what is "the input" you're referring to hide, the text selector, or the picture-upload?

I was kind of thinking to have the button toggle the larger upload element, though I'm wondering if Matthias is suggesting the opposite; to hide the URL. I'm not sure if that makes sense to me yet.

This is kind of what I was going to go for:

picture-card-2

Matthias, you seem to also be suggesting that remote URLs should be replaced with "upload to a local image from web", but I think people might not necessarily want to upload remote URLs. The behavior now is just to show the live image from the remote domain. Maybe that is desired, in case the remote image is dynamic, or user just don't want to store it on his server?

@bramkragten
Copy link
Member

@matthiasdebaat can you check above questions?

@matthiasdebaat
Copy link
Collaborator

Thanks for your feedback. Based on this I've got something like this in my mind. More details can be found in the Figma design.

CleanShot 2023-12-07 at 10 59 49@2x

My suggestions are:

  • Let the user to choose between upload or URL. When the user chooses upload, the URL input is hidden and vice versa.
  • Add a delete button when an image is uploaded.
  • URL is as we currently have it.

@karwosts
Copy link
Contributor Author

karwosts commented Dec 7, 2023

Updated from feedback.

I've for now dropped the crop feature from this as I don't really like how it works. When crop is enabled you are forced to go through the crop dialog, and there's no easy way to just use the entire image unaltered (you would have to try to drag the crop bubbles to exactly the corner of your image, and probably get it off by 1 pixel), so that seems potentially frustrating.

A better flow might be to have first the entire picture uploaded without crop, and then have a CROP button on the line below the picture to run it through the crop flow? Maybe that could be a different change.

image-selector-3

@bramkragten
Copy link
Member

bramkragten commented Feb 28, 2024

We should add the camera entity and image entity to that radio list, as that will override the image (the entity will too btw on a picture-entity card, if it is a camera entity... 😓 )

)}
>
</ha-button>
${this.canDelete
Copy link
Member

Choose a reason for hiding this comment

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

I don't think it is clear to a user what the delete button does, it looks like it does the same as the change picture button, but it deletes it from the server...

We should have a picker of current uplloaded files sometime 😓

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would offer to remove the delete button, but it was just added on Matthias's request 😕

The other weird thing about delete is that it's in a cancellable dialog. If you delete the picture, but then cancel the change, the picture becomes a broken link to a no longer existing image.

I assumed that more picture management functionality would maybe come someday in the future, but I felt maybe this was ok to go ahead without that, as that felt like might be a pretty big project with new APIs.

Copy link
Member

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.

For the sake of not blocking this PR, I'm okay if the delete button is removed.

We should have a picker of current uplloaded files sometime 😓

I agree

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For the sake of not blocking this PR, I'm okay if the delete button is removed.

It's gone.

@bramkragten bramkragten self-assigned this May 29, 2024
Copy link
Contributor

coderabbitai bot commented May 29, 2024

Walkthrough

Walkthrough

The updates introduce a new image selector component (ha-selector-image) and enhance image handling across various parts of the application. Changes include adding style support, updating image selection mechanisms in different card editors, and modifying image-related data handling functions. Additionally, new translations and styles for warning buttons have been incorporated.

Changes

Files/Groups Change Summary
src/components/ha-picture-upload.ts Added haStyle import, wrapped ha-button in a div, modified styles method to include haStyle.
src/components/ha-selector/ha-selector-image.ts Introduced ha-selector-image component with image upload and URL selection features.
src/components/ha-selector/ha-selector.ts Added ha-selector-image to LOAD_ELEMENTS for image selection functionality.
src/data/image_upload.ts Added URL prefix constant, function to extract ID from URL, renamed parameter in deleteImage function.
src/data/selector.ts Introduced ImageSelector interface in Selector type.
src/panels/lovelace/editor/config-elements/... Updated image property selector from text to image in various card editors (picture, entity, glance).
src/resources/styles.ts Added new style rule for warning buttons, setting primary theme color to error color.
src/translations/en.json Added new "image" section with keys for selecting, uploading, and specifying image URLs.

Sequence Diagram(s) (Beta)

sequenceDiagram
    participant User
    participant HaImageSelector
    participant HaPictureUpload
    participant ImageUploadAPI

    User->>HaImageSelector: Select image option
    HaImageSelector->>User: Show upload/URL input
    User->>HaImageSelector: Upload image / Enter URL
    HaImageSelector->>HaPictureUpload: Upload image
    HaPictureUpload->>ImageUploadAPI: Send image data
    ImageUploadAPI->>HaPictureUpload: Confirm upload
    HaPictureUpload->>HaImageSelector: Return image URL
    HaImageSelector->>User: Display selected image
Loading

Recent Review Details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE

Commits Files that changed from the base of the PR and between 5fab196 and aaaf3af.
Files selected for processing (10)
  • src/components/ha-picture-upload.ts (3 hunks)
  • src/components/ha-selector/ha-selector-image.ts (1 hunks)
  • src/components/ha-selector/ha-selector.ts (1 hunks)
  • src/data/image_upload.ts (2 hunks)
  • src/data/selector.ts (2 hunks)
  • src/panels/lovelace/editor/config-elements/hui-picture-card-editor.ts (1 hunks)
  • src/panels/lovelace/editor/config-elements/hui-picture-entity-card-editor.ts (1 hunks)
  • src/panels/lovelace/editor/config-elements/hui-picture-glance-card-editor.ts (1 hunks)
  • src/resources/styles.ts (1 hunks)
  • src/translations/en.json (2 hunks)
Additional Context Used
Biome (58)
src/components/ha-picture-upload.ts (5)

136-136: Unexpected any. Specify a different type.


1-2: Some named imports are only used as types.


7-11: Some named imports are only used as types.


11-12: All these imports are only used as types.


110-110: Use Number.NaN instead of the equivalent global.

src/components/ha-selector/ha-selector-image.ts (4)

18-18: Unexpected any. Specify a different type.


1-1: Some named imports are only used as types.


3-4: All these imports are only used as types.


4-5: All these imports are only used as types.

src/components/ha-selector/ha-selector.ts (5)

72-72: Unexpected any. Specify a different type.


81-81: Unexpected any. Specify a different type.


87-87: Unexpected any. Specify a different type.


1-1: Some named imports are only used as types.


4-9: Some named imports are only used as types.

src/data/image_upload.ts (4)

18-18: This variable implicitly has the any type.


32-32: This type annotation is trivially inferred from its initialization.


58-60: This else clause can be omitted because previous branches break early.


1-1: All these imports are only used as types.

src/data/selector.ts (20)

339-339: Unexpected any. Specify a different type.


363-363: Unexpected any. Specify a different type.


470-484: Prefer for...of instead of forEach.


486-499: Prefer for...of instead of forEach.


501-512: Prefer for...of instead of forEach.


525-539: Prefer for...of instead of forEach.


553-566: Prefer for...of instead of forEach.


567-578: Prefer for...of instead of forEach.


590-601: Prefer for...of instead of forEach.


688-688: Forbidden non-null assertion.


783-785: Forbidden non-null assertion.


811-813: Forbidden non-null assertion.


4-5: All these imports are only used as types.


5-6: All these imports are only used as types.


6-10: Some named imports are only used as types.


10-14: All these imports are only used as types.


14-15: All these imports are only used as types.


91-91: Don't use '{}' as a type.


103-103: Don't use '{}' as a type.


108-108: Don't use '{}' as a type.

src/panels/lovelace/editor/config-elements/hui-picture-card-editor.ts (8)

78-78: Forbidden non-null assertion.


80-80: Forbidden non-null assertion.


85-85: Forbidden non-null assertion.


88-88: Forbidden non-null assertion.


4-5: All these imports are only used as types.


6-7: All these imports are only used as types.


7-8: All these imports are only used as types.


9-10: All these imports are only used as types.

src/panels/lovelace/editor/config-elements/hui-picture-entity-card-editor.ts (4)

120-120: Forbidden non-null assertion.


122-122: Forbidden non-null assertion.


126-126: Forbidden non-null assertion.


1-1: Some named imports are only used as types.

src/panels/lovelace/editor/config-elements/hui-picture-glance-card-editor.ts (8)

82-82: Forbidden non-null assertion.


86-86: Forbidden non-null assertion.


122-122: Change to an optional chain.


135-135: Forbidden non-null assertion.


137-137: Forbidden non-null assertion.


141-141: Forbidden non-null assertion.


145-145: Forbidden non-null assertion.


1-1: Some named imports are only used as types.

Additional comments not posted (10)
src/data/image_upload.ts (2)

11-11: The addition of URL_PREFIX is a good practice for centralizing API endpoints, making them easier to manage and modify.


78-78: Renaming the parameter from media_id to image_id in the deleteImage function improves clarity, aligning the terminology with the rest of the image handling functions.

src/panels/lovelace/editor/config-elements/hui-picture-card-editor.ts (1)

27-27: Updating the schema to use the new image selector aligns with the PR's goal to enhance image handling. This change facilitates a more intuitive UI for image selection.

src/panels/lovelace/editor/config-elements/hui-picture-entity-card-editor.ts (1)

35-35: The addition of the image selector to the schema is consistent with the PR's enhancements to image handling, providing a unified approach across different card editors.

src/components/ha-picture-upload.ts (1)

Line range hint 5-174: The updates to HaPictureUpload include the integration of haStyle and improvements to the UI for handling image uploads and changes. The implementation of the image change and upload functionalities is clear and aligns with the PR's objectives to enhance image management.

src/panels/lovelace/editor/config-elements/hui-picture-glance-card-editor.ts (1)

38-38: The update to include the image selector in the schema for the picture glance card editor is consistent with the changes across other card editors, enhancing the user's ability to manage images directly within the UI.

src/components/ha-selector/ha-selector.ts (1)

35-35: Addition of image selector type to support new image selection functionality.

This change integrates the new ha-selector-image component into the selector system, enabling image selection capabilities across the platform.

src/resources/styles.ts (1)

85-85: Styling for warning buttons to use error color.

This change ensures that warning buttons are visually consistent with error indicators, enhancing UI consistency and user experience.

src/data/selector.ts (1)

43-43: Introduction of ImageSelector type to support new image selection functionality.

This addition is crucial for enabling the new image selector functionality within the system, allowing for a structured way to handle image selections.

src/translations/en.json (1)

380-384: Translations for the new image handling features are clear and well-structured.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@bramkragten
Copy link
Member

I would still really like to combine the image entity/camera entity/upload image/image url options for improved clarity that they can not be used together. But this is already a nice addition that we should merge. Thanks @karwosts!

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 4

Outside diff range comments (1)
src/components/ha-selector/ha-selector.ts (1)

Line range hint 72-72: Specify explicit types instead of using any.

- @property() public value?: any;
+ @property() public value?: string | number | YourCustomType; // Adjust the type according to expected values

- @property() public placeholder?: any;
+ @property() public placeholder?: string; // Adjust the type if necessary

- @property({ attribute: false }) public context?: Record<string, any>;
+ @property({ attribute: false }) public context?: Record<string, YourSpecificType>; // Define 'YourSpecificType' as needed

Using any can lead to less predictable code and more difficult debugging. Specifying explicit types improves type safety and code clarity.

Also applies to: 81-81, 87-87

Comment on lines +420 to +424
"image": {
"select_image": "Select image",
"upload": "Upload picture",
"url": "Local path or web URL"
},
Copy link
Contributor

Choose a reason for hiding this comment

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

This section appears to be a duplicate of the earlier translation keys for image handling. Please verify and consider removing if it's unintentional.

Comment on lines +260 to +263
export interface ImageSelector {
// eslint-disable-next-line @typescript-eslint/ban-types
image: {} | null;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Define a more specific type for ImageSelector.

- export interface ImageSelector {
-   image: {} | null;
- }
+ export interface ImageSelector {
+   image: {
+     url?: string;
+     altText?: string;
+   } | null;
+ }

Using {} as a type is generally discouraged as it is too broad and does not provide useful type checking. Defining a more specific type will enhance maintainability and clarity of the code.


Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
export interface ImageSelector {
// eslint-disable-next-line @typescript-eslint/ban-types
image: {} | null;
}
export interface ImageSelector {
image: {
url?: string;
altText?: string;
} | null;
}

Comment on lines +17 to +26
export const getIdFromUrl = (url: string): string | undefined => {
let id;
if (url.startsWith(URL_PREFIX)) {
id = url.substring(URL_PREFIX.length);
const idx = id.indexOf("/");
if (idx >= 0) {
id = id.substring(0, idx);
}
}
return id;
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider adding explicit type annotations to enhance code clarity and maintainability. For instance, id should be explicitly typed as string | undefined.

- let id;
+ let id: string | undefined;

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
export const getIdFromUrl = (url: string): string | undefined => {
let id;
if (url.startsWith(URL_PREFIX)) {
id = url.substring(URL_PREFIX.length);
const idx = id.indexOf("/");
if (idx >= 0) {
id = id.substring(0, idx);
}
}
return id;
export const getIdFromUrl = (url: string): string | undefined => {
let id: string | undefined;
if (url.startsWith(URL_PREFIX)) {
id = url.substring(URL_PREFIX.length);
const idx = id.indexOf("/");
if (idx >= 0) {
id = id.substring(0, idx);
}
}
return id;

Comment on lines +14 to +143
protected render() {
return html`
<div>
<label>
${this.hass.localize("ui.components.selectors.image.select_image")}
<ha-formfield
.label=${this.hass.localize("ui.components.selectors.image.upload")}
>
<ha-radio
name="mode"
value="upload"
.checked=${this.showUpload}
@change=${this._radioGroupPicked}
></ha-radio>
</ha-formfield>
<ha-formfield
.label=${this.hass.localize("ui.components.selectors.image.url")}
>
<ha-radio
name="mode"
value="url"
.checked=${!this.showUpload}
@change=${this._radioGroupPicked}
></ha-radio>
</ha-formfield>
</label>
${!this.showUpload
? html`
<ha-textfield
.name=${this.name}
.value=${this.value || ""}
.placeholder=${this.placeholder || ""}
.helper=${this.helper}
helperPersistent
.disabled=${this.disabled}
@input=${this._handleChange}
.label=${this.label || ""}
.required=${this.required}
></ha-textfield>
`
: html`
<ha-picture-upload
.hass=${this.hass}
.value=${this.value?.startsWith(URL_PREFIX) ? this.value : null}
@change=${this._pictureChanged}
></ha-picture-upload>
`}
</div>
`;
}

private _radioGroupPicked(ev): void {
this.showUpload = ev.target.value === "upload";
}

private _pictureChanged(ev) {
const value = (ev.target as HaPictureUpload).value;

fireEvent(this, "value-changed", { value: value ?? undefined });
}

private _handleChange(ev) {
let value = ev.target.value;
if (this.value === value) {
return;
}
if (value === "" && !this.required) {
value = undefined;
}

fireEvent(this, "value-changed", { value });
}

static get styles(): CSSResultGroup {
return css`
:host {
display: block;
position: relative;
}
div {
display: flex;
flex-direction: column;
}
label {
display: flex;
flex-direction: column;
}
ha-textarea,
ha-textfield {
width: 100%;
}
`;
}
}

declare global {
interface HTMLElementTagNameMap {
"ha-selector-image": HaImageSelector;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

The new HaImageSelector component is well-implemented, providing a flexible UI component for image selection. It supports both upload and URL modes, which aligns with the PR's objectives. Consider specifying a more precise type than any for the value property to enhance type safety.

- @property() public value?: any;
+ @property() public value?: string | null; // Adjust as appropriate based on expected types

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
@customElement("ha-selector-image")
export class HaImageSelector extends LitElement {
@property({ attribute: false }) public hass!: HomeAssistant;
@property() public value?: any;
@property() public name?: string;
@property() public label?: string;
@property() public placeholder?: string;
@property() public helper?: string;
@property({ attribute: false }) public selector!: ImageSelector;
@property({ type: Boolean }) public disabled = false;
@property({ type: Boolean }) public required = true;
@state() private showUpload = false;
protected firstUpdated(changedProps): void {
super.firstUpdated(changedProps);
if (!this.value || this.value.startsWith(URL_PREFIX)) {
this.showUpload = true;
}
}
protected render() {
return html`
<div>
<label>
${this.hass.localize("ui.components.selectors.image.select_image")}
<ha-formfield
.label=${this.hass.localize("ui.components.selectors.image.upload")}
>
<ha-radio
name="mode"
value="upload"
.checked=${this.showUpload}
@change=${this._radioGroupPicked}
></ha-radio>
</ha-formfield>
<ha-formfield
.label=${this.hass.localize("ui.components.selectors.image.url")}
>
<ha-radio
name="mode"
value="url"
.checked=${!this.showUpload}
@change=${this._radioGroupPicked}
></ha-radio>
</ha-formfield>
</label>
${!this.showUpload
? html`
<ha-textfield
.name=${this.name}
.value=${this.value || ""}
.placeholder=${this.placeholder || ""}
.helper=${this.helper}
helperPersistent
.disabled=${this.disabled}
@input=${this._handleChange}
.label=${this.label || ""}
.required=${this.required}
></ha-textfield>
`
: html`
<ha-picture-upload
.hass=${this.hass}
.value=${this.value?.startsWith(URL_PREFIX) ? this.value : null}
@change=${this._pictureChanged}
></ha-picture-upload>
`}
</div>
`;
}
private _radioGroupPicked(ev): void {
this.showUpload = ev.target.value === "upload";
}
private _pictureChanged(ev) {
const value = (ev.target as HaPictureUpload).value;
fireEvent(this, "value-changed", { value: value ?? undefined });
}
private _handleChange(ev) {
let value = ev.target.value;
if (this.value === value) {
return;
}
if (value === "" && !this.required) {
value = undefined;
}
fireEvent(this, "value-changed", { value });
}
static get styles(): CSSResultGroup {
return css`
:host {
display: block;
position: relative;
}
div {
display: flex;
flex-direction: column;
}
label {
display: flex;
flex-direction: column;
}
ha-textarea,
ha-textfield {
width: 100%;
}
`;
}
}
declare global {
interface HTMLElementTagNameMap {
"ha-selector-image": HaImageSelector;
}
}
@property() public value?: string | null;

@bramkragten bramkragten merged commit 064c51f into home-assistant:dev May 29, 2024
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants