From fc52fee781586fcd8507d2095fd6ff4471c93dca Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sat, 13 Jul 2024 22:09:08 -0400 Subject: [PATCH] Remove save button --- app/components/Input.tsx | 16 ++- app/components/InputSelect.tsx | 9 +- app/models/DataAttribute.ts | 11 -- app/scenes/Document/components/Properties.tsx | 114 +++++++++++------- app/utils/DataAttributesHelper.tsx | 2 +- server/commands/documentUpdater.ts | 2 +- shared/i18n/locales/en_US/translation.json | 1 + 7 files changed, 91 insertions(+), 64 deletions(-) diff --git a/app/components/Input.tsx b/app/components/Input.tsx index 3def4e5ac3..c49a8ce657 100644 --- a/app/components/Input.tsx +++ b/app/components/Input.tsx @@ -143,8 +143,12 @@ export interface Props onRequestSubmit?: ( ev: React.KeyboardEvent ) => unknown; - onFocus?: (ev: React.SyntheticEvent) => unknown; - onBlur?: (ev: React.SyntheticEvent) => unknown; + onFocus?: ( + ev: React.SyntheticEvent + ) => unknown; + onBlur?: ( + ev: React.SyntheticEvent + ) => unknown; } function Input( @@ -154,7 +158,9 @@ function Input( const internalRef = React.useRef(); const [focused, setFocused] = React.useState(false); - const handleBlur = (ev: React.SyntheticEvent) => { + const handleBlur = ( + ev: React.SyntheticEvent + ) => { setFocused(false); if (props.onBlur) { @@ -162,7 +168,9 @@ function Input( } }; - const handleFocus = (ev: React.SyntheticEvent) => { + const handleFocus = ( + ev: React.SyntheticEvent + ) => { setFocused(true); if (props.onFocus) { diff --git a/app/components/InputSelect.tsx b/app/components/InputSelect.tsx index 0125127772..952ac668ff 100644 --- a/app/components/InputSelect.tsx +++ b/app/components/InputSelect.tsx @@ -55,6 +55,8 @@ export type Props = { * The Modal will take care of preventing body scroll behaviour. */ skipBodyScroll?: boolean; + autoFocus?: boolean; + placeholder?: string; }; export interface InputSelectRef { @@ -85,6 +87,8 @@ const InputSelect = (props: Props, ref: React.RefObject) => { icon, nude, skipBodyScroll, + autoFocus, + placeholder, ...rest } = props; @@ -214,6 +218,7 @@ const InputSelect = (props: Props, ref: React.RefObject) => { neutral disclosure className={className} + autoFocus={autoFocus} icon={icon} $nude={nude} {...buttonProps} @@ -221,7 +226,9 @@ const InputSelect = (props: Props, ref: React.RefObject) => { {option ? ( labelForOption(option) ) : ( - Select a {ariaLabel.toLowerCase()} + + {placeholder ?? `Select a ${ariaLabel.toLowerCase()}`} + )} )} diff --git a/app/models/DataAttribute.ts b/app/models/DataAttribute.ts index 12f3d578a6..a5b43d1b89 100644 --- a/app/models/DataAttribute.ts +++ b/app/models/DataAttribute.ts @@ -47,17 +47,6 @@ class DataAttribute extends ParanoidModel { /** The user ID that created this data attribute. */ createdById: string; - - /** The default value to use when creating a new property. */ - get defaultValue() { - switch (this.dataType) { - case DataAttributeDataType.Boolean: - return false; - case DataAttributeDataType.List: - return this.options?.options?.[0]?.value; - } - return undefined; - } } export default DataAttribute; diff --git a/app/scenes/Document/components/Properties.tsx b/app/scenes/Document/components/Properties.tsx index 08cfef968e..46bc05ca3b 100644 --- a/app/scenes/Document/components/Properties.tsx +++ b/app/scenes/Document/components/Properties.tsx @@ -5,14 +5,15 @@ import * as React from "react"; import { useTranslation } from "react-i18next"; import styled from "styled-components"; import { Primitive } from "utility-types"; +import Flex from "@shared/components/Flex"; import { DataAttributeDataType, DocumentDataAttribute, } from "@shared/models/types"; +import { s } from "@shared/styles"; import { isUrl } from "@shared/utils/urls"; import Document from "~/models/Document"; import { Inner } from "~/components/Button"; -import ButtonSmall from "~/components/ButtonSmall"; import Input from "~/components/Input"; import InputSelect from "~/components/InputSelect"; import NudeButton from "~/components/NudeButton"; @@ -22,6 +23,8 @@ import useStores from "~/hooks/useStores"; import { DataAttributesHelper } from "~/utils/DataAttributesHelper"; import { Feature, FeatureFlags } from "~/utils/FeatureFlags"; +const PropertyHeight = 26; + type Props = { document: Document; }; @@ -32,18 +35,15 @@ export type PropertiesRef = { export const Properties = observer( React.forwardRef(function Properties_({ document }: Props, ref) { - const { dataAttributes } = useStores(); const [draftAttribute, setDraftAttribute] = React.useState(null); const handleAddProperty = (dataAttributeId: string) => { - const definition = dataAttributes.get(dataAttributeId); - setDraftAttribute((state) => state ? null : { - value: definition?.defaultValue ?? "", + value: "", dataAttributeId, updatedAt: new Date().toISOString(), } @@ -54,12 +54,11 @@ export const Properties = observer( addProperty: handleAddProperty, })); - const handleSave = async () => { - if (draftAttribute) { - document.setDataAttribute( - draftAttribute.dataAttributeId, - draftAttribute.value - ); + const handleSave = async (dataAttribute: DocumentDataAttribute) => { + if (dataAttribute) { + const value = dataAttribute.value; + setDraftAttribute(null); + document.setDataAttribute(dataAttribute.dataAttributeId, value); await document.save(); } }; @@ -101,10 +100,12 @@ export const Properties = observer( }); }} onRemove={() => setDraftAttribute(null)} - onSubmit={() => { - setDraftAttribute(null); - void handleSave(); - }} + onSubmit={(value) => + handleSave({ + ...draftAttribute, + value, + }) + } isEditing /> )} @@ -123,7 +124,7 @@ const Property = observer(function Property_({ dataAttribute: DocumentDataAttribute; isEditing?: boolean; onChange: (value: Primitive) => void; - onSubmit: () => void; + onSubmit: (value: Primitive) => void; onRemove: () => void; }) { const { t } = useTranslation(); @@ -156,9 +157,9 @@ const Property = observer(function Property_({ value ); - const handleSubmit = () => { + const handleSubmit = (value: Primitive) => { setIsEditing(false); - onSubmit(); + onSubmit(value); }; const inputId = `data-attribute-${dataAttribute.dataAttributeId}`; @@ -167,6 +168,7 @@ const Property = observer(function Property_({ {definition?.dataType === DataAttributeDataType.List ? ( ({ @@ -174,35 +176,49 @@ const Property = observer(function Property_({ value: option.value, })) ?? [] } - value={String(dataAttribute?.value) ?? ""} - onChange={onChange} + value={String(dataAttribute.value) ?? ""} + onChange={(val) => { + onChange(val); + handleSubmit(val); + }} /> ) : definition?.dataType === DataAttributeDataType.Boolean ? ( { + onChange(val); + handleSubmit(val); + }} /> ) : ( handleSubmit(event.currentTarget.value)} + onRequestSubmit={(event) => handleSubmit(event.currentTarget.value)} placeholder={definition.description ?? ""} - value={String(dataAttribute?.value) ?? ""} + value={String(dataAttribute.value) ?? ""} pattern={ definition ? DataAttributesHelper.getValidationRegex(definition)?.source : undefined } required - onChange={(event) => onChange(event.target.value)} + onChange={(event) => onChange(event.currentTarget.value)} autoFocus /> )} @@ -227,45 +243,49 @@ const Property = observer(function Property_({
{displayedInput ?? displayedValue} - {isEditing ? ( - - {t("Save")} - - ) : ( - <> + + {!isEditing && ( - setIsEditing(true)} - $isEditing={isEditing} - > + setIsEditing(true)}> - - - - - - - )} + )} + + + + + +
); }); +const Actions = styled(Flex)` + display: flex; + margin-left: 8px; +`; + const Label = styled.label` white-space: nowrap; `; -const HoverButton = styled(NudeButton)<{ $isEditing: boolean }>` +const HoverButton = styled(NudeButton)<{ $isEditing?: boolean }>` opacity: ${(props) => (props.$isEditing ? 1 : 0)}; + margin-left: -4px; + + &:hover, + &:focus { + color: ${s("text")}; + } `; const StyledInputSelect = styled(InputSelect)` padding: 0; margin: -4px; margin-right: 8px; - height: 26px; + height: ${PropertyHeight}px; ${Inner} { line-height: 14px; @@ -288,7 +308,7 @@ const List = styled.dl` display: flex; flex-flow: row wrap; margin-top: -1.4em; - margin-bottom: 2em; + margin-bottom: 1.6em; font-size: 14px; position: relative; `; @@ -298,7 +318,7 @@ const Dt = styled(Text)` clear: left; flex-basis: 20%; margin-bottom: 2px; - height: 26px; + height: ${PropertyHeight}px; svg { position: relative; @@ -312,6 +332,8 @@ const Dd = styled(Text)` display: flex; align-items: center; margin-bottom: 2px; + height: ${PropertyHeight}px; + gap: 4px; &:hover { ${HoverButton} { diff --git a/app/utils/DataAttributesHelper.tsx b/app/utils/DataAttributesHelper.tsx index eed5a18ea5..e9c51e6f31 100644 --- a/app/utils/DataAttributesHelper.tsx +++ b/app/utils/DataAttributesHelper.tsx @@ -66,7 +66,7 @@ export class DataAttributesHelper { public static getValidationRegex(dataAttribute: DataAttribute) { switch (dataAttribute.dataType) { case DataAttributeDataType.Number: - return /^[0-9]+$/; + return /^-?\d+(\.\d+)?$/; default: return undefined; } diff --git a/server/commands/documentUpdater.ts b/server/commands/documentUpdater.ts index 106b7aca89..3efb99b2b7 100644 --- a/server/commands/documentUpdater.ts +++ b/server/commands/documentUpdater.ts @@ -9,7 +9,7 @@ type Props = { user: User; /** The existing document */ document: Document; - /** TODO: Data attributes to apply to the document */ + /** Data attributes to apply to the document */ dataAttributes?: Omit[]; /** The new title */ title?: string; diff --git a/shared/i18n/locales/en_US/translation.json b/shared/i18n/locales/en_US/translation.json index 9e119fbc18..b19f3ff394 100644 --- a/shared/i18n/locales/en_US/translation.json +++ b/shared/i18n/locales/en_US/translation.json @@ -628,6 +628,7 @@ "Observing {{ userName }}": "Observing {{ userName }}", "Yes": "Yes", "No": "No", + "Select an option": "Select an option", "Backlinks": "Backlinks", "Close": "Close", "{{ teamName }} is using {{ appName }} to share documents, please login to continue.": "{{ teamName }} is using {{ appName }} to share documents, please login to continue.",