Replace datalist with my own list

This commit is contained in:
Lim Chee Aun 2023-04-18 23:33:59 +08:00
parent 60c635b610
commit fdf30c37cb
2 changed files with 81 additions and 19 deletions

View file

@ -22,5 +22,32 @@
#login input { #login input {
display: block; display: block;
width: 100%; width: 15em;
margin: 0 auto;
max-width: 100%;
transition: all 0.2s ease-in-out;
}
#instances-suggestions {
margin: 0.2em 0 0;
padding: 0;
list-style: none;
width: 90vw;
max-width: 40em;
overflow: auto;
white-space: nowrap;
mask-image: linear-gradient(
to right,
transparent,
black 1.2em,
black calc(100% - 5em),
transparent
);
animation: fade-in 0.2s ease-in-out;
height: 2.5em;
}
#instances-suggestions li {
display: inline-block;
margin: 0;
padding: 0;
} }

View file

@ -14,6 +14,9 @@ function Login() {
const instanceURLRef = useRef(); const instanceURLRef = useRef();
const cachedInstanceURL = store.local.get('instanceURL'); const cachedInstanceURL = store.local.get('instanceURL');
const [uiState, setUIState] = useState('default'); const [uiState, setUIState] = useState('default');
const [instanceText, setInstanceText] = useState(
cachedInstanceURL?.toLowerCase() || '',
);
const [instancesList, setInstancesList] = useState([]); const [instancesList, setInstancesList] = useState([]);
useEffect(() => { useEffect(() => {
@ -29,20 +32,13 @@ function Login() {
})(); })();
}, []); }, []);
useEffect(() => { // useEffect(() => {
if (cachedInstanceURL) { // if (cachedInstanceURL) {
instanceURLRef.current.value = cachedInstanceURL.toLowerCase(); // instanceURLRef.current.value = cachedInstanceURL.toLowerCase();
} // }
}, []); // }, []);
const onSubmit = (e) => { const submitInstance = (instanceURL) => {
e.preventDefault();
const { elements } = e.target;
let instanceURL = elements.instanceURL.value.toLowerCase();
// Remove protocol from instance URL
instanceURL = instanceURL.replace(/^https?:\/\//, '').replace(/\/+$/, '');
// Remove @acct@ or acct@ from instance URL
instanceURL = instanceURL.replace(/^@?[^@]+@/, '');
store.local.set('instanceURL', instanceURL); store.local.set('instanceURL', instanceURL);
(async () => { (async () => {
@ -71,6 +67,22 @@ function Login() {
})(); })();
}; };
const onSubmit = (e) => {
e.preventDefault();
const { elements } = e.target;
let instanceURL = elements.instanceURL.value.toLowerCase();
// Remove protocol from instance URL
instanceURL = instanceURL.replace(/^https?:\/\//, '').replace(/\/+$/, '');
// Remove @acct@ or acct@ from instance URL
instanceURL = instanceURL.replace(/^@?[^@]+@/, '');
if (!/\./.test(instanceURL)) {
instanceURL = instancesList.find((instance) =>
instance.includes(instanceURL),
);
}
submitInstance(instanceURL);
};
return ( return (
<main id="login" style={{ textAlign: 'center' }}> <main id="login" style={{ textAlign: 'center' }}>
<form onSubmit={onSubmit}> <form onSubmit={onSubmit}>
@ -78,34 +90,57 @@ function Login() {
<label> <label>
<p>Instance</p> <p>Instance</p>
<input <input
value={instanceText}
required required
type="text" type="text"
class="large" class="large"
id="instanceURL" id="instanceURL"
ref={instanceURLRef} ref={instanceURLRef}
disabled={uiState === 'loading'} disabled={uiState === 'loading'}
list="instances-list" // list="instances-list"
autocorrect="off" autocorrect="off"
autocapitalize="off" autocapitalize="off"
autocomplete="off" autocomplete="off"
spellcheck={false} spellcheck={false}
placeholder="instance domain"
onInput={(e) => {
setInstanceText(e.target.value);
}}
/> />
<datalist id="instances-list"> <ul id="instances-suggestions">
{instancesList
.filter((instance) => instance.includes(instanceText))
.slice(0, 10)
.map((instance) => (
<li>
<button
type="button"
class="plain4"
onClick={() => {
submitInstance(instance);
}}
>
{instance}
</button>
</li>
))}
</ul>
{/* <datalist id="instances-list">
{instancesList.map((instance) => ( {instancesList.map((instance) => (
<option value={instance} /> <option value={instance} />
))} ))}
</datalist> </datalist> */}
</label> </label>
{uiState === 'error' && ( {uiState === 'error' && (
<p class="error"> <p class="error">
Failed to log in. Please try again or another instance. Failed to log in. Please try again or another instance.
</p> </p>
)} )}
<p> <div>
<button class="large" disabled={uiState === 'loading'}> <button class="large" disabled={uiState === 'loading'}>
Log in Log in
</button>{' '} </button>{' '}
</p> </div>
<Loader hidden={uiState !== 'loading'} /> <Loader hidden={uiState !== 'loading'} />
<hr /> <hr />
<p> <p>