Skip to content

Commit

Permalink
PoC for creatable
Browse files Browse the repository at this point in the history
  • Loading branch information
vmilan committed May 31, 2024
1 parent 41c62d0 commit 119ac75
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 47 deletions.
8 changes: 5 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Not released

- Add creatable functionality to Autocomplete [#828](https://github.com/CartoDB/carto-react/pull/828)

## 3.0.0

### 3.0.0-alpha.9 (2024-05-29)
Expand All @@ -16,7 +18,7 @@

### 3.0.0-alpha.7 (2024-04-16)

- Update Deck GL v9 and removed dropping features functionality [#838](https://github.com/CartoDB/carto-react/pull/838)
- Update Deck GL v9 and removed dropping features functionality [#838](https://github.com/CartoDB/carto-react/pull/838)

## 2.5

Expand All @@ -26,8 +28,8 @@

### 2.5.2 (2024-04-10)

- configurable timeseries y axis [#861](https://github.com/CartoDB/carto-react/pull/861)
- configurable histogram y axis [#860](https://github.com/CartoDB/carto-react/pull/860)
- configurable timeseries y axis [#861](https://github.com/CartoDB/carto-react/pull/861)
- configurable histogram y axis [#860](https://github.com/CartoDB/carto-react/pull/860)

### 2.5.1 (2024-04-03)

Expand Down
16 changes: 4 additions & 12 deletions packages/react-ui/src/components/atoms/SelectField.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const PlaceholderItem = styled(MenuItem)(() => ({
display: 'none'
}));

const SelectField = forwardRef(
const Autocomplete = forwardRef(
(
{
children,
Expand Down Expand Up @@ -137,16 +137,8 @@ const SelectField = forwardRef(
}
);

SelectField.defaultProps = {
size: 'small'
};
SelectField.propTypes = {
placeholder: PropTypes.oneOfType([PropTypes.string, PropTypes.element]),
size: PropTypes.oneOf(['small', 'medium']),
menuProps: PropTypes.object,
inputProps: PropTypes.object,
helperText: PropTypes.oneOfType([PropTypes.string, PropTypes.element]),
labelSecondary: PropTypes.element
Autocomplete.propTypes = {
creatable: PropTypes.bool
};

export default SelectField;
export default Autocomplete;
8 changes: 8 additions & 0 deletions packages/react-ui/src/components/molecules/Autocomplete.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { AutocompleteProps as MuiAutocompleteProps } from '@mui/material/Autocomplete';

export type AutocompleteProps = MuiAutocompleteProps & {
creatable?: boolean;
};

declare const Autocomplete: (props: AutocompleteProps) => JSX.Element;
export default Autocomplete;
79 changes: 79 additions & 0 deletions packages/react-ui/src/components/molecules/Autocomplete.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import React from 'react';
import PropTypes from 'prop-types';
import {
Autocomplete as MuiAutocomplete,
createFilterOptions,
TextField
} from '@mui/material';

const filter = createFilterOptions();

const Autocomplete = ({ options, creatable, ...otherProps }) => {
const [value, setValue] = React.useState(null);

return (
<MuiAutocomplete
{...otherProps}
creatable={creatable}
value={value}
onChange={(event, newValue) => {
if (typeof newValue === 'string') {
setValue({
title: newValue
});
} else if (newValue && newValue.inputValue) {
// Create a new value from the user input
setValue({
title: newValue.inputValue
});
} else {
setValue(newValue);
}
}}
filterOptions={(options, params) => {
const filtered = filter(options, params);

const { inputValue } = params;
// Suggest the creation of a new value
const isExisting = options.some((option) => inputValue === option.title);
if (inputValue !== '' && !isExisting) {
filtered.push({
inputValue,
title: `Add "${inputValue}"`
});
}

return filtered;
}}
selectOnFocus
clearOnBlur
handleHomeEndKeys
options={options}
getOptionLabel={(option) => {
// Value selected with enter, right from the input
if (typeof option === 'string') {
return option;
}
// Add "xxx" option created dynamically
if (option.inputValue) {
return option.inputValue;
}
// Regular option
return option.title;
}}
renderOption={(props, option) => <li {...props}>{option.title}</li>}
sx={{ width: 300 }}
freeSolo
renderInput={(params) => <TextField {...params} label='Free solo with text demo' />}
/>
);
};

Autocomplete.defaultProps = {
size: 'medium'
};
Autocomplete.propTypes = {
creatable: PropTypes.bool
};

export default Autocomplete;
7 changes: 5 additions & 2 deletions packages/react-ui/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import BarWidgetUI from './widgets/BarWidgetUI/BarWidgetUI';
import HistogramWidgetUI from './widgets/HistogramWidgetUI/HistogramWidgetUI';
import PieWidgetUI from './widgets/PieWidgetUI/PieWidgetUI';
import LegendWidgetUI from './widgets/legend/LegendWidgetUI';
import LEGEND_TYPES from './widgets/legend/legend-types/LegendTypes'
import LEGEND_TYPES from './widgets/legend/legend-types/LegendTypes';
import LegendCategories from './widgets/legend/legend-types/LegendCategories';
import LegendIcon from './widgets/legend/legend-types/LegendIcon';
import LegendProportion from './widgets/legend/legend-types/LegendProportion';
Expand Down Expand Up @@ -52,6 +52,7 @@ import UploadField, {
import UploadFieldBase, {
UploadFieldBaseProps
} from './components/molecules/UploadField/UploadFieldBase';
import Autocomplete, { AutocompleteProps } from './components/molecules/Autocomplete';
import AppBar, { AppBarProps } from './components/organisms/AppBar/AppBar';
import LabelWithIndicator, {
LabelWithIndicatorProps
Expand Down Expand Up @@ -121,6 +122,8 @@ export {
UploadFieldProps,
UploadFieldBase,
UploadFieldBaseProps,
Autocomplete,
AutocompleteProps,
AppBar,
AppBarProps,
LabelWithIndicator,
Expand All @@ -137,4 +140,4 @@ export {
Alert,
AlertProps,
CartoAlertSeverity
};
};
2 changes: 2 additions & 0 deletions packages/react-ui/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import SelectField from './components/atoms/SelectField';
import MultipleSelectField from './components/molecules/MultipleSelectField/MultipleSelectField';
import UploadField from './components/molecules/UploadField/UploadField';
import UploadFieldBase from './components/molecules/UploadField/UploadFieldBase';
import Autocomplete from './components/molecules/Autocomplete';
import AppBar from './components/organisms/AppBar/AppBar';
import LabelWithIndicator from './components/atoms/LabelWithIndicator';
import { getCartoColorStylePropsForItem } from './utils/palette';
Expand Down Expand Up @@ -103,6 +104,7 @@ export {
MultipleSelectField,
UploadField,
UploadFieldBase,
Autocomplete,
AppBar,
ArrowDropIcon,
LabelWithIndicator,
Expand Down
Loading

0 comments on commit 119ac75

Please sign in to comment.