- 
          
 - 
                Notifications
    
You must be signed in to change notification settings  - Fork 479
 
feat: add native input DOM to follow standard behavior of HTML #1018
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
| 
           The latest updates on your projects. Learn more about Vercel for Git ↗︎ 
  | 
    
| 
           If there are any problems, I am willing to actively update this PR until it is merged.  | 
    
| expect(container.querySelector('input.rc-select-native-input').getAttribute('id')).toEqual('test'); | ||
| expect(container.querySelector('input.rc-select-native-input').getAttribute('name')).toEqual('test'); | ||
| expect(container.querySelector('input.rc-select-native-input').getAttribute('value')).toEqual('test 2'); | ||
| }) | 
Check notice
Code scanning / CodeQL
Semicolon insertion
| expect(container.querySelector('input.rc-select-native-input').getAttribute('id')).toEqual('test'); | ||
| expect(container.querySelector('input.rc-select-native-input').getAttribute('name')).toEqual('test'); | ||
| expect(container.querySelector('input.rc-select-native-input').getAttribute('value')).toEqual('test 1,test 2'); | ||
| }) | 
Check notice
Code scanning / CodeQL
Semicolon insertion
| expect(container.querySelector('input.rc-select-native-input').getAttribute('id')).toEqual('test'); | ||
| expect(container.querySelector('input.rc-select-native-input').getAttribute('name')).toEqual('test'); | ||
| expect(container.querySelector('input.rc-select-native-input').getAttribute('value')).toEqual('test 1,test 2'); | ||
| }) | 
Check notice
Code scanning / CodeQL
Semicolon insertion
          Codecov ReportAll modified and coverable lines are covered by tests ✅ 
 Additional details and impacted files@@           Coverage Diff           @@
##           master    #1018   +/-   ##
=======================================
  Coverage   99.78%   99.78%           
=======================================
  Files          38       39    +1     
  Lines        1398     1405    +7     
  Branches      391      391           
=======================================
+ Hits         1395     1402    +7     
  Misses          3        3           ☔ View full report in Codecov by Sentry.  | 
    
| /> | ||
| </span> | ||
| 
               | 
          ||
| <SelectNativeInput | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There exist a input for search usage. Seems that one should be aria hidden?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The role of the search input is a combobox, which contains many aria-* attributes that should not be aria hidden.
<SelectNativeInput /> is already aria hidden.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why make SelectNativeInput aria hidden?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've changed the code:
export default React.forwardRef<HTMLInputElement, SelectNativeInputProps>(
  function SelectNativeInput(props, ref) {
    const { prefixCls, className, ...rest } = props;
    return (
      <input
-       aria-hidden
-       tabIndex={-1}
        type="hidden"
-       readOnly
        ref={ref}
        className={classNames(`${prefixCls}-native-input`, className)}
        {...rest}
      />
    );
  },
);Thank you. You're right. aria-hidden shouldn't be set. But type="hidden" was already set. The screen reader still can't read it.
Should we remove the type='hidden' and add some accessibility attributes to make it accessible to screen readers?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess could use opacity: 0 + 0 width & height to let it invisible on the screen but still workable with reader.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
import classNames from 'classnames';
import React from 'react';
+ const visuallyHidden: React.CSSProperties = {
+   position: 'absolute',
+   overflow: 'hidden',
+   width: 1,
+   height: 1,
+   border: 0,
+   margin: -1,
+   padding: 0,
+   clip: 'rect(0 0 0 0)',
+   whiteSpace: 'nowrap',
+ };
interface SelectNativeInputProps extends React.InputHTMLAttributes<HTMLInputElement> {
  prefixCls?: string;
}
export default React.forwardRef<HTMLInputElement, SelectNativeInputProps>(
  function SelectNativeInput(props, ref) {
    const { prefixCls, className, style, ...rest } = props;
    return (
      <input
-       type="hidden"
        ref={ref}
        className={classNames(`${prefixCls}-native-input`, className)}
+       style={{ ...visuallyHidden, ...style }}
        {...rest}
      />
    );
  },
);6b03dea    to
    df533c3      
    Compare
  
    df533c3    to
    1de5b3d      
    Compare
  
    1de5b3d    to
    82e600c      
    Compare
  
    | 
           Since Select always has the search input, seems it will confuse the reader to check the real value holder if search input exist. Could it to add aria value on the search input instead to patch another input?  | 
    
          
 What aria value should I add to the search input?  | 
    
| 
           any update on this?  | 
    
Related
#790
ant-design/ant-design#36489
💡 Background and solution
<Select />is a form element which should have the ability to interact with the form.Ant Design is a great project that has brought great convenience to front-end developers all over the world.
However, until now, Ant Design's
<Select />still doesn't support this functionality. We had to use some hacks to implement it:This PR implements the above functionality and simply passes in
nativeInputPropsto do so:const App = () => { const defaultValue = "lucy"; - const [name, setName] = useState(defaultValue); const handleChange = (changedValue: string) => { setName(changedValue); }; const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => { event.preventDefault(); const formData = new FormData(event.currentTarget); console.log("name", formData.get("name")); }; return ( <form onSubmit={handleSubmit}> <Select defaultValue={defaultValue} style={{ width: 120 }} onChange={handleChange} + nativeInputProps={{ name: 'name' }} options={[ { value: "jack", label: "Jack" }, { value: "lucy", label: "Lucy" }, { value: "max", label: "Max" }, ]} /> - <input name="name" type="hidden" value={name} /> <button type="submit">Submit</button> </form> ); };The above code renders a hidden input DOM to receive form related attributes. In case of
<Select />withmultipleortagsmode, the value of the input is a comma-separated string.The type of
nativeInputPropsisReact.InputHTMLAttributes<HTMLInputElement>, which accepts all input props.