Skip to content

Commit

Permalink
Merge pull request #25 from IBM/feature/select-text-when-number-cleared
Browse files Browse the repository at this point in the history
Fix UX for "cleared" numbers
  • Loading branch information
vankeisb authored Apr 25, 2024
2 parents e3beec4 + bc9b3a0 commit e583757
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
35 changes: 29 additions & 6 deletions json-form/src/JsonEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ import {
nothing,
Port,
Sub,
Task,
Tuple,
updatePiped,
} from 'tea-cup-core';
import { DevTools, Program } from 'react-tea-cup';
import { contextMenuMsg, Msg, setJsonStr, setStrictModeMsg } from './Msg';
import { Program } from 'react-tea-cup';
import { contextMenuMsg, Msg, noOp, setJsonStr, setStrictModeMsg } from './Msg';
import {
computeAll,
CustomRendererModel,
Expand Down Expand Up @@ -199,6 +200,21 @@ function withOutValueChanged(
];
}

function selectTextCmd(path: JsPath): Cmd<Msg> {
const selectTask: Task<Error, string> = Task.fromLambda(() => {
const inputId = `input-${path.format('_')}`;
const input = document.getElementById(inputId);
if (!input) {
throw new Error(`input not found for path ${path.format()}`);
}
if (input instanceof HTMLInputElement) {
input.select();
}
return 'ok';
});
return Task.attempt(selectTask, () => noOp);
}

export function update(
msg: Msg,
model: Model,
Expand All @@ -207,11 +223,18 @@ export function update(
switch (msg.tag) {
case 'delete-property':
return withOutValueChanged(model, actionDeleteValue(model, msg.path));
case 'update-property':
return withOutValueChanged(
case 'update-property': {
const mac = updatePiped(
model,
actionUpdateValue(model, msg.path, msg.value),
(model) => actionUpdateValue(model, msg.path, msg.value),
(model) => {
const cmd: Cmd<Msg> =
msg.selectText === true ? selectTextCmd(msg.path) : Cmd.none();
return [model, cmd];
},
);
return withOutValueChanged(model, mac);
}
case 'add-property-clicked':
return noOut(actionAddPropertyClicked(model, msg.path));
case 'new-property-name-changed':
Expand Down Expand Up @@ -437,7 +460,7 @@ export function JsonEditor(props: JsonEditorProps): React.ReactElement {
return [maco[0], maco[1]];
}}
subscriptions={subscriptions}
devTools={DevTools.init<Model, Msg>(window)}
// devTools={DevTools.init<Model, Msg>(window)}
/>
);
}
1 change: 1 addition & 0 deletions json-form/src/Msg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export interface DeleteProperty extends HasPath {
export interface UpdateProperty extends HasPath {
tag: 'update-property';
value: JsonValue;
selectText?: boolean;
}

export interface AddPropertyClicked extends HasPath {
Expand Down
10 changes: 9 additions & 1 deletion json-form/src/renderer/Renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -397,11 +397,13 @@ function ViewArray(p: ViewValueProps<JvArray>): React.ReactElement {
function dispatchUpdateProperty(
p: ViewValueProps<JsonValue>,
newValue: JsonValue,
selectText?: boolean,
) {
p.dispatch({
tag: 'update-property',
path: p.path,
value: newValue,
selectText,
});
}

Expand All @@ -419,10 +421,16 @@ function ViewNumber(p: ViewValueProps<JvNumber>): React.ReactElement {
invalid={isInvalid(p)}
onChange={(evt) => {
let newValue = parseFloat(evt.target.value);
let selectText = false;
if (isNaN(newValue)) {
newValue = 0;
selectText = true;
}
dispatchUpdateProperty(p, { tag: 'jv-number', value: newValue });
dispatchUpdateProperty(
p,
{ tag: 'jv-number', value: newValue },
selectText,
);
}}
/>
);
Expand Down

0 comments on commit e583757

Please sign in to comment.