te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - What is a role of input with type number - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What is a role of input with type number - Stack Overflow

programmeradmin3浏览0评论

I am using react testing library and by its conventions, elements should be accessed by getByRole.

When I had input type="text" the role was textarea which was fine.

Now I have input with type="number" and the error message suggests I use spinbutton as the role which works but it doesn't make sense to me.

Is there a different role for input with number type?

I am using react testing library and by its conventions, elements should be accessed by getByRole.

When I had input type="text" the role was textarea which was fine.

Now I have input with type="number" and the error message suggests I use spinbutton as the role which works but it doesn't make sense to me.

Is there a different role for input with number type?

Share Improve this question edited Jan 31, 2023 at 7:57 Yilmaz 49.5k18 gold badges215 silver badges268 bronze badges asked Jul 9, 2022 at 13:22 SrdjaNo1SrdjaNo1 9094 gold badges11 silver badges24 bronze badges 2
  • You can remove spin button Refer this stackoverflow./questions/3975769/… – rishabh tripathi Commented Jul 9, 2022 at 13:26
  • If you don’t want a spinbutton, it is quite likely that you are not using the correct input type. Is your input actually a (floating point) number? or does it merely consist of numbers? If it’s the latter, you would want to use inputtype=numeric pattern=[0-9]* – Andy Commented Jul 11, 2022 at 8:25
Add a ment  | 

2 Answers 2

Reset to default 11

As per MDN on <input type="number">:

The implicit role for the element is spinbutton.

So as long as you didn't change the role by means of a role attribute, this is the correct role to use in getByRole('spinbutton').

An input number is like a counter, so it is spinning its values up and down by clicking in the spin buttons on the right side or the keyboard arrows, (even its pseudo elements are ::-webkit-inner-spin-button, ::-webkit-outer-spin-button).

If this is surprising to you, that input type might not be the right choice for the form control. The documentation reads further:

If spinbutton is not an important feature for your form control, consider not using type="number". Instead, use inputmode="numeric" along with a pattern attribute that limits the characters to numbers and associated characters.

The number input has several accessibility and usability issues when used for anything that is not a floating point number.

Update (based on OP ment)

So would userEvent.type work on it since it is a spinbutton?

Yes it will work, here is a sandbox with a basic example


import { render, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import "@testing-library/jest-dom";

it("should type a number in input", () => {
  const { getByRole } = render(<input type="number" />);
  const input = getByRole("spinbutton");
  userEvent.type(input, "123456");
  waitFor(() => expect(input).toHaveValue("123456"));
});

I will show you how to fish. If you write this in test file

 screen.logTestingPlaygroundURL();

this will generate a URL, if you paste this to the browser, you will see the rendered ponent on the page:

If you click on the element, it will generate the suggested query:

发布评论

评论列表(0)

  1. 暂无评论