Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/autocomplete/Autocomplete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ type autocompleteProps = {
* onRemoveAll of the selected listItems
*/
onRemoveAll?: () => void;
/**
* Specifies if that field needs to be filled or not
*/
required?: boolean;
};

//remove the default style from a button
Expand Down Expand Up @@ -161,6 +165,7 @@ export const Autocomplete = ({
onRemove,
onRemoveAll,
onFocus,
required = false,
props,
}: autocompleteProps) => {
const [activeOption, setActiveOption] = useState<number>(0);
Expand Down Expand Up @@ -280,6 +285,7 @@ export const Autocomplete = ({
type="text"
value={userInput}
onChange={handleChange}
required = {required}
inputProps={{
onKeyDown: onKeyDown,
autoComplete: 'off',
Expand Down Expand Up @@ -314,6 +320,7 @@ export const Autocomplete = ({
type="text"
value={userInput}
onChange={handleChange}
required = {required}
inputProps={{
onKeyDown: onKeyDown,
autoComplete: 'off',
Expand Down
12 changes: 12 additions & 0 deletions src/autocomplete/__tests__/Autocomplete.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -353,4 +353,16 @@ describe('FormInput', () => {

expect(onRemoveAllHandler).toHaveBeenCalled();
});

it('should check that required attribute is defaulted to false', () => {
render(<Autocomplete options={[]} />);
const input: any = screen.getByRole('textbox');
expect(input.required).toBe(false);
});

it('should check that if required is set to true, input child is rendered with the attribute', () => {
render(<Autocomplete options={[]} required />);
const input: any = screen.getByRole('textbox');
expect(input.required).toBe(true);
});
});
7 changes: 7 additions & 0 deletions src/formInput/FormInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ type FormInputProps = {
* allow to define an hint
*/
hint?: HintProps;
/**
* Specifies if that field needs to be filled or not
*/
required?: boolean;
};

export enum ErrorPosition {
Expand Down Expand Up @@ -133,6 +137,7 @@ export const FormInput = ({
containerClassName,
containerClassNameError,
labelClassName,
required,
hint,
inputDivProps = { style: { display: 'flex' } },
}: FormInputProps) => {
Expand Down Expand Up @@ -202,6 +207,7 @@ export const FormInput = ({
type={type}
value={value}
onChange={handleChange}
required = {required}
className={inputClassName}
aria-label={ariaLabel || name}
{...inputProps}
Expand All @@ -216,6 +222,7 @@ export const FormInput = ({
type={type}
value={value}
onChange={handleChange}
required = {required}
className={inputClassName}
aria-label={ariaLabel || name}
{...inputProps}
Expand Down
1 change: 1 addition & 0 deletions stories/Autocomplete/Documentation.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ An example with all the available properties is (the only mandatory property is
resultActiveClass="resultActiveClass"
notFoundText="No fruit found"
onSelected={handleSelected}
required = {false}
/>
```

Expand Down
41 changes: 41 additions & 0 deletions stories/Autocomplete/Styled.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,47 @@ The list of available options are:
</Story>
</Canvas>


# Required

<Canvas>
<Story name="required">
{() => {
const handleSelected = value => {
console.log(value);
};
return (
<form>
<Autocomplete
options={[
'Papaya',
'Persimmon',
'Paw Paw',
'Prickly Pear',
'Peach',
'Pomegranate',
'Pineapple',
]}
minCharsBeforeSearch={1}
debounceMs={100}
hintText="Select your fruit"
hintClass="autocomplete__label"
inputProps={{ className: 'govuk-input' }}
resultUlClass="autocomplete__menu"
resultlLiClass="autocomplete__option"
resultNoOptionClass="resultNoOptionClass"
resultActiveClass="autocomplete__option"
notFoundText="No fruit found"
onSelected={handleSelected}
required
/>
<button type="submit">Search</button>
</form>
);
}}
</Story>
</Canvas>

# Properties

<Props of={Autocomplete} />
1 change: 1 addition & 0 deletions stories/Input/Documentation.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ An example with all the available properties is:
inputClassName="someClassName"
containerClassName="myClassName"
labelClassName="myClassName"
required = {required}
inputProps={{
id: 'password'
placeholder: 'enter your password',
Expand Down
2 changes: 1 addition & 1 deletion stories/liveEdit/AutocompleteLive.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function AutocompleteDemo() {
resultActiveClass=""
inputProps={{}}
notFoundText="No fruit found"
onSelected={handleSelected}
onSelected={handleSelected}
/>
)
}
Expand Down