Skip to content

Commit 8952b98

Browse files
authored
Feature: Added inputRender prop to have the ability to replace default input element with custom. (#222)
* Added inputRender prop to have the ability to replace default input element with custom. * Added test for inputRender prop
1 parent 234dd2a commit 8952b98

File tree

3 files changed

+48
-18
lines changed

3 files changed

+48
-18
lines changed

src/Picker.tsx

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ export type PickerSharedProps<DateType> = {
6868
superNextIcon?: React.ReactNode;
6969
getPopupContainer?: (node: HTMLElement) => HTMLElement;
7070
panelRender?: (originPanel: React.ReactNode) => React.ReactNode;
71+
inputRender?: (props: React.InputHTMLAttributes<HTMLInputElement>) => React.ReactNode;
7172

7273
// Events
7374
onChange?: (value: DateType | null, dateString: string) => void;
@@ -175,6 +176,7 @@ function InnerPicker<DateType>(props: PickerProps<DateType>) {
175176
onSelect,
176177
direction,
177178
autoComplete = 'off',
179+
inputRender,
178180
} = props as MergedPickerProps<DateType>;
179181

180182
const inputRef = React.useRef<HTMLInputElement>(null);
@@ -457,6 +459,31 @@ function InnerPicker<DateType>(props: PickerProps<DateType>) {
457459
);
458460
}
459461

462+
const mergedInputProps: React.InputHTMLAttributes<HTMLInputElement> = {
463+
id,
464+
tabIndex,
465+
disabled,
466+
readOnly: inputReadOnly || typeof formatList[0] === 'function' || !typing,
467+
value: hoverValue || text,
468+
onChange: (e) => {
469+
triggerTextChange(e.target.value);
470+
},
471+
autoFocus,
472+
placeholder,
473+
ref: inputRef,
474+
title: text,
475+
...inputProps,
476+
size: getInputSize(picker, formatList[0], generateConfig),
477+
...getDataOrAriaProps(props),
478+
autoComplete,
479+
};
480+
481+
const inputNode: React.ReactNode = inputRender ? (
482+
inputRender(mergedInputProps)
483+
) : (
484+
<input {...mergedInputProps} />
485+
);
486+
460487
// ============================ Warning ============================
461488
if (process.env.NODE_ENV !== 'production') {
462489
warning(
@@ -521,24 +548,7 @@ function InnerPicker<DateType>(props: PickerProps<DateType>) {
521548
})}
522549
ref={inputDivRef}
523550
>
524-
<input
525-
id={id}
526-
tabIndex={tabIndex}
527-
disabled={disabled}
528-
readOnly={inputReadOnly || typeof formatList[0] === 'function' || !typing}
529-
value={hoverValue || text}
530-
onChange={(e) => {
531-
triggerTextChange(e.target.value);
532-
}}
533-
autoFocus={autoFocus}
534-
placeholder={placeholder}
535-
ref={inputRef}
536-
title={text}
537-
{...inputProps}
538-
size={getInputSize(picker, formatList[0], generateConfig)}
539-
{...getDataOrAriaProps(props)}
540-
autoComplete={autoComplete}
541-
/>
551+
{inputNode}
542552
{suffixNode}
543553
{clearNode}
544554
</div>

tests/__snapshots__/picker.spec.tsx.snap

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,20 @@ exports[`Picker.Basic icon 1`] = `
2929
</div>
3030
`;
3131

32+
exports[`Picker.Basic inputRender 1`] = `
33+
<div
34+
class="rc-picker-input"
35+
>
36+
<input
37+
autocomplete="off"
38+
readonly=""
39+
size="12"
40+
title=""
41+
value=""
42+
/>
43+
</div>
44+
`;
45+
3246
exports[`Picker.Basic panelRender 1`] = `
3347
Array [
3448
<div

tests/picker.spec.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,12 @@ describe('Picker.Basic', () => {
497497
expect(wrapper.find('.rc-picker-input').render()).toMatchSnapshot();
498498
});
499499

500+
it('inputRender', () => {
501+
const wrapper = mount(<MomentPicker inputRender={(props) => <input {...props} />} />);
502+
503+
expect(wrapper.find('.rc-picker-input').render()).toMatchSnapshot();
504+
});
505+
500506
describe('showNow', () => {
501507
it('datetime should display now', () => {
502508
const onSelect = jest.fn();

0 commit comments

Comments
 (0)