Suggest a display name from two fields
Sunil Pai’s Trellis experiment uses a name and company to suggest the next field. Let people check the suggestion before accepting it.
Sunil Pai · Trellis · Source post
Generate suggestions from entered data
An independent web example inspired by the original reference.
Enter a name and company, use the suggestion, then edit the display name.
Implementation steps
- Separate name and company fields from the field to autofill. In this sample, a display name suggestion is built from these two fields.
- Don't auto-commit suggestions. Place the content beside a “Use suggestion” button. Even if the inference is replaced with an API, leave the commit action to the user.
- Put the accepted value into a normal input so it stays freely editable.
Acceptance check
Suggestions appear once both fields are filled. The display name being edited doesn't change until the user presses “Use suggestion.”
View sample code
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Generate suggestions from entered data</title>
<style>
* { box-sizing: border-box; }
body {
margin: 0;
padding: 16px;
background: #23232a;
color: #f4f0ea;
font: 16px/1.6 system-ui, sans-serif;
}
label, output, strong, audio, progress { display: block; margin: 6px 0; }
input, select, button { max-width: 100%; font: inherit; }
input, select { padding: 4px 7px; }
button {
margin: 4px 0;
padding: 5px 12px;
cursor: pointer;
border: 1px solid #b5a399;
border-radius: 6px;
}
:focus-visible { outline: 3px solid #ffad73; outline-offset: 2px; }
audio { width: 100%; }
[hidden] { display: none !important; }
</style>
</head>
<body>
<label>Name <input id="name" autocomplete="name"></label>
<label>Company <input id="org" autocomplete="organization"></label>
<button id="use" disabled>Use suggestion</button>
<label>Display name <input id="display"></label>
<script>
const nameField = document.querySelector('#name');
const org = document.querySelector('#org');
const use = document.querySelector('#use');
function suggestion() {
return nameField.value.trim() + ' / ' + org.value.trim();
}
function refresh() {
use.disabled = !nameField.value.trim() || !org.value.trim();
use.textContent = use.disabled ? 'Use suggestion' : suggestion() + ' · use suggestion';
}
nameField.oninput = org.oninput = refresh;
use.onclick = () => { document.querySelector('#display').value = suggestion(); };
</script>
</body>
</html>Save as index.html and open in a browser to run it locally.